Nginx serves .php files as downloads, instead of executing them

Try this:

  1. Edit /etc/nginx/sites-available/default

  2. Uncomment both listen lines to make nginx listen on port 80 IPv4 and IPv6.

     listen   80; ## listen for ipv4; this line is default and implied
     listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
  3. Leave server_name alone

     # Make site accessible (...)
     server_name localhost;
    
  4. Add index.php to the index line

     root /usr/share/nginx/www;
     index index.php index.html index.htm;
    
  5. Uncomment location ~ \.php$ {}

     # pass the PHP scripts to FastCGI server listening on (...)
     #
     location ~ \.php$ {
             try_files $uri =404;
             fastcgi_split_path_info ^(.+?\.php)(/.+)?$;
             # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
             # With php5-cgi alone:
             #fastcgi_pass 127.0.0.1:9000;
             # With php5-fpm:
             fastcgi_pass unix:/var/run/php5-fpm.sock;
             fastcgi_index index.php;
             include fastcgi_params;
     }
    
  6. Edit /etc/php5/fpm/php.ini and make sure cgi.fix_pathinfo is set to 0

  7. Restart nginx and php5-fpm sudo service nginx restart && sudo service php5-fpm restart


I have just started using Linux a week ago, so I really hope to help you on this. I am using nano text editor to edit the files. run apt-get install nano if you don't have it. Google on it to know more.


You need to add this to /etc/nginx/sites-enabled/default to execute php files on Nginx Server:

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

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

    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;
    }
}

I had similar problem which was resolved by emptying the browser cache (also worked fine with different browser).