Nginx not showing PHP Files?

May 31, 2017 3.7k views
Nginx Block Storage Ubuntu 16.04

Hey,
So I am having a lot of problems with nginx lately. As of nginx is not accepting my php file to put it on my sub domain. I have setup php-fpm etc. I need help with it and below is the config.

Also the directory where the file is present
root /var/www
and
server_info
won't be shown as I don't wanna.

I have also did the php-fpm for php7.1 as that was available.

You can also see it on https://codepen.io/sharletsings123/pen/ZKdMXV

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80;
    listen [::]:80;

        #SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/cantShowItsPrivate/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

server_name lol.com; 

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf

    #   # With php7.1-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.1-fpm:
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#.      listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

Thanks/
Kind regards,
/shar.

3 Answers

Hi @sharletsings123

By default, Ubuntu 16.04 only comes with PHP7.0-FPM - did you remember to remove the previous version before installing 7.1?
Can you also list you pool configuration, which is located in /etc/php/7.1/fpm/pool.d/

Also list the last 20 lines of the error log:

tail -20 /var/log/nginx/error.log

@sharletsings123

The first thing I normally recommend is cleaning up the server block and removing the clutter so you aren't accidentally running in to issues with various lines should they happen to cause issues.

So your cleaned up server block would look like this:

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

    root /var/www/cantShowItsPrivate/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name lol.com; 

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }
}

Note, on your include snippets/fastcgi-php.conf line, you're missing a ; at the end. I've fixed that in the above :-).

From there, how PHP is handled depends on what's within the block starting with:

location ~ \.php$

The first thing I see is:

include snippets/fastcgi-php.conf

So we'd need to know what's in that file to know if there's something causing issues. You can also check the error log using the command that @hansen provided:

tail -20 /var/log/nginx/error.log

...

Generally, when it comes to PHP, you'd have something that looks like this instead of what you have right now:

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;

        include snippets/fastcgi-php.conf;
    }

Within snippets/fastcgi-php.conf, I'd use:

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 512k;
fastcgi_buffers 512 16k;
fastcgi_busy_buffers_size 1m;
fastcgi_temp_file_write_size 4m;
fastcgi_max_temp_file_size 4m;
fastcgi_intercept_errors off;

fastcgi_param SCRIPT_FILENAME   $request_filename;
fastcgi_param PATH_INFO         $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
fastcgi_param QUERY_STRING      $query_string;
fastcgi_param REQUEST_METHOD    $request_method;
fastcgi_param CONTENT_TYPE      $content_type;
fastcgi_param CONTENT_LENGTH    $content_length;
fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
fastcgi_param REQUEST_URI       $request_uri;
fastcgi_param DOCUMENT_URI      $document_uri;
fastcgi_param DOCUMENT_ROOT     $document_root;
fastcgi_param SERVER_PROTOCOL   $server_protocol;
fastcgi_param REQUEST_SCHEME    $scheme;
fastcgi_param HTTPS             $https if_not_empty;
fastcgi_param HTTP_PROXY        "";
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version;
fastcgi_param REMOTE_ADDR       $remote_addr;
fastcgi_param REMOTE_PORT       $remote_port;
fastcgi_param SERVER_ADDR       $server_addr;
fastcgi_param SERVER_PORT       $server_port;
fastcgi_param SERVER_NAME       $server_name;
fastcgi_param REDIRECT_STATUS   200;

Once those changes are made, restart NGINX and see if everything is working. If not, check the error log to see what it's showing and paste that to a code block as a reply and we can better help.

@hansen and @jtittle Thanks for all the help. The File is up and runnung! I had to fix snippets/fastcgi-php.conf.

After that it workd.

So thanks alot guys!

Have another answer? Share your knowledge.