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

29

217

I am installing a website in a droplet (Digital Ocean). I have an 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 files 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
            }

...

Other "locations" are commented on...

.

If answered 31/8, 2014 at 10:24 Comment(9)
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.Ardy
which file you want to see? Thanks @ArdyIf
Whichever file (or files) you put your config in. The relevant part being... where you set up PHP-FPM in your Nginx config.Ardy
Your config looks alright to me. Do you have any other location blocks that might be taking precedence over your PHP location block?Ardy
I do not know how to check that:/ I dont think so.If
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.)Ardy
Check If I do a query on my website... ( 5.101.99.123/?=imitacion&submit=Ir ) or 5.101.99.123/?= :S :SIf
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!Calumny
See similar question/answer for PHP7 at https://mcmap.net/q/128335/-serving-php-files-as-downloads-instead-of-executing-them/287948Frum
C
180

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

Corrosion answered 31/10, 2014 at 6:5 Comment(16)
/etc/init.d/nginx restartHeribertoheringer
service nginx restart && service php5-fpm restartCornea
nginx -s reloadCornea
with PHP7-fpm is : service php7.0-fpm restartBonspiel
with PHP7-fpm, replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass 127.0.0.1:9000;Delude
@Joy, tried your suggestion but it didn't worked. Had to use fastcgi_pass unix:/run/php/php7.0-fpm.sock;Laminate
I only have php7.0-fpm.pid in /run/php and not php5-fpm.sock :(Padilla
Also make sure the directory that contains the php file is not set to be autoindexed by nginx!Interfluve
In CentOS 7, how do I find that php 7.2 fpm? I can't find it inside /var/run/Outskirts
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.Aeschines
I tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?Ethereal
For php7.2: unix:/var/run/php5-fpm.sock; convert to unix:/var/run/php/php7.2-fpm.sock; (one more nesting /php)Scoles
@Cornea Thanks, this is the one that worked after trying restarting services separately!Danseuse
adding index.php is very important and not very visible to the most! pay attention to details.Colloid
Setting cgi.fix_pathinfo to 0 worked for meTowage
If HTTPS is enable, make sure that you uncomment php location and add index.php to all server{} blocks in /etc/nginx/sites-available/default.Range
C
81

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

Cosh answered 24/1, 2017 at 20:51 Comment(0)
A
62

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;
    }
}
Appling answered 21/2, 2015 at 9:38 Comment(9)
What is SCRIPT_FILENAME ?Pinpoint
If you're using PHP 7.0, then this is correct: fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;Reclamation
@AndrewFox- my filename is php7.0-fpm.pid and not php7.0-fpm.sock in /var/run/php/ what does that mean?Padilla
@Padilla try it. sudo service php7.0-fpm restart. if sock is missing it indicates that php process is being run.Tumbrel
@Tumbrel - tried it and the file still wasn't created. anyway I've found out I don't even need this filePadilla
@Padilla How does your config look like? I also have this problem with fpm :(Outskirts
@Outskirts - 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.confPadilla
There is no php in /var/run... Where to find it?Rustle
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.Onassis
D
35

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.

Dzerzhinsk answered 11/2, 2019 at 17:19 Comment(1)
nginx is a web SERVER not browser, please fix it.Thumb
D
18

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

Department answered 14/3, 2017 at 11:58 Comment(3)
I tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?Ethereal
above shared link "complete detail" is brokenEthereal
What means include snippets/fastcgi-php.conf;?Conductive
H
10

I had the same issue and none of the answers solved the problem.

I ran:

sudo nginx -t

to test the config file at /etc/nginx/sites-available/default.

It gave me these errors:

nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:115
nginx: configuration file /etc/nginx/nginx.conf test failed

So I went into the config file and on the last line there was

#}

I uncommented, ran the test command again and it worked

Hammerless answered 1/7, 2017 at 18:5 Comment(1)
Nice touch to include the test-command syntax in addition to an advice to fix the issue.Schlessinger
P
8

This workded for me.

1) MyApp file

vi /etc/nginx/sites-available/myApp

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

  root /var/www/myApp;
  index index.php index.html index.htm;

  location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }
}

PHP5 users

Change

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

to

fastcgi_pass unix:/var/run/php5-fpm.sock;

2) Configure cgi.fix_pathinfo

Set cgi.fix_pathinfo to 0

Location:

PHP5 /etc/php5/fpm/php.ini

PHP7 /etc/php/7.0/fpm/php.ini


3) Restart services

FPM

php5 sudo service php5-fpm restart

php7 sudo service php7.0-fpm restart

NGINX

sudo service nginx restart
Pearlpearla answered 20/8, 2017 at 19:46 Comment(1)
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;Jaw
S
6

For me it helped to add ?$query_string at the end of /index.php, like below:

location / {
        try_files $uri $uri/ /index.php?$query_string;
}
Sooth answered 21/11, 2017 at 23:35 Comment(0)
E
5
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

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

The above snippets worked for me in case of php7.2

Exuberant answered 13/10, 2019 at 4:3 Comment(0)
A
4

If any of the proposed answers is not working, try this:

1.fix www.conf in etc/php5/fpm/pool.d:

listen = 127.0.0.1:9000;(delete all line contain listen= )

2.fix nginx.conf in usr/local/nginx/conf:

remove server block server{} (if exist) in block html{} because we use server{} in default (config file in etc/nginx/site-available) which was included in nginx.conf.

3. fix default file in etc/nginx/site-available

location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }

4.restart nginx service

sudo service nginx restart

5.restart php service

service php5-fpm restart

6.enjoy

Create any php file in /usr/share/nginx/html and run in "server_name/file_name.php" (server_name depend on your config,normaly is localhost, file_name.php is name of file which created in /usr/share/nginx/html ).

I am using Ubuntu 14.04

Adscription answered 31/8, 2015 at 19:19 Comment(0)
D
4

For anyone having same issue with PHP 7, this is what I done to make nginx execute php files properly in CentOS 7, posted here so in case of anyone having same problem:

  • Follow step by step this document on Digital Ocean.

  • Open the /etc/nginx/conf.d/default.conf (by default I don't have sites-enabled nor sites-available, you can edit accordingly).

  • Edit the location parameter as below:

default.conf:

location ~ \.php$ {
    try_files $uri =404;
    #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

    #instruct nginx execute php7 files instead download them :D
    fastcgi_pass unix:/var/run/php-fpm/www.sock;

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
  • Restart Nginx and PHP services sudo systemctl restart php-fpm and sudo systemctl restart nginx.

  • Last but most important, clear browser cache or running in incognito (Chrome) or Private Browsing (Firefox) etc...

Hope this helpful and happy coding

Dysuria answered 11/2, 2017 at 7:43 Comment(0)
E
3

The answer above seemed to comment out too much for the solution I reached. This is what my file looked like:

/etc/nginx/sites-available/default

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

Hope this helps some folks who are frustrated on a sunday afternoon (c:

Esse answered 17/5, 2015 at 23:24 Comment(0)
M
3

My solution was to add

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

to my custom configuration file, for example etc/nginx/sites-available/example.com.conf

Adding to /etc/nginx/sites-available/default didn't work for me.

Mikol answered 9/6, 2017 at 9:33 Comment(0)
B
2

I my case I was not using /etc/nginx/sites-available/default I was using a different server block configuration file (e.g. example.com), and the only way I was able to fix this problem is by removing the default server block configuration file symbolic link:

$ rm /etc/nginx/sites-enabled/default

then reloading Nginx:

$ sudo systemctl reload nginx
Barone answered 8/8, 2018 at 19:25 Comment(0)
U
2

So this is what finally worked in my case as rewrite rules where the culprit
I changed the nginx rewrite rules as follows..

   location /vendors { rewrite ^/vendors/?$ /vendors.php break; } 

becomes...

  location /vendors { rewrite ^/vendors/?$ /vendors.php last; }

Apparently without the last keyword, the request didn't get restarted, so it never hit the .php location segment, and was simply interpreted as a download –

Undermine answered 19/3, 2019 at 21:51 Comment(0)
S
2

For the record, I found that my php-fpm was not running and I fixed it with service php7.2-fpm start.

Sweptwing answered 23/7, 2019 at 15:0 Comment(0)
H
2

I solved my problem now with this code (change your IP):

location / {
access_log off;
    log_not_found  off;
    client_max_body_size    2000m;
    client_body_buffer_size 512k;
    proxy_buffering on;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;
    proxy_buffer_size 64k;
    proxy_buffers 32 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_connect_timeout 300s;
    proxy_http_version 1.1;
    proxy_set_header Range "";
    proxy_pass   https://123.123.123.123:444;
    proxy_set_header   Host   $host;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_redirect     off;
}
Hiddenite answered 31/8, 2019 at 6:5 Comment(0)
M
1

What worked for me with Ubuntu 16.04, and php7 was deleting this line

fastcgi_split_path_info ^(.+\.php)(/.+)$;

It stopped downloading php files after that.

Motile answered 26/5, 2016 at 10:4 Comment(0)
C
1

Uncomment the .php location in /etc/nginx/sites-available/default

sudo vi /etc/nginx/sites-available/default:

location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            # With php5-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
Cytologist answered 16/3, 2017 at 9:11 Comment(0)
K
1

If anything else doesn't help you. And maybe earlier you installed apache2 with info.php test file. Just clear App Data (cache,cookie) for localhost.

Kruter answered 10/7, 2017 at 13:51 Comment(0)
N
1

check your nginx config file extension is *.conf.
for example: /etc/nginx/conf.d/myfoo.conf

I got the same situation. After I rename the my config file from myfoo to myfoo.conf, it fixed. Do not forget to restart nginx after rename it.

Nahshon answered 18/7, 2017 at 8:48 Comment(0)
A
1

First you have to Remove cache in your browser

Then open terminal and run the following command:

sudo apt-get install php-gettext
sudo nano /etc/nginx/sites-available/default

Then add the following code in the default file:

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

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

If any mismatch just correction and restart Nginx from terminal by the following command

sudo systemctl restart nginx

Then go to browser and Enjoy ...

Ancestral answered 8/8, 2017 at 8:56 Comment(0)
P
1

For me it was the line: fastcgi_pass unix:/var/run/php5-fpm.sock;

which had to be just: fastcgi_pass unix:/run/php5-fpm.sock;

Petiolule answered 26/5, 2018 at 14:18 Comment(0)
B
1

I was about to go mental trying to fix this, for me the issue was that Cloudflare had cached the php file and kept making me download it.

The fix for me was to purge the cache on Cloudflare.

Benisch answered 4/2, 2019 at 2:23 Comment(1)
maybe you want to tell us how?Jarred
P
1

I had been having the same problem what solved it was this server block also have this block above other location blocks if you have css not loading issues. Which I added to my sites-available conf file.

location ~ [^/]\.php(/|$) {
fastcgi_split_path_info  ^(.+\.php)(/.+)$;
fastcgi_index            index.php;
fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
include                  fastcgi_params;
fastcgi_param   PATH_INFO       $fastcgi_path_info;
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Polynomial answered 1/3, 2019 at 9:51 Comment(0)
Z
1

One more thing to check: if you've set up HTTPS access before setting up PHP -- I used certbot -- you'll need to make the changes in /etc/nginx/sites-available/default twice because there will be two server blocks (one listening on port 80 and one listening on port 443).

(I was setting up this server primarily for email and didn't have any use for PHP when I first installed nginx just as a way to run certbot more easily.)

Zwart answered 11/7, 2020 at 20:19 Comment(0)
R
1

I was struggling with the issue for long time, and these steps worked for me.

Step 1: Location block config for all PHP files

location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

Step 2: Add the fastcgi_param in the config file We just have to open the /etc/nginx/fastcgi_params file and add the below line at the end of the file.

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

And then restart the services,

systemctl restart php7.3-fpm
systemctl restart nginx
Returnable answered 17/4, 2021 at 13:47 Comment(0)
R
1

I installed PHP with homebrew on Mac, in my case php-fpm service was not running.

brew services list

enter image description here

Started the service and php scripts begin to execute.

brew services start php

My fastcgi settings in nginx server location block

location ~ \.php$ {
  ...
  include        fastcgi_params;
  fastcgi_pass   127.0.0.1:9000;
  ...
}
Receipt answered 19/9, 2021 at 19:46 Comment(0)
W
0

In Nginx 1.24.0, the default /etc/nginx/fastcgi_params config file is missing these lines:

fastcgi_pass    unix:/run/php-fpm/php-fpm.sock;
fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;

Personally, I have my nginx.conf include my wordpress.conf which includes the default fastcgi_params

Note: you won't see the php-fpm.sock file until after you start php-fpm, for example:

systemctl enable php-fpm --now                                                                                                                                  
# We should now see /run/php-fpm/php-fpm.sock

I'm using PHP 8.3.3 on Arch Linux.

Writer answered 22/2 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.