demoskp
By:
demoskp

How to get PHP working after LEMP with 16.04 installation? I get error 404 even with info.php file.

June 9, 2017 183 views
Nginx PHP LEMP Ubuntu 16.04

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;
# sslcertificate
ssl
certificate /etc/nginx/certchain.crt;
# ssl
certificatekey
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;


            }

}

3 Answers

I found and resolved the issue, I moved
root /var/www/website.com/html;
index index.html index.htm index.php index.nginx-debian.html;
to the top just before server name and added some other code which you can see below:

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

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

        server_name example.com www.example.com;

        location / {

        try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;

        }

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



        }


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

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

                server_name example.com www.example.com;
                access_log /var/log/nginx/nginx.vhost.access.log;
                error_log /var/log/nginx/nginx.vhost.error.log;
        location / {
                 try_files $uri $uri/ =404;
        }


        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;

        }

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



    }

  • Okay great job :)

  • @demoskp

    As a general note, to help with configuration, you can stick the index directive in:

    /etc/nginx/nginx.conf
    

    ... between the http { and } block and then remove it from all of your server blocks. That would at least remove one line from your server blocks and prevent index issues.

    i.e

    http {
        index index.html index.htm index.php index.nginx-debian.html;
    
        ....
        ....
        ....
        ....
    }
    

    Where .... is the rest of the in the http block configuration. You'd then restart NGINX.

    By placing it in the nginx.conf file, it sets index globally so it works for all server blocks. I do this on all standard and custom builds as I try to keep as much out of the server block as I can where possible.

@demoskp

You need first to enable this site by creating a symbolic link for this site in sites-enabled directory

use this command to achieve this:

sudo ln -s /etc/nginx/sites-available/<sitename> /etc/nginx/sites-enabled/<sitename>

then restart nginx: sudo service nginx restart

Hope this helps

  • The symbolic link exists as my site file exists in the folder sites-enabled as well and any changes I make in sites-available can be seen in sites-enabled too.

Have another answer? Share your knowledge.