建立 PHP-FPM 使用一个 Unix Socket
vi /etc/php-fpm.d/www.conf
[...]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[...]
重启PHP-FPM
systemctl restart php-fpm.service
配置文件:
vi /etc/nginx/conf.d/default.conf
配置内容如下:
[...]
location ~ \.php$ {
root /usr/share/nginx/html;
;fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[...]
重启Nginx
为什么要用unix:/tmp/php-cgi.sock,
最主要的特征就是unix socket比tcp快,当网站流量大的时候,服务器的优化是分毫必争的.
当我们用php-fpm来管理我们的php启动时,按照如下的配置,就会自动生成/tmp/php-cgi.sock。在php-fpm.conf文件中设置
[www]
listen = /tmp/php-cgi.sock
修改nginx.conf为:
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-cgi.sock;
重启php-fpm和ngix
第一种配置:
/etc/sysconfig/spawn-fcgi
1 | FASTCGI_USER=nginx |
2 | FASTCGI_GROUP=nginx |
3 | SOCKET=/var/run/spawn-fcgi.sock |
4 | PIDFILE=/var/run/spawn-fcgi.pid |
5 | PHP5_SOCKET=/var/run/php-fcgi.sock |
6 | CHILDREN=6 |
7 | PHP5=/usr/bin/php-cgi |
8 | MODE=0600 |
9 | OPTIONS= "-s $PHP5_SOCKET -S -M $MODE -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5" |
1 | chkconfig spawn-fcgi on |
2 | service spawn-fcgi start |
1 | location ~ \.php$ { |
2 | root html; |
3 | #fastcgi_pass 127.0.0.1:9000; |
4 | fastcgi_pass unix:/var/run/php-fcgi.sock; |
5 | fastcgi_index index.php; |
6 | fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; |
7 | include fastcgi_params; |
8 | } |
1 | vi /etc/rc.local |
2 | /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid |
1 | location ~ \.php$ { |
2 | root html; |
3 | fastcgi_pass 127.0.0.1:9000; |
4 | #fastcgi_pass unix:/var/run/php-fcgi.sock; |
5 | fastcgi_index index.php; |
6 | fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; |
7 | include fastcgi_params; |
8 | } |
一个是fastcgi_pass unix:/var/run/php-fcgi.sock;
一个是fastcgi_pass 127.0.0.1:9000;