PHP files keep downloading instead of loading with nginx ubuntu

June 17, 2015 14.1k views
PHP LEMP Nginx MySQL Ubuntu

my configuration file is virtually identical to step four of this tutorial here

here is what my code looks like as of configuration file

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name 128.199.226.219;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   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;
    #}

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

You are missing most of the php configuration in your nginx conf file. This (taken from the end of step 4 in that tutorial) is what you should have in place. Be sure to have the Location / block which tells nginx to use php-fpm to process .php files.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    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$ {
        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;
    }
}
  • What exactly is in my configuration file that is causing it to download the info.php file instead of opening it when i try and view the php info page?

    Also should I also remove these "#" to make them work or does it make no difference?

  • The # characters mark the entire line after them as a comment so they are ignored by nginx. Uncommenting those should do the trick. Right now since the whole section on php files is commented out nginx is just considering them a regular text file.

Have another answer? Share your knowledge.