Nginx反向代理功能-实现FastCGI实战案例

           Nginx反向代理功能-实现FastCGI实战案例

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.FastCGI概述

1>.CGI的由来

  最早的Web服务器只能简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html文件,但是后期随着网站功能增多网站开发也越来越复杂,以至于出现动态技术,比如像php(1995年)、java(1995)、python(1991)语言开发的网站,但是nginx/apache服务器并不能直接运行php、java这样的文件。

  apache实现的方式是打补丁,但是nginx缺通过与第三方基于协议实现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户的请求,处理完成后返回数据给Nginx并回收进程,最后nginx在返回给客户端,那这个约定就是通用网关接口(common gateway interface,简称CGI),CGI(协议)是web服务器和外部应用程序之间的接口标准,是cgi程序和web服务器之间传递信息的标准化接口。

2>.为什么FastCGI 

  CGI协议虽然解决了语言解析器和seb server之间通讯的问题,但是它的效率很低,因为web server每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率。

3>.什么是PHP-FPM

  PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个实现了Fastcgi的程序,并且提供进程管理的功能。进程包括master进程和worker进程。master进程只有一个,负责监听端口,接受来自webserver的请求。worker进程一般会有多个,每个进程中会嵌入一个PHP解析器,进行PHP代码的处理。

二.Nginx与php-fpm在同一服务器实战案例

1>.安装php相关软件环境

[root@node101.yinzhengjie.org.cn ~]# yum -y install php-fpm php-mysql 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                                 | 8.7 kB  00:00:00     
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                                                                                 | 3.6 kB  00:00:00     
epel                                                                                                                                                 | 5.4 kB  00:00:00     
extras                                                                                                                                               | 2.9 kB  00:00:00     
updates                                                                                                                                              | 2.9 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                                                                          |  90 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                                                        | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                                                                        | 6.9 MB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package php-fpm.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-46.1.el7_7 for package: php-fpm-5.4.16-46.1.el7_7.x86_64
---> Package php-mysql.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 5.4.16-46.1.el7_7 for package: php-mysql-5.4.16-46.1.el7_7.x86_64
--> Running transaction check
---> Package php-common.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-46.1.el7_7.x86_64
---> Package php-pdo.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                  Arch                                 Version                                          Repository                             Size
============================================================================================================================================================================
Installing:
 php-fpm                                  x86_64                               5.4.16-46.1.el7_7                                updates                               1.4 M
 php-mysql                                x86_64                               5.4.16-46.1.el7_7                                updates                               101 k
Installing for dependencies:
 libzip                                   x86_64                               0.10.1-8.el7                                     base                                   48 k
 php-common                               x86_64                               5.4.16-46.1.el7_7                                updates                               565 k
 php-pdo                                  x86_64                               5.4.16-46.1.el7_7                                updates                                99 k

Transaction Summary
============================================================================================================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 2.2 M
Installed size: 8.8 M
Downloading packages:
(1/5): libzip-0.10.1-8.el7.x86_64.rpm                                                                                                                |  48 kB  00:00:00     
(2/5): php-common-5.4.16-46.1.el7_7.x86_64.rpm                                                                                                       | 565 kB  00:00:00     
(3/5): php-mysql-5.4.16-46.1.el7_7.x86_64.rpm                                                                                                        | 101 kB  00:00:00     
php-pdo-5.4.16-46.1.el7_7.x86_ FAILED                                          
http://mirror.lzu.edu.cn/centos/7.7.1908/updates/x86_64/Packages/php-pdo-5.4.16-46.1.el7_7.x86_64.rpm: [Errno 14] curl#56 - "Recv failure: Connection reset by peer":-- ETA 
Trying other mirror.
(4/5): php-fpm-5.4.16-46.1.el7_7.x86_64.rpm                                                                                                          | 1.4 MB  00:00:06     
(5/5): php-pdo-5.4.16-46.1.el7_7.x86_64.rpm                                                                                                          |  99 kB  00:00:05     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       358 kB/s | 2.2 MB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libzip-0.10.1-8.el7.x86_64                                                                                                                               1/5 
  Installing : php-common-5.4.16-46.1.el7_7.x86_64                                                                                                                      2/5 
  Installing : php-pdo-5.4.16-46.1.el7_7.x86_64                                                                                                                         3/5 
  Installing : php-mysql-5.4.16-46.1.el7_7.x86_64                                                                                                                       4/5 
  Installing : php-fpm-5.4.16-46.1.el7_7.x86_64                                                                                                                         5/5 
  Verifying  : php-common-5.4.16-46.1.el7_7.x86_64                                                                                                                      1/5 
  Verifying  : libzip-0.10.1-8.el7.x86_64                                                                                                                               2/5 
  Verifying  : php-pdo-5.4.16-46.1.el7_7.x86_64                                                                                                                         3/5 
  Verifying  : php-mysql-5.4.16-46.1.el7_7.x86_64                                                                                                                       4/5 
  Verifying  : php-fpm-5.4.16-46.1.el7_7.x86_64                                                                                                                         5/5 

Installed:
  php-fpm.x86_64 0:5.4.16-46.1.el7_7                                                  php-mysql.x86_64 0:5.4.16-46.1.el7_7                                                 

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7                        php-common.x86_64 0:5.4.16-46.1.el7_7                        php-pdo.x86_64 0:5.4.16-46.1.el7_7                       

Complete!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum -y install php-fpm php-mysql

2>. php相关参数优化

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^;|^$|^ " /etc/php-fpm.d/www.conf           #查看默认配置
[www]
listen = 127.0.0.1:9000                           #监听地址及IP地址
listen.allowed_clients = 127.0.0.1                     #允许客户端从哪个源IP地址访问,要允许所有IP地址,行首加";"注释即可
user = apache                                 #PHP-FPM启动的用户,会涉及到后期文件的权限问题
group = apache                                 #PHP-FPM启动的组,会涉及到后期文件的权限问题
pm = dynamic                                  #动态模块进程管理
pm.max_children = 50                             #静态方式下开启的php-fpm进程数量,在动态模式下他限定php-fpm的最大进程数
pm.start_servers = 5                             #动态模式下初始进程数,必须大于等于pm.min_spare_servers和小于等于pm.max_children的值
pm.min_spare_servers = 5                            #最小空闲进程数
pm.max_spare_servers = 35                           #最大空闲进程数
slowlog = /var/log/php-fpm/www-slow.log                  #慢日志路径
php_admin_value[error_log] = /var/log/php-fpm/www-error.log       #错误日志
php_admin_flag[log_errors] = on                       #开启错误日志
php_value[session.save_handler] = files                  #php session保存方式及路径
php_value[session.save_path] = /var/lib/php/session            #当时使用file保存session的文件路径
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# vim /etc/php-fpm.d/www.conf 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^;|^$|^ " /etc/php-fpm.d/www.conf       #优化后的配置
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 500
pm.start_servers = 100
pm.min_spare_servers = 100
pm.max_spare_servers = 200
pm.max_requests = 500000                              #进程累计请求回收值,会重新生成进程。
pm.status_path = /my_php_status                          #状态访问URL
ping.path = /ping                                  #ping访问的地址
ping.response = ping-pong                              #ping的返回值 
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.启动php

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# systemctl start php-fpm
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                127.0.0.1:9000                                                                   *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

4>.配置nginx转发php程序

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
  
http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti
me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location ~ .php$ {
        #"$document_root"会调用root目录,若不写root指令对应的目录路径,则"$document_root"所对应的值为空.
        root /yinzhengjie/data/web/php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #如果"SCRIPT_FILENAME"后面是绝对路径则可以省略上面的"root /data/nginx/php;"
        #fastcgi_param SCRIPT_FILENAME /yinzhengjie/data/web/php$fastcgi_script_name;
        #千万别忘记添加改行,若不添加"include fastcgi_params;"尽管上面配置的都正确,nginx也无法将fastcgi程序的处理结果返回给浏览器,返回给浏览器的只能是一个零字节的文件。
        include fastcgi_params;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

5>.准备php的测试页面

[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/php
mkdir: created directory ‘/yinzhengjie/data/web/php’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/web/php/index.php
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/php/index.php
<?php
    phpinfo();
?>
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

6>.启动Nginx

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                127.0.0.1:9000                                                                   *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                127.0.0.1:9000                                                                   *:*                  
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                        *:443                                                                    *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]#

7>.浏览器访问"http://node101.yinzhengjie.org.cn/index.php",效果如下图所示

三.Nginx与php-fpm不在同一服务器实战案例

1>.安装较新的php版本yum源

[root@node106.yinzhengjie.org.cn ~]# yum -y install wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                            | 9.1 kB  00:00:00     
 * base: mirrors.huaweicloud.com
 * epel: mirrors.yun-idc.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                                            | 3.6 kB  00:00:00     
extras                                                                                                          | 2.9 kB  00:00:00     
updates                                                                                                         | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                     Arch                          Version                                   Repository                   Size
=======================================================================================================================================
Installing:
 wget                        x86_64                        1.14-18.el7_6.1                           base                        547 k

Transaction Summary
=======================================================================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                 | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7_6.1.x86_64                                                                                         1/1 
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                                                         1/1 

Installed:
  wget.x86_64 0:1.14-18.el7_6.1                                                                                                        

Complete!
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# yum -y install wget
[root@node106.yinzhengjie.org.cn ~]# wget https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
--2019-12-29 08:43:29--  https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17908 (17K) [application/x-redhat-package-manager]
Saving to: ‘remi-release-7.rpm’

100%[=============================================================================================>] 17,908      --.-K/s   in 0s      

2019-12-29 08:43:29 (44.2 MB/s) - ‘remi-release-7.rpm’ saved [17908/17908]

[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# wget https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
[root@node106.yinzhengjie.org.cn ~]# ll
total 20
-rw-r--r-- 1 root root 17908 Sep  6 14:35 remi-release-7.rpm
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /etc/yum.repos.d/
total 0
drwxr-xr-x 2 root root 229 Dec 29 08:43 backup
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# rpm -ivh remi-release-7.rpm 
warning: remi-release-7.rpm: Header V4 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:remi-release-7.7-1.el7.remi      ################################# [100%]
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /etc/yum.repos.d/
total 52
drwxr-xr-x 2 root root  229 Dec 29 08:43 backup
-rw-r--r-- 1 root root  446 Sep  6 14:31 remi-glpi91.repo
-rw-r--r-- 1 root root  446 Sep  6 14:31 remi-glpi92.repo
-rw-r--r-- 1 root root  446 Sep  6 14:31 remi-glpi93.repo
-rw-r--r-- 1 root root  446 Sep  6 14:31 remi-glpi94.repo
-rw-r--r-- 1 root root  855 Sep  6 14:31 remi-modular.repo
-rw-r--r-- 1 root root  456 Sep  6 14:31 remi-php54.repo
-rw-r--r-- 1 root root 1314 Sep  6 14:31 remi-php70.repo
-rw-r--r-- 1 root root 1314 Sep  6 14:31 remi-php71.repo
-rw-r--r-- 1 root root 1314 Sep  6 14:31 remi-php72.repo
-rw-r--r-- 1 root root 1314 Sep  6 14:31 remi-php73.repo
-rw-r--r-- 1 root root 1314 Sep  6 14:31 remi-php74.repo
-rw-r--r-- 1 root root 2605 Sep  6 14:31 remi.repo
-rw-r--r-- 1 root root  750 Sep  6 14:31 remi-safe.repo
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

2>.根据公司正在使用的php版本安装相同版本即可

[root@node106.yinzhengjie.org.cn ~]# yum -y install php56-php-fpm php56-php-mysql
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * remi-safe: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package php56-php-fpm.x86_64 0:5.6.40-15.el7.remi will be installed
--> Processing Dependency: php56-php-common(x86-64) = 5.6.40-15.el7.remi for package: php56-php-fpm-5.6.40-15.el7.remi.x86_64
---> Package php56-php-mysqlnd.x86_64 0:5.6.40-15.el7.remi will be installed
--> Processing Dependency: php56-php-pdo(x86-64) = 5.6.40-15.el7.remi for package: php56-php-mysqlnd-5.6.40-15.el7.remi.x86_64
--> Running transaction check
---> Package php56-php-common.x86_64 0:5.6.40-15.el7.remi will be installed
--> Processing Dependency: php56-php-pecl-jsonc(x86-64) for package: php56-php-common-5.6.40-15.el7.remi.x86_64
--> Processing Dependency: php56-php-pecl-zip(x86-64) for package: php56-php-common-5.6.40-15.el7.remi.x86_64
--> Processing Dependency: php56-runtime for package: php56-php-common-5.6.40-15.el7.remi.x86_64
---> Package php56-php-pdo.x86_64 0:5.6.40-15.el7.remi will be installed
--> Running transaction check
---> Package php56-php-pecl-jsonc.x86_64 0:1.3.10-1.el7.remi will be installed
---> Package php56-php-pecl-zip.x86_64 0:1.15.5-1.el7.remi will be installed
--> Processing Dependency: libzip5(x86-64) >= 1.5.2 for package: php56-php-pecl-zip-1.15.5-1.el7.remi.x86_64
--> Processing Dependency: libzip.so.5()(64bit) for package: php56-php-pecl-zip-1.15.5-1.el7.remi.x86_64
---> Package php56-runtime.x86_64 0:3.0-1.el7.remi will be installed
--> Processing Dependency: /usr/sbin/semanage for package: php56-runtime-3.0-1.el7.remi.x86_64
--> Processing Dependency: environment-modules for package: php56-runtime-3.0-1.el7.remi.x86_64
--> Processing Dependency: scl-utils for package: php56-runtime-3.0-1.el7.remi.x86_64
--> Running transaction check
---> Package environment-modules.x86_64 0:3.2.10-10.el7 will be installed
--> Processing Dependency: libtcl8.5.so()(64bit) for package: environment-modules-3.2.10-10.el7.x86_64
---> Package libzip5.x86_64 0:1.5.2-1.el7.remi will be installed
---> Package policycoreutils-python.x86_64 0:2.5-33.el7 will be installed
--> Processing Dependency: policycoreutils = 2.5-33.el7 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libcgroup for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
---> Package scl-utils.x86_64 0:20130529-19.el7 will be installed
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
--> Processing Dependency: audit-libs(x86-64) = 2.8.5-4.el7 for package: audit-libs-python-2.8.5-4.el7.x86_64
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> Package policycoreutils.x86_64 0:2.5-29.el7 will be updated
---> Package policycoreutils.x86_64 0:2.5-33.el7 will be an update
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed
--> Running transaction check
---> Package audit-libs.x86_64 0:2.8.4-4.el7 will be updated
--> Processing Dependency: audit-libs(x86-64) = 2.8.4-4.el7 for package: audit-2.8.4-4.el7.x86_64
---> Package audit-libs.x86_64 0:2.8.5-4.el7 will be an update
--> Running transaction check
---> Package audit.x86_64 0:2.8.4-4.el7 will be updated
---> Package audit.x86_64 0:2.8.5-4.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                                 Arch                    Version                              Repository                  Size
=======================================================================================================================================
Installing:
 php56-php-fpm                           x86_64                  5.6.40-15.el7.remi                   remi-safe                  1.4 M
 php56-php-mysqlnd                       x86_64                  5.6.40-15.el7.remi                   remi-safe                  192 k
Installing for dependencies:
 audit-libs-python                       x86_64                  2.8.5-4.el7                          base                        76 k
 checkpolicy                             x86_64                  2.5-8.el7                            base                       295 k
 environment-modules                     x86_64                  3.2.10-10.el7                        base                       107 k
 libcgroup                               x86_64                  0.41-21.el7                          base                        66 k
 libsemanage-python                      x86_64                  2.5-14.el7                           base                       113 k
 libzip5                                 x86_64                  1.5.2-1.el7.remi                     remi-safe                   56 k
 php56-php-common                        x86_64                  5.6.40-15.el7.remi                   remi-safe                  698 k
 php56-php-pdo                           x86_64                  5.6.40-15.el7.remi                   remi-safe                  107 k
 php56-php-pecl-jsonc                    x86_64                  1.3.10-1.el7.remi                    remi-safe                   45 k
 php56-php-pecl-zip                      x86_64                  1.15.5-1.el7.remi                    remi-safe                   45 k
 php56-runtime                           x86_64                  3.0-1.el7.remi                       remi-safe                  1.1 M
 policycoreutils-python                  x86_64                  2.5-33.el7                           base                       457 k
 python-IPy                              noarch                  0.75-6.el7                           base                        32 k
 scl-utils                               x86_64                  20130529-19.el7                      base                        24 k
 setools-libs                            x86_64                  3.3.8-4.el7                          base                       620 k
 tcl                                     x86_64                  1:8.5.13-8.el7                       base                       1.9 M
Updating for dependencies:
 audit                                   x86_64                  2.8.5-4.el7                          base                       256 k
 audit-libs                              x86_64                  2.8.5-4.el7                          base                       102 k
 policycoreutils                         x86_64                  2.5-33.el7                           base                       916 k

Transaction Summary
=======================================================================================================================================
Install  2 Packages (+16 Dependent packages)
Upgrade             (  3 Dependent packages)

Total download size: 8.5 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/21): audit-libs-2.8.5-4.el7.x86_64.rpm                                                                       | 102 kB  00:00:00     
(2/21): audit-2.8.5-4.el7.x86_64.rpm                                                                            | 256 kB  00:00:00     
(3/21): audit-libs-python-2.8.5-4.el7.x86_64.rpm                                                                |  76 kB  00:00:00     
(4/21): environment-modules-3.2.10-10.el7.x86_64.rpm                                                            | 107 kB  00:00:00     
(5/21): checkpolicy-2.5-8.el7.x86_64.rpm                                                                        | 295 kB  00:00:00     
(6/21): libsemanage-python-2.5-14.el7.x86_64.rpm                                                                | 113 kB  00:00:00     
(7/21): libcgroup-0.41-21.el7.x86_64.rpm                                                                        |  66 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/remi-safe/packages/php56-php-common-5.6.40-15.el7.remi.x86_64.rpm: Header V4 DSA/SHA1 Signature, key I
D 00f97f56: NOKEYPublic key for php56-php-common-5.6.40-15.el7.remi.x86_64.rpm is not installed
(8/21): php56-php-common-5.6.40-15.el7.remi.x86_64.rpm                                                          | 698 kB  00:00:00     
(9/21): php56-php-pecl-jsonc-1.3.10-1.el7.remi.x86_64.rpm                                                       |  45 kB  00:00:00     
(10/21): php56-php-pecl-zip-1.15.5-1.el7.remi.x86_64.rpm                                                        |  45 kB  00:00:00     
(11/21): php56-runtime-3.0-1.el7.remi.x86_64.rpm                                                                | 1.1 MB  00:00:00     
(12/21): policycoreutils-2.5-33.el7.x86_64.rpm                                                                  | 916 kB  00:00:00     
(13/21): policycoreutils-python-2.5-33.el7.x86_64.rpm                                                           | 457 kB  00:00:00     
(14/21): python-IPy-0.75-6.el7.noarch.rpm                                                                       |  32 kB  00:00:00     
(15/21): scl-utils-20130529-19.el7.x86_64.rpm                                                                   |  24 kB  00:00:00     
(16/21): libzip5-1.5.2-1.el7.remi.x86_64.rpm                                                                    |  56 kB  00:00:01     
(17/21): setools-libs-3.3.8-4.el7.x86_64.rpm                                                                    | 620 kB  00:00:00     
(18/21): php56-php-pdo-5.6.40-15.el7.remi.x86_64.rpm                                                            | 107 kB  00:00:02     
(19/21): tcl-8.5.13-8.el7.x86_64.rpm                                                                            | 1.9 MB  00:00:00     
(20/21): php56-php-mysqlnd-5.6.40-15.el7.remi.x86_64.rpm                                                        | 192 kB  00:00:03     
(21/21): php56-php-fpm-5.6.40-15.el7.remi.x86_64.rpm                                                            | 1.4 MB  00:00:05     
---------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                  1.5 MB/s | 8.5 MB  00:00:05     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Importing GPG key 0x00F97F56:
 Userid     : "Remi Collet <RPMS@FamilleCollet.com>"
 Fingerprint: 1ee0 4cce 88a4 ae4a a29a 5df5 004e 6f47 00f9 7f56
 Package    : remi-release-7.7-1.el7.remi.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : audit-libs-2.8.5-4.el7.x86_64                                                                                      1/24 
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                                                                               2/24 
  Updating   : policycoreutils-2.5-33.el7.x86_64                                                                                  3/24 
  Installing : libcgroup-0.41-21.el7.x86_64                                                                                       4/24 
  Installing : checkpolicy-2.5-8.el7.x86_64                                                                                       5/24 
  Installing : libsemanage-python-2.5-14.el7.x86_64                                                                               6/24 
  Installing : setools-libs-3.3.8-4.el7.x86_64                                                                                    7/24 
  Installing : libzip5-1.5.2-1.el7.remi.x86_64                                                                                    8/24 
  Installing : python-IPy-0.75-6.el7.noarch                                                                                       9/24 
  Installing : policycoreutils-python-2.5-33.el7.x86_64                                                                          10/24 
  Installing : scl-utils-20130529-19.el7.x86_64                                                                                  11/24 
  Installing : 1:tcl-8.5.13-8.el7.x86_64                                                                                         12/24 
  Installing : environment-modules-3.2.10-10.el7.x86_64                                                                          13/24 
  Installing : php56-runtime-3.0-1.el7.remi.x86_64                                                                               14/24 
  Installing : php56-php-pecl-zip-1.15.5-1.el7.remi.x86_64                                                                       15/24 
  Installing : php56-php-common-5.6.40-15.el7.remi.x86_64                                                                        16/24 
  Installing : php56-php-pecl-jsonc-1.3.10-1.el7.remi.x86_64                                                                     17/24 
  Installing : php56-php-pdo-5.6.40-15.el7.remi.x86_64                                                                           18/24 
  Installing : php56-php-mysqlnd-5.6.40-15.el7.remi.x86_64                                                                       19/24 
  Installing : php56-php-fpm-5.6.40-15.el7.remi.x86_64                                                                           20/24 
  Updating   : audit-2.8.5-4.el7.x86_64                                                                                          21/24 
  Cleanup    : policycoreutils-2.5-29.el7.x86_64                                                                                 22/24 
  Cleanup    : audit-2.8.4-4.el7.x86_64                                                                                          23/24 
  Cleanup    : audit-libs-2.8.4-4.el7.x86_64                                                                                     24/24 
=====================================================================

  WARNING : PHP 5.6 have reached its "End of Life" in
  January 2019. Even, if this package includes some of
  the important security fix, backported from 7.2, the
  UPGRADE to a maintained version is very strongly RECOMMENDED.

=====================================================================
  Verifying  : 1:tcl-8.5.13-8.el7.x86_64                                                                                          1/24 
  Verifying  : php56-php-pecl-zip-1.15.5-1.el7.remi.x86_64                                                                        2/24 
  Verifying  : environment-modules-3.2.10-10.el7.x86_64                                                                           3/24 
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                                                                               4/24 
  Verifying  : php56-runtime-3.0-1.el7.remi.x86_64                                                                                5/24 
  Verifying  : php56-php-fpm-5.6.40-15.el7.remi.x86_64                                                                            6/24 
  Verifying  : scl-utils-20130529-19.el7.x86_64                                                                                   7/24 
  Verifying  : php56-php-pdo-5.6.40-15.el7.remi.x86_64                                                                            8/24 
  Verifying  : php56-php-common-5.6.40-15.el7.remi.x86_64                                                                         9/24 
  Verifying  : python-IPy-0.75-6.el7.noarch                                                                                      10/24 
  Verifying  : libzip5-1.5.2-1.el7.remi.x86_64                                                                                   11/24 
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                                   12/24 
  Verifying  : php56-php-mysqlnd-5.6.40-15.el7.remi.x86_64                                                                       13/24 
  Verifying  : policycoreutils-python-2.5-33.el7.x86_64                                                                          14/24 
  Verifying  : audit-2.8.5-4.el7.x86_64                                                                                          15/24 
  Verifying  : php56-php-pecl-jsonc-1.3.10-1.el7.remi.x86_64                                                                     16/24 
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                                                                              17/24 
  Verifying  : audit-libs-2.8.5-4.el7.x86_64                                                                                     18/24 
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                                                                      19/24 
  Verifying  : policycoreutils-2.5-33.el7.x86_64                                                                                 20/24 
  Verifying  : libcgroup-0.41-21.el7.x86_64                                                                                      21/24 
  Verifying  : audit-2.8.4-4.el7.x86_64                                                                                          22/24 
  Verifying  : audit-libs-2.8.4-4.el7.x86_64                                                                                     23/24 
  Verifying  : policycoreutils-2.5-29.el7.x86_64                                                                                 24/24 

Installed:
  php56-php-fpm.x86_64 0:5.6.40-15.el7.remi                        php56-php-mysqlnd.x86_64 0:5.6.40-15.el7.remi                       

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                              checkpolicy.x86_64 0:2.5-8.el7                                   
  environment-modules.x86_64 0:3.2.10-10.el7                          libcgroup.x86_64 0:0.41-21.el7                                   
  libsemanage-python.x86_64 0:2.5-14.el7                              libzip5.x86_64 0:1.5.2-1.el7.remi                                
  php56-php-common.x86_64 0:5.6.40-15.el7.remi                        php56-php-pdo.x86_64 0:5.6.40-15.el7.remi                        
  php56-php-pecl-jsonc.x86_64 0:1.3.10-1.el7.remi                     php56-php-pecl-zip.x86_64 0:1.15.5-1.el7.remi                    
  php56-runtime.x86_64 0:3.0-1.el7.remi                               policycoreutils-python.x86_64 0:2.5-33.el7                       
  python-IPy.noarch 0:0.75-6.el7                                      scl-utils.x86_64 0:20130529-19.el7                               
  setools-libs.x86_64 0:3.3.8-4.el7                                   tcl.x86_64 1:8.5.13-8.el7                                        

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7              audit-libs.x86_64 0:2.8.5-4.el7              policycoreutils.x86_64 0:2.5-33.el7             

Complete!
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# yum -y install php56-php-fpm php56-php-mysql

3>.php相关参数优化(优化参数的具体意思可以参考Nginx和PHP部署在同一台服务器的PHP调优笔记)

[root@node106.yinzhengjie.org.cn ~]# egrep  -v "^;|^$|^ " /opt/remi/php56/root/etc/php-fpm.d/www.conf        #这是清华源默认安装的php的存放PHP文件的路径,以下为默认配置 
[www]
user = apache
group = apache
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /opt/remi/php56/root/var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /opt/remi/php56/root/var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /opt/remi/php56/root/var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /opt/remi/php56/root/var/lib/php/wsdlcache
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# vim /opt/remi/php56/root/etc/php-fpm.d/www.conf 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# egrep  -v "^;|^$|^ " /opt/remi/php56/root/etc/php-fpm.d/www.conf           #需要根据实际情况进行调优
[www]
user = nginx                    #注意,使用nginx用户管理php程序那就得有nginx用户哟~
group = nginx
listen = 172.30.1.106:9000            #监听本地地址
listen.allowed_clients = 172.30.1.101      #允许访问的客户端IP地址
pm = dynamic
pm.max_children = 500
pm.start_servers = 100
pm.min_spare_servers = 100
pm.max_spare_servers = 200
pm.max_requests = 500000
pm.status_path = /my_php_status
ping.path = /ping
ping.response = ping-pong
slowlog = /opt/remi/php56/root/var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /opt/remi/php56/root/var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /opt/remi/php56/root/var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /opt/remi/php56/root/var/lib/php/wsdlcache
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

4>.创建nginx用户并启动php服务

[root@node106.yinzhengjie.org.cn ~]# useradd nginx -u 2000          #该用户的uid最好和Nginx服务器的uid要一致哟~
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# id nginx  
uid=2000(nginx) gid=2000(nginx) groups=2000(nginx)
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                                     *:22                                                  *:*                  
LISTEN      0      128                                    :::22                                                 :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# systemctl start php56-php-fpm      #启动php服务
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                          172.30.1.106:9000                                                *:*                  
LISTEN      0      128                                     *:22                                                  *:*                  
LISTEN      0      128                                    :::22                                                 :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

5>.PHP节点创建存放php目录

[root@node106.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/php              #该目录会在Nginx配置文件中指定
mkdir: created directory ‘/yinzhengjie’
mkdir: created directory ‘/yinzhengjie/data’
mkdir: created directory ‘/yinzhengjie/data/web’
mkdir: created directory ‘/yinzhengjie/data/web/php’
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/php/index.php             #编写PHP测试数据,也可以直接将Nginx节点的PHP相关文件拷贝到改目录即可
<?php
    phpinfo();
?>
[root@node106.yinzhengjie.org.cn ~]# 

6>.修改Nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location ~ .php$ {
        #"$document_root"会调用root目录,若不写root指令对应的目录路径,则"$document_root"所对应的值为空.
        #root /yinzhengjie/data/web/php;
        #指定PHP服务器地址
        fastcgi_pass 172.30.1.106:9000;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #如果"SCRIPT_FILENAME"后面是绝对路径则可以省略上面的"root /data/nginx/php;"
        fastcgi_param SCRIPT_FILENAME /yinzhengjie/data/web/php$fastcgi_script_name;
        #千万别忘记添加改行,若不添加"include fastcgi_params;"尽管上面配置的都正确,nginx也无法将fastcgi程序的处理结果返回给浏览器,返回给浏览器的只能是一个零字节的文件。
        include fastcgi_params;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

7>.重新加载Nginx的配置

[root@node101.yinzhengjie.org.cn ~]# rm -rf /yinzhengjie/data/web/php        #Nginx节点是可以删除之前存放较久的PHP文件,因为目前PHP目录压根不在Nginx上存放,而是在node106.yinzhengjie.org.cn中存放着。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload                  #重新加载Nginx程序
[root@node101.yinzhengjie.org.cn ~]# 

8>.浏览器访问"http://node101.yinzhengjie.org.cn/index.php",如下图所示,php版本为5.6的啦~

原文地址:https://www.cnblogs.com/yinzhengjie/p/12111179.html