zoukankan      html  css  js  c++  java
  • 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/
  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239682.html
Copyright © 2011-2022 走看看