进阶——使用Unix Domain Socket优化Nginx+php-fpm性能

First of all!

0.未配置Nginx+php-fpm的请移步至https://blog.silversky.moe/rt/centos-7-nginx-php-fpm-high-performance-web-server

1.自备Google  //当然不仅仅是Google Search,还有Google Translate。

2.既然是进阶,要有些常识之类的哦~

3.欢迎挑刺,下面评论区。

============================分割线============================

From Wikipedia:

Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. Like named pipes, Unix domain sockets support transmission of a reliable stream of bytes (SOCK_STREAM, compare to TCP). In addition, they support ordered and reliable transmission of datagrams (SOCK_SEQPACKET), or unordered and unreliable transmission of datagrams (SOCK_DGRAM, compare to UDP). The Unix domain socket facility is a standard component of POSIXoperating systems.

The API for Unix domain sockets is similar to that of an Internet socket, but rather than using an underlying network protocol, all communication occurs entirely within the operating system kernel. Unix domain sockets use the file system as their address name space. Processes reference Unix domain sockets as file system inodes, so two processes can communicate by opening the same socket.

In addition to sending data, processes may send file descriptors across a Unix domain socket connection using the sendmsg() and recvmsg() system calls. This allows the sending processes to grant the receiving process access to a file descriptor for which the receiving process otherwise does not have access.[1] This can be used to implement a rudimentary form of capability-based security.[2] For example, this allows the Clam AntiVirus scanner to run as an unprivilegeddaemon on Linux and BSD, yet still read any file sent to the daemon’s Unix domain socket.

Original Wikipedia URL: Unix domain socket

============================分割线============================

那么上面就是Wiki的介绍,不知道大家看懂了没有=。=

接下来就开始实践咯~

0.cd到 /dev/shm目录

# cd /dev/shm

1.创建php-cgi.sock文件 //其实这里取什么名字都可以,后面都改成你取的名字就好

# touch php-cgi.sock

2.设置文件所有者 //这里要将用户名和用户组换为你运行php-fpm的用户名及用户组,这里用www-data举例

# chown www-data:www-data php-cgi.sock

3.设置文件权限

# chmod 777 php-cgi.sock

4.修改Nginx网站配置文件 //之前教程里面讲到的版本是在 /etc/nginx/conf.d里面,主配置文件(nginx.conf)不要动

fastcgi_pass    127.0.0.1:9000;

修改为

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

5.修改php-fpm的pool配置文件 //默认是www.conf在/etc/php-fpm.d目录下

listen = 127.0.0.1:9000

修改为 //或者将上面那行注释掉,再添加下面这一行

listen = /dev/shm/php-cgi.sock

6.重启Nginx和php-fpm看看效果吧~

# service nginx restart

# service php-fpm restart

封面原图地址:http://icarus.silversky.moe:666/illustration/34317