16

I've installed a testing server using nginx + php-fpm. I've tried all of the following:

Nginx + Php5-fpm not rendering php files

nginx + php fpm -> 404 php pages - file not found

When accessing PHP files, nginx throws an 404 error

Summarizing what I've tried:

  • Reinstalling.
  • Changing the script privileges (changed them to 0777).
  • fastcgi_intercept_errors on.
  • Checked the root directive at the levels: server, location and location ~ \.php.
  • Checked the fastcgi_param SCRIPT_FILENAME directive.

The server returns 404 on (and only on) .php scripts. I can rename them to .html and they'd be fine. How can I go about this?

This is my nginx.conf:

user nginx; worker_processes 1; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 2; include /etc/nginx/conf.d/*.conf; index index.html index.htm; server { listen 80; server_name _; root /var/www/html; location / { root /var/www/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /40x.html { #root /var/www/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { #root /var/www/html; } location ~ \.php$ { root /var/www/html; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } 

5 Answers 5

14

Solved it. It turns out that the problem was the permissions set on the socket where php was listening. I just had to change a directive called listen.mode on /etc/php-fpm.d/www.conf

listen.mode = 0750 

And set the user to nginx:

listen.owner = nginx listen.group = nginx 

So the file looks like this:

srwxr-x---. 1 nginx nginx 0 jul 8 08:59 /var/run/php5-fpm.sock 

Because I was using a unix socket instead of a tcp port:

listen = /var/run/php5-fpm.sock; 

Also, I was getting 404 instead of 500 or 503 because my www.conf was configured to redirect errors to custom pages, and since they weren't there, I was getting 404's.

Edit:

It appears that in most recent versions of the nginx distribution in Fedora (Fedora 22, 23), nginx uses the apache user by default, and the socket is set to the user apache too, so no further configuration is needed.

1
  • Right, you must control: - What user/group run nginx (or any webserver) - What user/group run php-fpm Commented Mar 5, 2015 at 19:38
1

Was having the same issue, until I noticed my PHP location block was missing the root entry.

I had set one in the server block under location /, but not one on the server or inside the PHP location block.

location ~ \.php$ { root /usr/local/www/; <--- this was missing try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php82-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } 

Or have one set one the server level

 server { listen 80; server_name example.com www.example.com; root /usr/local/www/; <--- this line here location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?q=$uri&$args; ... ... 
0

I Actually got a "Not Found" error because a book I read gave me an incorrect matching string for the path /php_status which I had configured in php-fpm 7.0.x (currently 7.0.19) and in nginx 1.12 (currently 1.12.0)

Here is the /etc/php/7.0/fpm/pool.d/{config}

pm.status_path = /php_status 

Here is the config for default in /etc/nginx/sites-available (I'm on Ubuntu)

server { listen 80 default; root /var/www; index index.html index.htm default.html; access_log /dev/null; error_log /dev/null; location / { try_files $uri $uri/ =404; } location /php_status { fastcgi_pass unix:/var/run/php7.0-fpm.sock; # fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; allow 127.0.0.1; deny all; } } 

Note: The following is designed so that /php_status is not publicly available on the internet (nor is PHP served or setup for default host). It also includes fastcgi_pass directive for tcp and unix-socket php-fpm

You also should run the following two commands after

sudo service nginx reload sudo service php7.0-fpm restart 

To verify try running

curl http://127.0.0.1/php_status 
0

The 404 error indicates that Nginx is unable to find the file at the specified path.

  1. Check the Nginx error log to find the exact error message by running:
sudo cat /var/log/nginx/error.log 

Look for lines similar to:

*34 open() "/usr/share/nginx/html/foo.php" failed (2: No such file or directory) 

This message indicates that Nginx is trying to access a file that doesn’t exist at the specified path.

  1. Update your Nginx configuration. Modify the location directive in your Nginx configuration file. For example, if your PHP files are in /home/alice, your configuration should look like this:
location ~ \.php$ { root /home/alice; # Update this to your actual directory ... } 
  1. Reload Nginx to apply the changes: sudo systemctl reload nginx

Now Nginx will be able to locate and serve your PHP files correctly.

-1

It occured to me, for another reason: The solution was to put this definition:

root /usr/share/nginx/html; # <- replace with your directory 

in the server block, NOT in the location block:

server { listen .... root /usr/share/nginx/html; # <- replace with your directory ... location ~ \.php$ { # no 'root' definition here :) ... } } 
2
  • no, you can also set a location based root. it might be wrong for the specific location but not in general imho Commented Jun 10, 2022 at 17:51
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Jun 10, 2022 at 17:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.