lnmp使用socket方式连接nginx优化php-fpm性能

lnmp使用socket方式连接nginx优化php-fpm性能


Nginx连接fastcgi的方式有2种:TCP和unix domain socket


什么是Unix domain socket?—— 维基百科
Unix domain socket 或者 IPC socket是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。与管道相比,Unix domain sockets 既可以使用字节流和数据队列,而管道通信则只能通过字节流。Unix domain sockets的接口和Internet socket很像,但它不使用网络底层协议来通信。Unix domain socket 的功能是POSIX操作系统里的一种组件。


Unix domain sockets 使用系统文件的地址来作为自己的身份。它可以被系统进程引用。所以两个进程可以同时打开一个Unix domain sockets来进行通信。不过这种通信方式是发生在系统内核里而不会在网络里传播。


将tcp改成socket方式的配置方法:
1、修改php-fpm的配置文件(如果是编译安装可能是/etc/php-fpm,视具体情况而定)


vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /dev/shm/php-cgi.sock


2、修改.sock文件权限(修改为运行php-fpm的用户)
方法:
ps -ef|grep php-fpm
root      6940     1  0 17:03 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx     6941  6940  0 17:03 ?        00:00:00 php-fpm: pool www
nginx     6942  6940  0 17:03 ?        00:00:00 php-fpm: pool www
nginx     6943  6940  0 17:03 ?        00:00:00 php-fpm: pool www
nginx     6944  6940  0 17:03 ?        00:00:00 php-fpm: pool www
nginx     6945  6940  0 17:03 ?        00:00:00 php-fpm: pool www


修改用户权限
chown nginx:nginx /dev/shm/php-cgi.sock
其中nginx是php-fpm.conf里面设置的用户和群组


ll /dev/shm/php-cgi.sock 
srw-rw-rw- 1 nginx nginx 0 Oct  8 17:03 /dev/shm/php-cgi.sock


3、修改nginx配置文件
vim /etc/nginx/nginx.conf


将 fastcgi_pass   127.0.0.1:9000;
改为


fastcgi_pass  unix:/dev/shm/php-cgi.sock;


具体:
location ~ \.php$ {
try_files $uri = 404;
#fastcgi_pass   127.0.0.1:9000;
fastcgi_pass  unix:/dev/shm/php-cgi.sock;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}


4、重启nginx和php-fpm
systemctl restart php-fpm
systemctl restart nginx


经过测试,php应用正常使用


参考链接:
https://blog.linuxeye.com/364.html
http://blog.csdn.net/niao_ye/article/details/39666695
http://yangjunwei.com/a/1760.html
http://blog.csdn.net/liv2005/article/details/7741732
http://www.cnxct.com/default-configuration-and-performance-of-nginx-phpfpm-and-tcp-socket-or-unix-domain-socket/
posted @ 2016-10-27 14:59  reblue520  阅读(463)  评论(0编辑  收藏  举报