我正在尝试在 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
中有错误配置配置文件。
首先,您指定 root
和 index
location \
中的指令部分。
location / {
root /data/web;
index index.html index.htm;
}
该部分仅在所有其他部分不匹配时才有效,因为它具有短裤掩码长度(/
是一个符号)每
URL
以 .php
结尾,如 phpinfo.php
或 index.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;
}
但是——没有root
和 index
指令(顺便说一句 index
是可选的,但在您的情况下首先是强制性的)。有两种解决方案:
首先,指定
root
和 index
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;
}
第二个是放root
和 index
在全局上下文中(在 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/