不要使用编译安装LNMP……

说的就是你,LNMP一键安装包

安装 PHP

1. 添加EPEL和REMI存储库

yum install epel-release
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

 

2. 根据需要配置 PHP 7.x 存储库

yum install yum-utils

# PHP 7.1
yum-config-manager --enable remi-php71
# PHP 7.2
yum-config-manager --enable remi-php72
# PHP 7.3
yum-config-manager --enable remi-php73
# PHP 7.4
yum-config-manager --enable remi-php74

 

3. 安装 php 及其扩展

yum install php php-cli php-fpm php-common php-opcache php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis php-mysql

 

4. 查看 PHP 版本及扩展

php -v

php --modules

 

安装 Nginx

1. 安装 EPEL 仓库

yum install epel-release

 

2. 安装 Nginx

yum install nginx

 

3. 启动和设置开机自启

systemctl start nginx

systemctl enable nginx

 

配置 Nginx 启用 PHP

1. 修改 nginx 配置文件

vim /etc/nginx/nginx.conf

 

参考如下修改:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

# 这里把用户修改为nginx用户
user nginx nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    server_tokens off;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # 这里手动添加一些 fastcgi 参数
    # fastcgi config
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;

    # gzip config
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";


    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

}

 

2. 在 /etc/nginx/ 中新建一个配置文件

vim /etc/nginx/enable-php.conf

 

根据官方文档添加如下内容:

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";

    fastcgi_pass 127.0.0.1:9000;
    # If you’re using unix socket change fastcgi_pass to:
    # fastcgi_pass unix:/var/run/php-fpm.sock;

    fastcgi_index index.php;

    # include the fastcgi_param setting
    # include fastcgi_params;
    include fastcgi.conf;

    # SCRIPT_FILENAME parameter is used for PHP FPM determining
    #  the script name. If it is not set in fastcgi_params file,
    # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
    # please comment off following line:
    # fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

 

3. 修改 PHP 配置

编辑php配置文件

vim /etc/php.ini

 

取消注释并配置时区

date.timezone = PRC 

 

3. 修改 php-fpm 配置

编辑 php-fpm 配置文件

vim /etc/php-fpm.d/www.conf

 

修改用户和用户组为 nginx

user = nginx
group = nginx

 

设置 php-fpm 自启

systemctl enable php-fpm

systemctl start php-fpm

 

4. 在需要启用 php 的 nginx 服务中增加配置

对于需要启用php的服务,只需要在配置的 server 部分插入之前添加的 enable-php.conf 配置文件即可

    server {
        listen       443 default_server ssl http2;
        listen       [::]:443 default_server ssl http2;
        server_name  _;
        root         /usr/share/nginx/default;

        # 其他配置省略
        # ......

        # 插入以下这行来启用 PHP
        include /etc/nginx/enable-php.conf;

    }

 

5. 重启相关服务

systemctl restart php-fpm

systemctl restart nginx

 

PHP 安全加固

修改配置以加强 php 安全性

编辑php配置文件

vim /etc/php.ini

 

1. 禁止使用的PHP危险函数

Web木马程序通常利用php的特殊函数执行系统命令,查询任意目录文件,增加修改删除文件等。

php木马程序常使用的函数为:dl, assert, exec, popen, system, passthru, shell_exec等。

disable_functions = phpinfo,escapeshellarg,escapeshellcmd,proc_close,assert,error_log,pfsockopen,putenv,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_set,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

 

2. 禁止显示 php 版本信息

禁止 php 版本信息在 HTTP 头部内容中泄露:

expose_php = Off

 

3. 控制 php 脚本能访问的目录

使用open_basedir选项能够控制 PHP 脚本只能访问指定的目录,这样能够避免 PHP 脚本访问不应该访问的文件,一定程度下降低了 phpshell 的危害,可以防止php木马跨站。一般情况下,可以设置为只能访问网站目录和 /tmp 目录:

open_basedir = /usr/share/nginx/:/tmp/

 

4. 关闭注册全局变量

该选项参数在 PHP 5.3 以后的版本中已被移除。

在PHP中提交的变量,包括使用POST或者GET提交的变量,会自动注册为全局变量,能够直接访问,这是对服务器非常不安全的,所以不能让它注册为全局变量,就把注册全局变量选项关闭。

register_globals = Off

 

5. SQL 注入防护

该选项参数在 PHP 5.4 以后的版本中已被移除。

magic_quotes_gpc选项默认是关闭的。如果打开该选项,PHP 将会把引用的数据中包含单引号 ‘ 和双引号 ” 以及反斜线 \ 自动加上反斜线,自动转译符号,确保数据操作的正确运行,magic_quotes_gpc的设定值将会影响通过Get / Post / Cookies 获得的数据,可以有效的防止 SQL 注入漏洞。

magic_quotes_gpc = on

 

6. 错误信息控制

一般 PHP 环境在没有连接到数据库或者其他情况下会有错误提示信息,错误信息中可能包含 PHP 脚本当前的路径信息或者查询的 SQL 语句等信息,这类信息如果暴露给黑客是不安全的,因此建议禁止该错误提示:

display_errors = Off

 

如果确实要显示错误信息,一定要设置显示错误信息的级别。例如,只显示警告以上的错误信息:

error_reporting = E_WARNING & E_ERROR

 

在关闭错误提示信息后,对于错误信息进行记录,便于排查服务器运行异常的原因:

log_errors = On

 

同时,需要设置错误日志存放的目录,建议您将 PHP 错误日志与 nginx 的日志存放在同一目录下:

注意: 该文件必须设置允许 Nginx 用户或用户组具有写的权限。
error_log = /var/log/php/error.log

 

7. 禁止访问远程文件

允许访问URL远程资源使得PHP应用程序的漏洞变得更加容易被利用,php脚本若存在远程文件包含漏洞可以让攻击者直接获取网站权限及上传web木马,一般会在php配置文件中关闭该功能,若需要访问远程服务器建议采用其他方式,如 libcurl 库。

allow_url_fopen = Off

 

8. nginx文件解析漏洞防御

在 php-fpm 配置文件中修改 security.limit_extensions 参数,限制PHP脚本引擎只支持解析哪些扩展名的文件:

vim /etc/php-fpm.d/www.conf

 

修改 security.limit_extensions:

; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; execute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
security.limit_extensions = .php .php3 .php4 .php5 .php7

 

安装 MariaDB

使用 MariaDB 代替Mysql

1. 安装、启动、设置自启

yum install mariadb-server

systemctl start mariadb

systemctl enable mariadb

 

2. 初始化数据库

mysql_secure_installation

 

3. 修改数据库默认字符集

编辑数据库配置:

vim /etc/my.cnf.d/server.cnf

 

在 [mysqld] 部分添加:

[mysqld]
character_set_server=utf8mb4
collation-server=utf8mb4_unicode_ci

 

修改客户端配置:

vim /etc/my.cnf.d/client.cnf

 

在 [client] 部分添加:

[client]
character_set_server=utf8mb4
collation-server=utf8mb4_unicode_ci

 

重启 mariadb 服务:

systemctl restart mariadb

 

安装/升级到 MariaDB 10

CentOS 7 默认源最新为 MariaDB Server 5.5,如需使用新特性(包括对应于 MySQL 7 以上版本的特性及 MariaDB Community 10 以上版本的特性)请升级 MariaDB,参考 MariaDB 文档
从 MariaDB 5.5 升级到 MariaDB 10 与新安装的步骤一致,升级后数据库内容将会保留,但为保障数据安全,仍建议升级前备份数据库

1. 首先下载并使用 mariadb_repo_setup 脚本为 YUM 配置 MariaDB 存储库:

yum install wget
wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup
./mariadb_repo_setup

 

2. 安装 MariaDB Community Server 及其依赖:

yum install MariaDB-server

 

3. 配置 MariaDB(若为新安装):

启用服务:

systemctl enable mariadb
systemctl start mariadb

 

初始化配置(10.5.2 版本后有所变化,见文档):

# MariaDB starting with 10.4.6
mysql_secure_installation

# MariaDB starting with 10.5.2
mariadb-secure-installation 

 


到这里就基本安装配置完成了,接下来根据自己需求详细配置就行了。


本文被阅读了:1,858次

作者头像
关于  Ailitonia

正因站在了巨人的肩膀上,才越发觉得自己渺小。不求成为巨人,但求与其同行。 把自己所见所闻,记录下来。

→查看所有由Ailitonia发布的文章