[已解决]nginx报错connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory)

Centos8配置Nginx + PHP环境,启动后访问php文件报错:

2023/10/29 17:10:20 [crit] 879#0: *16 connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) while connecting to upstream, client: 172.17.0.1, server: _, request: “GET /test-php.php HTTP/1.1”, upstream: “fastcgi://unix:/run/php-fpm/www.sock:”, host: “127.0.0.1:10088”

看得出是php-fpm的问题,通常是套接字没配置好。

笔者解决方法最终是,统一用127.0.0.1:9000配置,配置文件路径:/etc/nginx/conf.d/php-fpm.conf,如图:

具体php和nginx如何搭配,见下文。

ps:好久没碰nginx了,感觉与php连通配置位置又变了,反正换汤不换药,顺手记录下详细排查过程。

第一步:首先要确保php-fpm已正常启动:

$ sudo systemctl start php-fpm

第二步:php-fpm和nginx连通,配置有两套

A:php配置文件 /etc/php-fpm.d/www.conf

一共改三行:

listen = /run/php-fpm/www.sock

user = nginx
group = nginx

nginx配置文件 /etc/nginx/nginx.conf

fastcgi_pass   unix:/run/php-fpm/www.sock;

B: php配置文件 /etc/php-fpm.d/www.conf

listen = 127.0.0.1

user = nginx
group = nginx

nginx配置文件 /etc/nginx/nginx.conf

fastcgi_pass   127.0.0.1;

参考资料

  1. https://www.cnblogs.com/adhzl/p/11808922.html
  2. https://www.5axxw.com/questions/simple/1ggrov