RHEL/Centos7.x Nginx FastCGI with php

本文介绍如何配置Nginx与PHP-FPM服务,包括安装过程、配置文件详解及通过TCP与Unix套接字进行通信的方法。此外,还提供了解决权限问题的方案,并演示了如何测试配置。

To build Nginx, refer to 

https://blog.youkuaiyun.com/hushui/article/details/79711432

https://blog.youkuaiyun.com/hushui/article/details/79638741

 #Default path

/etc/nginx/nginx.conf    ---> Configuration 

/var/log/nginx    ---> Log folder 
 /etc/nginx/html   --->  ROOT of web page

 

## Start nginx server 

lake@localhost:~/Cloud/nginx/nginx-1.13.9$ sudo /usr/sbin/nginx  

 

## Install php-fpm 

  RHEL7.2/php  5.4.16-36


Note /php-fpm-5.4.16-36.el7_1.x86_64.rpm is not in Default RPM release, but come from Engineer Release repo. 


[root@localhost Packages]# rpm -ivh ~lake/Cloud/php/php-
php-fpm-5.4.16-36.el7_1.x86_64.rpm       php-mbstring-5.4.16-36.el7_1.x86_64.rpm  
[root@localhost Packages]# rpm -ivh ~lake/Cloud/php/php-fpm-5.4.16-36.el7_1.x86_64.rpm 
warning: /home/lake/Cloud/php/php-fpm-5.4.16-36.el7_1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:php-fpm-5.4.16-36.el7_1          ################################# [100%]
[root@localhost Packages]# rpm -ql  php-fpm-5.4.16-36.el7_1.x86_64
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/php-fpm.d/www.conf
/etc/sysconfig/php-fpm
/run/php-fpm
/usr/lib/systemd/system/php-fpm.service
/usr/lib/tmpfiles.d/php-fpm.conf
/usr/sbin/php-fpm
/usr/share/doc/php-fpm-5.4.16
/usr/share/doc/php-fpm-5.4.16/fpm_LICENSE
/usr/share/doc/php-fpm-5.4.16/php-fpm.conf.default
/usr/share/fpm
/usr/share/fpm/status.html
/usr/share/man/man8/php-fpm.8.gz
/var/log/php-fpm
[root@localhost Packages]#  


[root@localhost Packages]#    systemctl enable     php-fpm.service  
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost Packages]#    systemctl  start      php-fpm.service  
[root@localhost Packages]# 

 
[root@localhost Packages]#    systemctl  status       php-fpm.service  
\u25cf php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-03-27 03:04:16 EDT; 1min 5s ago
 Main PID: 9289 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           \u251c\u25009289 php-fpm: master process (/etc/php-fpm.conf)
           \u251c\u25009296 php-fpm: pool www
           \u251c\u25009297 php-fpm: pool www
           \u251c\u25009298 php-fpm: pool www
           \u251c\u25009299 php-fpm: pool www
           \u2514\u25009300 php-fpm: pool www


Mar 27 03:04:16 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...
Mar 27 03:04:16 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
 

######Default configuration of php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d/www.conf
pid   /var/run/php-fpm/php-fpm.pid 
Unix sock: /var/run/php5-fpm.sock    depending on /etc/nginx/nginx.conf


lake@localhost:/etc/nginx/html$ ls -l  /var/run/php-fpm/php-fpm.pid 
-rw-r--r-- 1 root root 4 Mar 27 03:04 /var/run/php-fpm/php-fpm.pid
lake@localhost:/etc/nginx/html$ ls -l  /var/run/php5-fpm.sock  
ls: cannot access /var/run/php5-fpm.sock: No such file or directory
lake@localhost:/etc/nginx/html$ netstat -an --tcp  |grep  9000 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN     
 
lake@localhost:/etc/nginx/html$ grep 9000     /etc/php-fpm.d/www.conf 
listen = 127.0.0.1:9000
[root@localhost Packages]#  lsof -i:9000
COMMAND  PID   USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
php-fpm 9289   root    6u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 9296 apache    0u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 9297 apache    0u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 9298 apache    0u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 9299 apache    0u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 9300 apache    0u  IPv4 31315175      0t0  TCP localhost:cslistener (LISTEN)

############ Permission issue!! 

1. Nginx user: 
root@localhost # lsof -i:80
COMMAND   PID  USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nginx   29643  root    6u  IPv4 31201664      0t0  TCP *:http (LISTEN)
nginx   29644 nginx    6u  IPv4 31201664      0t0  TCP *:http (LISTEN)
root@localhost # ps -aux |grep nginx 
root     29643  0.0  0.0  23452   904 ?        Ss   00:51   0:00 nginx: master process /usr/sbin/nginx
nginx    29644  0.0  0.0  25996  1924 ?        S    00:51   0:00 nginx: worker process
root     31001  0.0  0.0 112648   956 pts/8    S+   00:58   0:00 grep --color=auto nginx
 
2. php-fpm user: apache 

[root@localhost Packages]# ps -aux  |grep php-fpm 
root      9289  0.0  0.0 330624 10412 ?        Ss   03:04   0:00 php-fpm: master process (/etc/php-fpm.conf)
apache    9296  0.0  0.0 332708  5612 ?        S    03:04   0:00 php-fpm: pool www
apache    9297  0.0  0.0 332708  5612 ?        S    03:04   0:00 php-fpm: pool www
apache    9298  0.0  0.0 332708  5608 ?        S    03:04   0:00 php-fpm: pool www
apache    9299  0.0  0.0 332708  5044 ?        S    03:04   0:00 php-fpm: pool www
apache    9300  0.0  0.0 332708  5044 ?        S    03:04   0:00 php-fpm: pool www
root     12401  0.0  0.0 112644   952 pts/8    S+   03:46   0:00 grep --color=auto php
 
 
 

### make sure nginx/php-fpm and /etc/nginx/html are in same user/group!!!!


/etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx       -------> RHEL/RPM default is apache, change to nginx 
; RPM: Keep a group allowed to write in log dir.
group = nginx     


############ Restart php-fpm service with new user/nginx 


[root@localhost Packages]#  systemctl  restart    php-fpm.service 
[root@localhost Packages]#  lsof -i:9000 
COMMAND   PID  USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
php-fpm 15777  root    6u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 15784 nginx    0u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 15785 nginx    0u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 15787 nginx    0u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 15788 nginx    0u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 15789 nginx    0u  IPv4 31378888      0t0  TCP localhost:cslistener (LISTEN)

 

 

 

 

 

### Test phpinfo in Nginx default  php-fpm/TCP:9000

 /etc/nginx/nginx.conf  


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  ### default /scripts path is wrong 
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  ###  $request_filename is also OK!!!
            include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 /etc/nginx/nginx.conf 
### Restart nginx web server with new configuration 
lake@localhost:/etc/nginx/html$ sudo /usr/sbin/nginx   -s reload


[root@localhost Packages]#  lsof -i:80  ### make sure nginx/php-fpm and /etc/nginx/html are in same user/group!!!!
COMMAND   PID  USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
firefox   301  lake   55u  IPv4 31393165      0t0  TCP localhost:51949->localhost:http (ESTABLISHED)
nginx   16981  root    6u  IPv4 31385329      0t0  TCP *:http (LISTEN)
nginx   17390 nginx    4u  IPv4 31379177      0t0  TCP localhost:http->localhost:51949 (ESTABLISHED)
nginx   17390 nginx    6u  IPv4 31385329      0t0  TCP *:http (LISTEN)
[root@localhost Packages]# 




############################################################

 

 

 

 

 

 

Change to Unix socket for communication between Nginx and FastCGI/php-fpm

######Default configuration 
/etc/nginx/nginx.conf
/etc/php-fpm.conf
/etc/php-fpm.d/www.conf 
Unix sock: /var/run/php5-fpm.sock    depending on /etc/nginx/nginx.conf


############################################################
lake@localhost:/etc/nginx/html$ netstat -an -lx   |grep php 


lake@localhost:/etc/nginx/html$   lsof -U  |grep php 
lake@localhost:/etc/nginx/html$ ls -l  /var/run/php5-fpm.sock  
ls: cannot access /var/run/php5-fpm.sock: No such file or directory
lake@localhost:/etc/nginx/html$ 


/etc/nginx/nginx.conf  


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
    ###   fastcgi_pass   127.0.0.1:9000;
             fastcgi_pass   unix:/var/run/php5-fpm.sock;    ### use Unix socket 


            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  ### default /scripts path is wrong 
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  ###  $request_filename is also OK!!!
            include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 /etc/nginx/nginx.conf 
 
 /etc/php-fpm.d/www.conf 
...
;listen = 127.0.0.1:9000
listen = /var/run/php5-fpm.sock
 /etc/php-fpm.d/www.conf 


############ Restart php-fpm and nginx with Unix socket for FastCGI/php-fpm
lake@localhost:/etc/nginx/html$  systemctl  restart    php-fpm.service  
lake@localhost:/etc/nginx/html$ netstat -an -lx   |grep php  
unix  2      [ ACC ]     STREAM     LISTENING     31415354 /var/run/php5-fpm.sock
lake@localhost:/etc/nginx/html$ ls -l  /var/run/php5-fpm.sock  
srw-rw-rw- 1 root root 0 Mar 27 05:03 /var/run/php5-fpm.sock
lake@localhost:/etc/nginx/html$ 
lake@localhost:/etc/nginx/html$ ls -l  /var/run/php5-fpm.sock  
srw-rw-rw- 1 root root 0 Mar 27 05:03 /var/run/php5-fpm.sock
lake@localhost:/etc/nginx/html$ 


lake@localhost:/etc/nginx/html$ sudo /usr/sbin/nginx   -s reload 

 

 

 

 

### Test from  Unix socket between Nginx and Php-fpm


lake@localhost:/etc/nginx/html$ cat index.php 
echo "<?php echo "the 1st test page \r\n"; phpinfo(); ?>" 
lake@localhost:/etc/nginx/html$ 


Browser view phpinfo    
 
PHP Version 5.4.16


System Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64
Build Date Jun 23 2015 21:19:01
Server API FPM/FastCGI  *******************
.................
.......
lake@localhost:/etc/nginx/html$ sudo   lsof -U     |grep nginx 
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
nginx     16981           root    4u  unix 0xffff8808014c5280      0t0 31405136 socket
nginx     16981           root    5u  unix 0xffff8808014c7080      0t0 31405137 socket
php-fpm   19623          nginx    0u  unix 0xffff880801e41a40      0t0 31415354 /var/run/php5-fpm.sock
php-fpm   19623          nginx    7u  unix 0xffff880801e421c0      0t0 31415355 socket
php-fpm   19624          nginx    0u  unix 0xffff880801e41a40      0t0 31415354 /var/run/php5-fpm.sock
php-fpm   19624          nginx    7u  unix 0xffff880801e421c0      0t0 31415355 socket
php-fpm   19625          nginx    0u  unix 0xffff880801e41a40      0t0 31415354 /var/run/php5-fpm.sock
php-fpm   19625          nginx    7u  unix 0xffff880801e421c0      0t0 31415355 socket
php-fpm   19626          nginx    0u  unix 0xffff880801e41a40      0t0 31415354 /var/run/php5-fpm.sock
php-fpm   19626          nginx    7u  unix 0xffff880801e421c0      0t0 31415355 socket
php-fpm   19627          nginx    0u  unix 0xffff880801e41a40      0t0 31415354 /var/run/php5-fpm.sock
php-fpm   19627          nginx    7u  unix 0xffff880801e421c0      0t0 31415355 socket
nginx     19786          nginx    5u  unix 0xffff8808014c7080      0t0 31405137 socket

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值