LNMP use socket to connect Nginx optimize PHP-FPM performance

Source: Internet
Author: User
Tags fpm unix domain socket file permissions
LNMP use socket to connect Nginx optimize PHP-FPM performance


There are 2 ways to connect fastcgi nginx: TCP and UNIX domain sockets


What is a UNIX domain socket. --Wikipedia
A Unix domain socket or an IPC socket is a terminal that enables data communication between two or more processes on the same operating system. Compared to pipelines, Unix domain sockets can use both byte streams and data queues, while pipe traffic can only be accessed by byte streams. The Unix domain sockets interface is similar to the Internet socket, but it does not communicate using the network underlying protocol. The Unix domain socket function is a component of the POSIX operating system.


Unix domain sockets uses the address of the system file as its own identity. It can be referenced by the system process. So two processes can open a UNIX domain sockets to communicate at the same time. However, this mode of communication occurs in the kernel of the system and does not spread through the network.


Configuration method to change TCP to socket mode:
1, modify the PHP-FPM configuration file (if it is compiled installation may be/etc/php-fpm, depending on the specific circumstances)


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


2. Modify the. sock file permissions (modify to the user who is running PHP-FPM)
Method:
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


Modify User Rights
Chown Nginx:nginx/dev/shm/php-cgi.sock
Where Nginx is the user and group set within the 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, modify the Nginx configuration file
Vim/etc/nginx/nginx.conf


Will Fastcgi_pass 127.0.0.1:9000;
To


Fastcgi_pass Unix:/dev/shm/php-cgi.sock;


Specific:
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, restart Nginx and PHP-FPM
Systemctl Restart PHP-FPM
Systemctl Restart Nginx


After testing, PHP application normal use


Reference Links:
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.