Question

Nginx and PHP getting error 404 with scripts

  • Posted on June 22, 2014
  • ronerazoAsked by ronerazo

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: <pre> 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;

} </pre> 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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.

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;


                }

}

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>