Nginx serves .php files as downloads, instead of executing them
Solution 1
Try this:
Edit
/etc/nginx/sites-available/default
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
Leave
server_name
alone# Make site accessible (...) server_name localhost;
Add
index.php
to theindex
lineroot /usr/share/nginx/www; index index.php index.html index.htm;
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; }
Edit
/etc/php5/fpm/php.ini
and make surecgi.fix_pathinfo
is set to0
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,
- nginx is a web server (and not an application server) and thus, it can only serve static pages.
- 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)
- 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
Related videos on Youtube

Apeiron
Updated on July 08, 2022Comments
-
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 mainhttp://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 over 8 yearsYes... 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 over 8 yearswhich file you want to see? Thanks @Brad
-
Brad over 8 yearsWhichever file (or files) you put your config in. The relevant part being... where you set up PHP-FPM in your Nginx config.
-
Brad over 8 yearsYour config looks alright to me. Do you have any other location blocks that might be taking precedence over your PHP location block?
-
Apeiron over 8 yearsI do not know how to check that:/ I dont think so.
-
Brad over 8 yearsThe 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 over 8 yearsCheck If I do a query on my website... ( 5.101.99.123/?=imitacion&submit=Ir ) or 5.101.99.123/?= :S :S
-
tm_lv almost 7 yearsafter 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 almost 5 yearsSee similar question/answer for PHP7 at stackoverflow.com/q/42664080/287948
-
-
Guillermo Garcia over 7 years/etc/init.d/nginx restart
-
Duke almost 7 years
service nginx restart && service php5-fpm restart
-
Duke almost 7 years
nginx -s reload
-
Alex about 6 yearswith PHP7-fpm is : service php7.0-fpm restart
-
Joy almost 6 yearswith PHP7-fpm, replace
fastcgi_pass unix:/var/run/php5-fpm.sock;
withfastcgi_pass 127.0.0.1:9000;
-
Swapnil Mhaske almost 6 yearsI 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 yearsWhat is SCRIPT_FILENAME ?
-
Eddie over 5 yearsThanks a lot. You saved my day) Also +1 for incognito!
-
greybeard over 5 yearsNice touch to include the test-command syntax in addition to an advice to fix the issue.
-
user2490003 over 5 yearsYou 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 about 5 yearsIf you're using PHP 7.0, then this is correct:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
-
Kleag almost 5 yearsThis made it for me, also after hours trying a lot of other solutions suggested.
-
vsync over 4 years@AndrewFox- my filename is
php7.0-fpm.pid
and notphp7.0-fpm.sock
in/var/run/php/
what does that mean? -
vsync over 4 yearsI only have
php7.0-fpm.pid
in/run/php
and notphp5-fpm.sock
:( -
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 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 over 4 yearsAlso make sure the directory that contains the php file is not set to be
autoindex
ed by nginx! -
Pathros over 4 yearsIn CentOS 7, how do I find that php 7.2 fpm? I can't find it inside
/var/run/
-
Pathros over 4 years@vsync How does your config look like? I also have this problem with fpm :(
-
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 yearsIt 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 about 4 yearsMake 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 about 4 yearsI tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?
-
Kishore about 4 yearsI tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?
-
Kishore about 4 yearsabove shared link "complete detail" is broken
-
Timo almost 4 yearsWhat means
include snippets/fastcgi-php.conf;
? -
Jon almost 4 yearsNice explainer. Thanks.
-
Cornelius over 3 yearsThere is no php in /var/run... Where to find it?
-
VanAlbert over 3 yearsfastcgi_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 yearsFor php7.2:
unix:/var/run/php5-fpm.sock;
convert tounix:/var/run/php/php7.2-fpm.sock;
(one more nesting/php
) -
Amir Kaftari about 3 yearsYou saved my time bro.
-
Geeocode over 2 yearsnginx 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 over 2 yearsmaybe you want to tell us how?
-
mrRobot about 2 yearsadding
index.php
is very important and not very visible to the most! pay attention to details. -
Kashif Umair Liaqat almost 2 yearsSetting
cgi.fix_pathinfo
to0
worked for me -
MillerMedia over 1 yearI 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 over 1 yearThis has been the actual solution for me.
-
Natnael 11 monthsBig thank you. Nice Explanation
-
Todd 9 monthsThis appears to be a duplicate of np8's answer. Please submit a different resolution or delete.
-
alexia 9 monthsThis 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.