缺少 Nginx 的 fastcgi-php.conf 片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43262435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:48:23  来源:igfitidea点击:

Nginx's fastcgi-php.conf snippet is missing

phpubuntunginx

提问by Ulad Kasach

I'm attempting to serve PHP with nginx, i've followed this tutorialsuccessfuly before but on a new server for some reason I get the following error:

我正在尝试使用 nginx 为 PHP 提供服务,我之前成功地遵循了本教程,但由于某种原因在新服务器上出现以下错误:

 nginx: [emerg] open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory) 

In fact, the whole snippetsdirectory of the nginx installation is missing.

其实snippetsnginx安装的整个目录都不见了。

I've installed PHP with the following commands:
- sudo apt-get install -y php7.0-cli php7.0-cgi php-fpm php-mysql
- sudo systemctl restart php7.0-fpm

我已经安装了PHP用下面的命令:
- sudo apt-get install -y php7.0-cli php7.0-cgi php-fpm php-mysql
-sudo systemctl restart php7.0-fpm

I've installed the most up to date nginx that is available - and yet the directory and file is still not present.

我已经安装了可用的最新 nginx - 但目录和文件仍然不存在。

How can this be remedied?

如何补救?

Bonus: What could have caused this?

奖励:是什么导致了这种情况?

回答by Yang_2333

TLDR:

域名注册地址:

Final version is:

最终版本是:

location ~ \.php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi_params;                
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}


i think it depends on the Nginx version you are using.

我认为这取决于您使用的 Nginx 版本。

For Nate's answer, nginx-fullwill install 1.10.3 for you.

对于 Nate 的回答,nginx-full将为您安装 1.10.3。

I'm using Nginx 1.12.2 on Ubuntu 16.04, with this version, it doesn't have sites-enabledand sites-availablewith it; and it also has a different PHP CGI setup.

我在 Ubuntu 16.04 上使用 Nginx 1.12.2,在这个版本中,它没有sites-enabledsites-available它;它还有一个不同的 PHP CGI 设置。

You can either use Ulad Kasach's solution, or start to use the new way.

您可以使用 Ulad Kasach 的解决方案,也可以开始使用新方法。

Here's an official doc for how to do it: https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

这是有关如何执行此操作的官方文档:https: //www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

BTW, in above post, you should also replace fastcgi.confwith fastcgi_params.

顺便说一句,在上面的帖子中,您还应该替换fastcgi.conffastcgi_params.

And add one more line which is in default orignially:

再添加一行,这是默认的:

fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

These are all new changes with Nginx 1.12.2 :(

这些都是 Nginx 1.12.2 的新变化:(

Final version is:

最终版本是:

location ~ \.php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi_params;                
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}

回答by Ulad Kasach

Ended up having to look at a previous, working, configurations file and replicating it manually. Simply made the snippetsdirectory and added a fastcgi-php.conffile with the following content:

最终不得不查看以前的工作配置文件并手动复制它。只需创建snippets目录并添加一个fastcgi-php.conf包含以下内容的文件:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

You'll also need to replace the last line, include fastcgi.conf;with include fastcgi_params;.

您还需要更换的最后一行,include fastcgi.conf;include fastcgi_params;

I would have recommended to create the file fastcgi.conf;if it was not literally the same file with the additional line fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;in the case of fastcgi.conf

fastcgi.conf;如果fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;在以下情况下它与附加行不是同一个文件,我会建议创建该文件fastcgi.conf

回答by Nate

Had me stumped for a moment too. You want to install the nginx-fullpackage on ubuntu as opposed to just nginx. nginx-fullcontains the bits you were missing.

让我也难倒了一会儿。您想nginx-full在 ubuntu上安装该软件包,而不仅仅是nginx. nginx-full包含您丢失的位。

回答by insCode

i am using

我在用

  1. nginx/1.4.6
  2. Ubuntu 14.04.5 LTS
  1. nginx/1.4.6
  2. Ubuntu 14.04.5 LTS

and my site config looks like this

我的网站配置看起来像这样

server {
    listen 3010;

    root /usr/share/nginx/docs;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;   

    server_name phpSetup;


    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

and working nicely . so i do recommend to update your config include snippets/fastcgi-php.conf;to

并且工作得很好。所以我建议更新您的配置, 包括代码段/fastcgi-php.conf;

include fastcgi_params;

包括 fastcgi_params;

so the location block is

所以位置块是

location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

HOPE IT HELPS

希望能帮助到你

read more on : https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

阅读更多内容:https: //www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

thanks,

谢谢,