缺少Nginx的fastcgi-php.conf片段

Ula*_*ach 7 php ubuntu nginx

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

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

实际上,snippets缺少nginx安装的整个目录.

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

我已经安装了最新的nginx,但目录和文件仍然不存在.

如何解决这个问题?

奖金:可能是什么导致了这个?

Yan*_*333 19

我认为这取决于你使用的Nginx版本.

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

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

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

以下是如何操作的官方文档:https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

顺便说一句,在上面的帖子,你也应该更换fastcgi.conf使用fastcgi_params.

并添加一个默认为orignially的行:

fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

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

最终版本是:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • HTML工作,PHP不是,我到处看,这一个为我工作! (3认同)

Ula*_*ach 5

最终不得不查看先前的有效配置文件并手动复制它。只需创建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;
Run Code Online (Sandbox Code Playgroud)

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

我建议创建一个文件,fastcgi.conf;如果它实际上不是同一文件,则fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;在以下情况下需要附加一行:fastcgi.conf


Nat*_*ate 5

我也难过了。您想将nginx-full软件包安装在ubuntu上,而不是just上nginxnginx-full包含您丢失的位。


小智 5

我在用

  1. nginx/1.4.6
  2. Ubuntu 14.04.5 LTS

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

包括 fastcgi_params;

所以位置块是

location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

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

谢谢,