14

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;
        }


    }

}
arielnmz
  • 403
  • 1
  • 4
  • 13

3 Answers3

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.

arielnmz
  • 403
  • 1
  • 4
  • 13
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
MrMesees
  • 127
  • 5
-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 :)
        ...
    }
}
Ari
  • 99
  • no, you can also set a location based root. it might be wrong for the specific location but not in general imho – djdomi Jun 10 '22 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](/help/how-to-answer). – Community Jun 10 '22 at 17:51