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

324,356

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

I see a lot of solutions above and many worked correctly for me, but I didn't understand what they were doing and was worried of just copy pasting the code, specifically, fastcgi. So here are my 2 cents,

  1. nginx is a web server (and not an application server) and thus, it can only serve static pages.
  2. whenever, we try rendering/returning a .php file, for example index.php, nginx doesn't know what to do, since it just can't understand a .php file (or for that matter any extension apart from a select few like .html, .js etc. which are static files)
  3. Thus in order to run other kinds of files we need something that sits between nginx and the application (here the php application). This is where common gateway interface (CGI) comes in. It's a piece of software that manages this communication. CGIs can be implemented in any possible language Python (uWSGI), PHP (FPM) and even C. FastCGI is basically an upgraded version of CGI which is much much faster than CGI.

For some, servers like Apache, there is built in support to interpret PHP and thus no need for a CGI.

This digital ocean link, explains the steps to install FPM pretty well and I am not writing the steps needed to solve the issue of php files getting downloaded instead of rendering since the other answers IMHO pretty good.

Solution 5

Update nginx config /etc/nginx/sites-available/default or your config file

if you are using php7 use this

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

if you are using php5 use this

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

Visit here for complete detail Detail here

Share:
324,356

Related videos on Youtube

Apeiron
Author by

Apeiron

Updated on July 08, 2022

Comments

  • Apeiron
    Apeiron 6 months

    I am installing a website in a droplet (Digital Ocean). I have a issue for install NGINX with PHP properly. I did a tutorial https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04 but when I try to run some .php file it's just downloading it... for example... http://5.101.99.123/info.php it's working but... If I go to the main http://5.101.99.123 it's downloading my index.php :/

    Any idea?

    -rw-r--r--  1 agitar_user www-data   418 Jul 31 18:27 index.php
    -rw-r--r--  1 agitar_user www-data    21 Aug 31 11:20 info.php
    

    My /etc/nginx/sites-available/default

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
            root /var/www/html;
            index index.html index.htm index.php;
            # Make site accessible from http://localhost/
            server_name agitarycompartir.com;
                   location ~ \.php$ {
                        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;
                }
                  location / {
                        try_files $uri $uri/ =404;
                        # Uncomment to enable naxsi on this location
                        # include /etc/nginx/naxsi.rules
                }
    

    ...

    Others "location" are commented...

    .

    • Brad
      Brad over 8 years
      Yes... you didn't set up PHP-FPM with Nginx correctly. That's all we can tell you though since you didn't show us any of your config.
    • Apeiron
      Apeiron over 8 years
      which file you want to see? Thanks @Brad
    • Brad
      Brad over 8 years
      Whichever file (or files) you put your config in. The relevant part being... where you set up PHP-FPM in your Nginx config.
    • Brad
      Brad over 8 years
      Your config looks alright to me. Do you have any other location blocks that might be taking precedence over your PHP location block?
    • Apeiron
      Apeiron over 8 years
      I do not know how to check that:/ I dont think so.
    • Brad
      Brad over 8 years
      The location block starts with location some-regex-here {. That regex and flags specify how the server should treat matching resources. The PHP location block that you show in your question instructs Nginx to apply those rules for every request/resource that matches. It sounds like PHP isn't falling into those rules. Assuming the file is being loaded at all, it makes sense that another location block is catching the PHP files before it makes it to the PHP block. (By the way, a quick way to test if the file is getting loaded or not is to make a syntax error and restart the server.)
    • Apeiron
      Apeiron over 8 years
      Check If I do a query on my website... ( 5.101.99.123/?=imitacion&submit=Ir ) or 5.101.99.123/?= :S :S
    • tm_lv
      tm_lv almost 7 years
      after like an hour of googling found this askubuntu.com/a/601996/89455 - if you had a bad configuration before try clearing the cache - worked here!
    • Peter Krauss
      Peter Krauss almost 5 years
      See similar question/answer for PHP7 at stackoverflow.com/q/42664080/287948
  • Guillermo Garcia
    Guillermo Garcia over 7 years
    /etc/init.d/nginx restart
  • Duke
    Duke almost 7 years
    service nginx restart && service php5-fpm restart
  • Duke
    Duke almost 7 years
    nginx -s reload
  • Alex
    Alex about 6 years
    with PHP7-fpm is : service php7.0-fpm restart
  • Joy
    Joy almost 6 years
    with PHP7-fpm, replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass 127.0.0.1:9000;
  • Swapnil Mhaske
    Swapnil Mhaske almost 6 years
    I think the restart and incognito mode helped me. Thank a ton for the noobness we can do.
  • jdstaerk almost 6 years
    @Joy, tried your suggestion but it didn't worked. Had to use fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  • mp3por over 5 years
    What is SCRIPT_FILENAME ?
  • Eddie
    Eddie over 5 years
    Thanks a lot. You saved my day) Also +1 for incognito!
  • greybeard
    greybeard over 5 years
    Nice touch to include the test-command syntax in addition to an advice to fix the issue.
  • user2490003
    user2490003 over 5 years
    You just saved me after an hour of frustration. Everything was correct, it just needed a browser cache reset (or as in my case, i tried it in private browsing).
  • Andrew Fox
    Andrew Fox about 5 years
    If you're using PHP 7.0, then this is correct: fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  • Kleag almost 5 years
    This made it for me, also after hours trying a lot of other solutions suggested.
  • vsync
    vsync over 4 years
    @AndrewFox- my filename is php7.0-fpm.pid and not php7.0-fpm.sock in /var/run/php/ what does that mean?
  • vsync
    vsync over 4 years
    I only have php7.0-fpm.pid in /run/php and not php5-fpm.sock :(
  • zhisme
    zhisme over 4 years
    @vsync try it. sudo service php7.0-fpm restart. if sock is missing it indicates that php process is being run.
  • vsync
    vsync over 4 years
    @zhisme - tried it and the file still wasn't created. anyway I've found out I don't even need this file
  • Victor Lamoine
    Victor Lamoine over 4 years
    Also make sure the directory that contains the php file is not set to be autoindexed by nginx!
  • Pathros
    Pathros over 4 years
    In CentOS 7, how do I find that php 7.2 fpm? I can't find it inside /var/run/
  • Pathros
    Pathros over 4 years
    @vsync How does your config look like? I also have this problem with fpm :(
  • vsync
    vsync over 4 years
    @Pathros - I found that wordpress (in my case) has Server config files in another folder, and those were the ones needed to be edited /etc/nginx/conf.d/wordpress_https.conf
  • Viktor Joras over 4 years
    It was strange to me that my site worked on IE and chrome while in mozilla it downloded the site intead of executing it. It was mozilla's cache fault.
  • NoBugs
    NoBugs about 4 years
    Make sure /var/run/php5-fpm.sock exists for whatever OS you have. Certbot still thinks Ubuntu has PHP7.0 not 7.2 and puts that in config for some reason.
  • Kishore
    Kishore about 4 years
    I tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?
  • Kishore
    Kishore about 4 years
    I tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?
  • Kishore
    Kishore about 4 years
    above shared link "complete detail" is broken
  • Timo
    Timo almost 4 years
    What means include snippets/fastcgi-php.conf;?
  • Jon
    Jon almost 4 years
    Nice explainer. Thanks.
  • Cornelius
    Cornelius over 3 years
    There is no php in /var/run... Where to find it?
  • VanAlbert
    VanAlbert over 3 years
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; is incorrect. It should be /var/run/php/php7.0-fpm.sock; and you need to update the specified php version with whatever you have installed, for instance: /var/run/php/php7.2-fpm.sock;
  • Oleg Reym over 3 years
    For php7.2: unix:/var/run/php5-fpm.sock; convert to unix:/var/run/php/php7.2-fpm.sock; (one more nesting /php)
  • Amir Kaftari
    Amir Kaftari about 3 years
    You saved my time bro.
  • Geeocode
    Geeocode over 2 years
    nginx is a web SERVER not browser, please fix it.
  • Jānis Kristaps Lūsis over 2 years
    @Duke Thanks, this is the one that worked after trying restarting services separately!
  • Thorsten Staerk
    Thorsten Staerk over 2 years
    maybe you want to tell us how?
  • mrRobot
    mrRobot about 2 years
    adding index.php is very important and not very visible to the most! pay attention to details.
  • Kashif Umair Liaqat
    Kashif Umair Liaqat almost 2 years
    Setting cgi.fix_pathinfo to 0 worked for me
  • MillerMedia
    MillerMedia over 1 year
    I am using PHP 7.3 and had to use the line fastcgi_pass unix:/var/run/php-fpm/www.sock; to make it work.
  • ar2015
    ar2015 over 1 year
    This has been the actual solution for me.
  • Natnael 11 months
    Big thank you. Nice Explanation
  • Todd
    Todd 9 months
    This appears to be a duplicate of np8's answer. Please submit a different resolution or delete.
  • alexia
    alexia 9 months
    This is exactly what happened to me too. I was very puzzled as my PHP setup was working fine on other subdomains or when navigating to /index.php manually.