CentOS7 下安装PHP环境并且配置 Nginx 支持 php-fpm 模块

in 运维 with 0 comment

系统环境

CentOS Linux release 7.6.1810 (Core)

用手边一台测试服务器测试一下LNMP环境,如果谁感兴趣,可以参考下。
可以用下列命令查看自己的 CentOS 系统版本:

cat /etc/redhat-release
cat /proc/version

安装 Nginx

采用 yum 源方式安装,与采用源码包编译安装各有优缺点,下次再尝试源码包方式。

一、 Nginx 官网向导方式:http://nginx.org/en/linux_packages.html

vim /etc/yum.repos.d/nginx.repo
''' 官网建议填入如下内容 '''
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

''' 在我的环境中可以适当简化 '''
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

''' 检查 '''
yum repolist

二、 使用 rpm 包安装方式获取 yum 源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
''' 安装 '''
yum -y install nginx
''' 启动 '''
systemctl start nginx.service
systemctl enable nginx.service
''' 运行状态 '''
systemctl status nginx.service
ps -ef | grep nginx

''' 查看nginx的版本 '''
nginx -V

安装 PHP

CentOS 默认 yum 源是 php5.4

''' 更新 yum 源 '''
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

''' 安装 PHP7.2 及常用扩展 '''
yum -y install php72w php72w-common php72w-fpm
yum -y install php72w-gd php72w-xml php72w-mbstring php72w-ldap php72w-pear php72w-xmlrpc php72w-cli
yum -y install php72w-mcrypt php72w-devel php72w-pecl-redis php72w-mysql

''' 测试 '''
php -v
php -m

''' 启动 '''
systemctl start php-fpm.service
systemctl enable php-fpm.service
''' php-fpm 服务默认使用 9000 端口 '''
netstat -tln | grep 9000

''' 修改配置文件 '''
vim /etc/php.ini
...
cgi.fix_pathinfo=0
''' 这一项默认被注释并且值为1。根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,
从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0 '''

配置 Nginx 支持 PHP

''' 修改配置文件 '''
vim /etc/nginx/conf.d/default.conf 
...
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm index.php;    >>> 追加 index.php
}
... 
''' 紧接着找到 location ~ \.php$ 这段,将前面的#号去掉,将root后面的替换为你的网站根目录,
将/scripts替换为 $document_root '''
location ~ \.php$ {
    root           /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

''' 重启 nginx '''
systemctl restart nginx.service

''' 编辑测试文件,在浏览器打开,看看是否成功输出 '''
vim /usr/share/nginx/html/test.php
...
<?php
    phpinfo();
?>

安装 MySQL(MariaDB)

请参考:https://www.shiyl.com/archives/db_install.html


配置 Nginx 支持 https

''' 根据个人使用习惯,创建文件夹用来管理证书 '''
mkdir /etc/pki/nginx    >>> 放置证书:*.pem 或者 *.crt
mkdir /etc/pki/nginx/private    >>> 放置私钥:*.key

''' 修改配置文件 '''
vim /etc/nginx/conf.d/default.conf 
...    ''' 添加 server 节点 '''
server {
    listen       443 ssl;
    server_name  YOUR_DOMAIN_NAME;

    ssl_certificate /etc/pki/nginx/YOUR_CERTIFICATE_FILE.pem;    >>> 现在也流行 *.crt
    ssl_certificate_key /etc/pki/nginx/private/YOUR_PRIVATE_KEY_FILE.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

''' 测试并重启 nginx 服务 '''
nginx -t
systemctl restart nginx.service
  • 配置 http 自动跳转到 https
''' 修改配置文件 '''
vim /etc/nginx/conf.d/default.conf 
...
server {
    listen       80;
...
    rewrite ^(.*)$ https://$host$1 permanent;    >>> 添加 rewrite 条目
...

SSL证书请参考:https://www.shiyl.com/archives/ssl_cert.html

Comments are closed.