php - nginx 工作正常,但 php7.2-fpm 不能正常工作

标签 php ubuntu nginx

我正在尝试在 ubuntu 18.04 上使用 nginx 运行 php7.2-fpm 来运行 Wordpress。 Nginx 工作正常,但 php 不工作。
日志文件:

2020/08/10 11:06:34 [error] 107906#107906: *1 open() "/usr/share/nginx/htmlunix:/run/php/php7.2-fpm.sock" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:09:57 [error] 108481#108481: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:20:13 [error] 109596#109596: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:22:35 [error] 110539#110539: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:22:36 [error] 110539#110539: *2 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:26:10 [error] 110817#110817: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:27:05 [error] 110926#110926: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
2020/08/10 11:30:43 [error] 110926#110926: *2 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /phpinfo.php HTTP/1.1", host: "example.com"
2020/08/10 11:38:20 [error] 111593#111593: *1 open() "/usr/share/nginx/html404" failed (2: No such file or directory), client: ###.###.###.###, server: example.com, request: "GET /index.php HTTP/1.1", host: "example.com"
我的 nginx 配置文件:
server {

    listen 80;
    charset UTF-8;
    server_name example.com www.example.com;
    
    location ~ \.php$ {
        try_files            $uri = 404;
        fastcgi_pass         unix:/run/php/php7.2-fpm.sock;
        fastcgi_index        index.php;
        include              fastcgi_params;
        fastcgi_read_timeout 300;
    }
    
    location / {
        root  /data/web;
        index index.html index.htm;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
对不起我的英语短。

最佳答案

您在 nginx 中有错误配置配置文件。
首先,您指定 rootindex location \ 中的指令部分。

location / {
        root  /data/web;
        index index.html index.htm;
}
该部分仅在所有其他部分不匹配时才有效,因为它具有短裤掩码长度(/ 是一个符号)
URL.php 结尾,如 phpinfo.phpindex.php将匹配到 location ~ \.php$部分:
location ~ \.php$ {
        try_files            $uri = 404;
        fastcgi_pass         unix:/run/php/php7.2-fpm.sock;
        fastcgi_index        index.php;
        include              fastcgi_params;
        fastcgi_read_timeout 300;
}
但是——没有rootindex指令(顺便说一句 index 是可选的,但在您的情况下首先是强制性的)。
有两种解决方案:
首先,指定rootindex location ~ \.php$ 中的指令部分:
location ~ \.php$ {
        try_files            $uri = 404;
        root                 /data/web;
        index                index.php index.html index.htm;
        fastcgi_pass         unix:/run/php/php7.2-fpm.sock;
        fastcgi_param        SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_index        index.php;
        include              fastcgi_params;
        fastcgi_read_timeout 300;
}
第二个是放rootindex在全局上下文中(在 server 内):
server {

    listen 80;
    charset UTF-8;
    server_name example.com www.example.com;
    root  /data/web;
    index index.php index.html index.htm;

    ...

}

UPD:还有 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;必须在 fastcgi_pass 之后添加指令
我强烈推荐你阅读 Official nginx.org "Begginers guide"更好地了解事物的运作方式。

关于php - nginx 工作正常,但 php7.2-fpm 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63339780/

相关文章:

linux - 使用 htaccess .. 所有 url 都必须使用 www 重定向到 https

Nginx 尝试代理传递到上游名称

Python Flask 用户登录重定向

php - 如何使用 PHP 发布到 Facebook 页面

php - 需要 ext-mongodb ^1.5.0 symfony

javascript - 在登录表单发布到 php 并由 php 验证登录详细信息后加载 div

ubuntu - 无法安装 phpmyadmin Ubuntu 19.10

PHP MySQL 将查询中的多行合并为具有 1 个相似列的一个结果

android - 在不同位置应用不同命令

nginx - Go:在模板中的 if 语句中使用环境变量