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?
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
Had me stumped for a moment too. You want to install the nginx-full package on ubuntu as opposed to just nginx. nginx-full contains the bits you were missing.
i am using
nginx/1.4.6
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
include 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/
thanks,
Related
I'm new to nginx and want to configure it so the user can access URLs like
http://[ip_address]/dev/index.php/customer/account/login/
I think this may be related to using FastCGI to process the request and pass it to Magento. However, whenever I access it, i see a 404 message. I can confirm that the user running nginx and owning the directory and files is www-data. So it has access to it. I need help configuring nginx & FastCGI properly so the request loads the correct page.
All of my application is in dev/ folder. Here's the relevant chunk of default file in /etc/nginx/sites-available/defaul:
root /var/www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _ test.xxx.com;
location /dev/ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php$is_args$args;
}
location /dev/app/ {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Here is my nginx config. May help you.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 900;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
I know there is so much documentation about this but i can't understand where is the problem.
I have fresh installation of Debian 8 and the first think I install is Nginx fastcgi.
Nginx is working but it can't execute any php file.
i have tried some combination of section location ~ .php$ of nginx configuration file but when i try to load .php file from beowser it's just downloaded. There is the last try;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
#include snippets/fastcgi-php.conf;
}
where could be the problem?
Thx all
What do you mean by nothing work? If you get a error of type 500, then it may come from your nginx configuration. However if it is just that your php is not executed, it means that php-fpm service may not be running.
First at all I am new here and new to unix. My previous experience was solely with cPanel, Plesk etc. So please forgive me if there are some mistakes in my approach here.
I have a Centos (release 7.3.1611) VPS with Nginx (1.10.2) and PHP (7.0.17).
I followed some guides to get PHP running and followed some answers given here to similar problems as well to no avail.
Here is my configuration:
www.conf:
listen = /run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
I changed permission and ownership on php-fpm.sock.
nginx.conf:
include /etc/nginx/default.d/*.conf;
nginx/default.d/default.conf:
index index.php index.html index.htm;
server_name _;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Finally I have created a info.php file. When executing it I am getting a nginx error: The page you are looking for is temporarily unavailable. Please try again later.
If disabling the nginx error page the browser is asking me if I want to download the file info.php.
Thank you for helping me out!!!
I initially encountered similar issues but fixed by making sure the path to 'include' and 'fastcgi_pass' were valid. You can verify if something is available at these paths by navigating to those directories.
# PHP 7
# cgi.fix_pathinfo=0 in php.ini
# case-sensitive regex
location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
try_files $uri $uri/ =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Hope this helps.
Cheers,
Ian
Based on this question How to install symfony2 app in a subdirectory in nginx
I've created symfony3 application that works in subdirectory called bcms4. I've manged to make php work with PHP-FPM but I have probelms with assets. When I want to GET asset it directs the request to app_dev and shows 404 because obviosly the path does not exist.
My question is how to make assets not to be proccesed by app_dev but downloaded as supposed?
So when I enter
test.localhost/s/asdfad -> it runs symfony
test.localhost/asdf -> it runs other app living in main dir
test.localhost/s/assets/css/test.css -> it will show file in directory /var/www/test.localhost/bcms4/web/assets/css/test.css
My nginx config:
server {
listen 80;
root /var/www/test.localhost;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name test.localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/s(/.*)$ {
try_files /s/web$1 /web$1 #sf2dev =404;
}
location #sf2dev {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/test.localhost/bcms4/web/app_dev.php;
fastcgi_param SCRIPT_NAME /s/app_dev.php;
fastcgi_param REQUEST_URI /s$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_intercept_errors on;
}
}
After hours of trying I've managed to figure it out with little hack.
This is what I've added to my config file
location ~ ^/s(/.*).\w{1,5}$ {
rewrite ^/s(/.*) /bcms4/web$1 break;
return 404;
}
It'll rewrite files that has prefix /s and extension to directory where they are actually.
Maybe it will help someone. I'll leave question open for a while maybe someone has better solution cause it's seems hacky for me.
I am using nginx with php5-fpm on a Debian stable system. Here is my default site:
server {
listen 443 default_server;
server_name _;
root /home/www/known;
# Deny access to dot files and dirs
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# Enable PHP for vhosts
index index.php index.html index.htm index.nginx-debian.html;
location ~ \.php$ {
# 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;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
ssl on;
ssl_certificate /etc/ssl/certs/phyks.me/root.crt;
ssl_certificate_key /etc/ssl/private/phyks.me/root.key;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
The PHP part is taken from the php fastcgi snippet bundled with Nginx in Debian. The /home/www/known/index.php file exists and is readable by www-data (which both nginx and php-fpm runs as) but I get a "no input file specified" although the file path is passed correctly by nginx to php-fpm.
I tried to replace the content of the location block for php files by
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
In this case I have a blank page (with status 200) and cannot get any verbose output to the log, either from php5-fpm or nginx.
I have been stuck hours with this, trying different config options, but could not figure out what is going on… Do you have any ideas? Plus similar configuration works on other servers.
Thanks!
Please check if your /etc/nginx/fastcgi.conf or /etc/nginx/fastcgi_params includes this param SCRIPT_FILENAME $request_filename; or else append it to file.