lnmp使用socket方式串連nginx最佳化php-fpm效能

來源:互聯網
上載者:User
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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.