ronerazo
By:
ronerazo

Nginx and PHP getting error 404 with scripts

June 22, 2014 28.5k views
I have Nginx running and I'm trying to execute php scripts with it, I'm using the following tutorial as a guide: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04 I've done the same steps as the article but I have a problem; I get a 404 when I try to access my php file. Here's what I've tried: -Verified the directory permissions and ownerships: permissions set to 755 and owner is www-data with group www-data -Changed file name to index.php I currently have 2 config files for each of my sites( mydomain.com and blog.mydomain.com) and I'm trying to run php scripts on mydomain.com. Here's my config file for the site:
server {
        listen 80;

        server_name mydomain.com;

        location / {
                root /usr/share/nginx/html/mydomain.com;
                index index.html index.htm index.php;
        }

location ~ \.php$ {

        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}
Is there another config file I should be checking? Or perhaps there is a different config since I'm running subdomains? Thanks for your time.
3 Answers

I managed to get it working, the thing I was missing was including <code>location ~ .php$</code> inside the <code>location /</code> section. It ended up looking like this:
<pre>
location / {
root /usr/share/nginx/html/mydomain.com;
index index.html index.htm index.php;

            location ~ \.php$ {

                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
            include fastcgi_params;
             }
    }

</pre>
Not sure if it's standard to do it like this but it worked for me.

Are you seeing anything interesting in your Nginx error logs when you try to access the site? Run:
<pre>
tail /var/log/nginx/error.log
</pre>

I'd also move <code>index.php</code> to the front in your location block:
<pre>
location / {
root /usr/share/nginx/html/mydomain.com;
index index.php index.html index.htm;
}
</pre>

I am getting the same error when trying to load the info.php file and have been trying to solve it for hours but can't seem to find a solution.

I installed LEMP with 16.04 but it seems hard to find why my php is not working.

My nginx sites-available file looks like this:

server {
        listen 80;
        listen [::]:80;

        server_name website.com www.website.com;

        location / {
                root /var/www/website.com/html;
        index index.html index.htm index.php index.nginx-debian.html;
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;


        }

}


server {
        listen 443;
        ssl on;
                # ssl_certificate
                ssl_certificate /etc/nginx/cert_chain.crt;
                # ssl_certificate_key
                ssl_certificate_key /root/website.com.key;

                server_name website.com website.com;
                access_log /var/log/nginx/nginx.vhost.access.log;
                error_log /var/log/nginx/nginx.vhost.error.log;
        location / {

                root /var/www/website.com/html;
        index index.php  index.html index.htm index.nginx-debian.html;

        }

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;


                }

}
Have another answer? Share your knowledge.