我正在尝试在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
}
对不起,我的英文简短。
答案 0 :(得分:0)
您在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",以更好地了解事物的工作原理。