1、修改nginx配置文件
$ vim /usr/local/etc/nginx/servers/admin.share.com.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/usr/local/var/run/php-fpm/php72-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
2、修改php-fpm配置文件
$ vim /usr/local/etc/php/7.2/php-fpm.d/www.conf
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = _www
listen.group = _www
listen.mode = 0660
出现的异常:
nginx 502错误 failed (13: Permission denied)
安装好nginx和php-fpm后出现502的错误 查找原因是说php-fpm没有启动,查看nginx日志后 发现以下错误
2018/03/19 18:37:44 [crit] 12216#0: *22 connect() to unix:/usr/local/var/run/php-fpm/php72-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0 .0.1, server: admin.share.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://unix:/usr/local/var/run/php-fpm/php72-fpm.sock:", host: "admin.share.com"
解决办法:
$ chmod 0660 /usr/local/var/run/php-fpm/php72-fpm.sock
or
$ chmod 0660 /usr/local/var/run/php-fpm/php72-fpm.sock
$ chown _www:_www /usr/local/var/run/php-fpm/php72-fpm.sock
重启php-fpm
注: nginx运行的用户(nginx.conf)要和php-fpm运行的用户一样(www.conf),一般报没有权限都是用户不一致导致的,因为php-fpm是由nginx去执行的,所以要以nginx运行的用户去运行php-fpm才有权限。
两种通信方式的分析和总结
从原理上来说,unix socket方式肯定要比tcp的方式快而且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还要申请临时端口和tcp相关资源。
unix socket会显得不是那么稳定,当并发连接数爆发时,会产生大量的长时缓存,在没有面向连接协议支撑的情况下,大数据包很有可能就直接出错并不会返回异常。而TCP这样的面向连接的协议,多少可以保证通信的正确性和完整性。