I'm attempting to serve PHP with nginx, i've followed this tutorial successfuly before but on a new server for some reason I get the following error:
nginx: [emerg] open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory)
In fact, the whole snippets
directory of the nginx installation is missing.
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
I've installed the most up to date nginx that is available - and yet the directory and file is still not present.
How can this be remedied?
Bonus: What could have caused this?
PHP-FPM is installed and active for NGINX. And that's it, you've got NGINX up and running with PHP-FPM support. Remember, when you build your virtualhost configuration files, you'll need to make sure to include PHP support in those. For that, you can use the /etc/nginx/sites-available/default file as an example.
FastCGI is a protocol based on the earlier CGI, or common gateway interface, protocol meant to improve performance by not running each request as a separate process. It is used to efficiently interface with a server that processes requests for dynamic content.
PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script.
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.
For Nate's answer, nginx-full
will install 1.10.3 for you.
I'm using Nginx 1.12.2 on Ubuntu 16.04, with this version, it doesn't have sites-enabled
and sites-available
with it; and it also has a different PHP CGI setup.
You can either use Ulad Kasach's solution, or start to use the new way.
Here's an official doc for how to do it: https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
BTW, in above post, you should also replace fastcgi.conf
with fastcgi_params
.
And add one more line which is in default orignially:
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
These are all new changes with 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;
}
Ended up having to look at a previous, working, configurations file and replicating it manually. Simply made the snippets
directory and added a fastcgi-php.conf
file with the following content:
# 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;
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With