在编译php-fpm时,若没有指定fpm用户,在配置文件中也没有指定用户,则sock文件会由root(启动php-fpm的用户)创建,其权限是srw-rw----

而nginx一般由nginx用户启动,会导致无法读取sock文件,造成nginx返回502错误。

nginx日志会记录错误如下:

[crit] 1663#0: *40 connect() to unix:/dev/shm/php-fpm.sock failed (13: Permission denied) while connecting to upstream

解决办法是在配置文件中指定listen用户。

; 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 0666
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

改完后重启php-fpm即可。