By Apeiron


2014-08-31 10:24:17 8 Comments

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 (#)

20 comments

@coda 2019-02-11 17:19:33

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 browser (and not an application browser) 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.

@KristinnVikarJ 2019-02-04 02:23:51

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.

@rootx 2015-02-21 09:38:04

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

@RockJake28 2016-09-19 00:30:51

THIS. This is the only one that has worked for me!

@mp3por 2017-06-20 13:41:37

What is SCRIPT_FILENAME ?

@Andrew Fox 2017-10-14 06:37:10

If you're using PHP 7.0, then this is correct: fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

@vsync 2018-04-29 18:16:46

@AndrewFox- my filename is php7.0-fpm.pid and not php7.0-fpm.sock in /var/run/php/ what does that mean?

@zhisme 2018-05-02 08:59:13

@vsync try it. sudo service php7.0-fpm restart. if sock is missing it indicates that php process is being run.

@vsync 2018-05-02 16:20:14

@zhisme - tried it and the file still wasn't created. anyway I've found out I don't even need this file

@Pathros 2018-06-23 03:35:46

@vsync How does your config look like? I also have this problem with fpm :(

@vsync 2018-06-23 14:52:32

@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

@lkahtz 2019-01-09 16:40:49

This one should be accepted as the answer.

@Waqleh 2018-08-08 19:25:05

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

@Alejandro Jourdan 2018-10-17 16:52:54

This worked for me!

@Dongato 2018-05-26 14:18:18

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;

@Jack M. 2014-10-31 06:05:12

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 (if you don't know how to do it, restart the server)


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.

@JG. 2015-08-27 15:01:20

/etc/init.d/nginx restart

@Duke 2016-01-05 23:34:35

service nginx restart && service php5-fpm restart

@Duke 2016-01-05 23:40:22

nginx -s reload

@lean 2016-12-15 09:30:45

with PHP7-fpm is : service php7.0-fpm restart

@Joy 2017-02-24 12:11:54

with PHP7-fpm, replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass 127.0.0.1:9000;

@jdstaerk 2017-03-19 14:57:56

@Joy, tried your suggestion but it didn't worked. Had to use fastcgi_pass unix:/run/php/php7.0-fpm.sock;

@vsync 2018-04-29 18:30:00

I only have php7.0-fpm.pid in /run/php and not php5-fpm.sock :(

@Victor Lamoine 2018-05-15 13:35:52

Also make sure the directory that contains the php file is not set to be autoindexed by nginx!

@Dhaduk Mitesh 2018-06-09 04:33:02

I tried this. But not work for me.

@Pathros 2018-06-23 03:34:10

In CentOS 7, how do I find that php 7.2 fpm? I can't find it inside /var/run/

@NoBugs 2018-10-27 16:24:51

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 D R 2018-11-06 02:25:01

I tried this didn't work for me, I'm using ubuntu 14.04 and php 7.0 how can I fix this issue?

@Tomeg 2017-11-21 23:35:29

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

@Sharif El Shobkshy 2017-08-20 19:46:35

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

@Sanaulla 2017-08-08 08:56:01

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 ...

@Ivan 2017-07-18 08:48:47

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.

@Al Che 2017-07-10 13:51:40

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.

@Bjorn Lindholm Hansen 2017-07-01 18:05:47

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

@greybeard 2017-07-01 18:43:16

Nice touch to include the test-command syntax in addition to an advice to fix the issue.

@T.Todua 2018-11-27 09:26:58

BEST TIP :) thanks

@Kelvin Low Ee Hahn 2017-06-09 09:33:01

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.

@Manivannan Murugavel 2017-03-16 09:11:21

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

@M Arfan 2017-03-14 11:58:20

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

@Kishore D R 2018-11-06 02:34:26

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 D R 2018-11-06 02:35:24

above shared link "complete detail" is broken

@SonDang 2017-02-11 07:43:19

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

@np8 2017-01-24 20:51:39

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

@Swapnil Mhaske 2017-03-09 13:32:24

I think the restart and incognito mode helped me. Thank a ton for the noobness we can do.

@Eddie 2017-06-23 12:17:05

Thanks a lot. You saved my day) Also +1 for incognito!

@user2490003 2017-09-02 22:43:35

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).

@Kleag 2018-01-16 23:27:50

This made it for me, also after hours trying a lot of other solutions suggested.

@Viktor Joras 2018-08-27 09:03:24

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.

@huuthang 2015-08-31 19:19:55

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

@briankip 2016-05-26 10:04:33

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.

@Benjamin McFerren 2015-05-17 23:24:22

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:

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Share Nginx server configuration

  • 2014-06-11 04:04:32
  • Anam
  • 1221 View
  • 3 Score
  • 3 Answer
  • Tags:   php nginx

2 Answered Questions

[SOLVED] can't interpret PHP on RHEL + nginx

  • 2017-06-08 17:33:56
  • Night
  • 91 View
  • 1 Score
  • 2 Answer
  • Tags:   php linux nginx

0 Answered Questions

2 Answered Questions

NGINX shows script code instead of execute

  • 2014-01-14 20:40:09
  • Emiliano Albanese
  • 2676 View
  • 1 Score
  • 2 Answer
  • Tags:   php nginx rewrite

0 Answered Questions

All php file result in 404 on nginx

0 Answered Questions

I'm trying to setup Moodle 2.3 (not 2.5) ver with nginx latest build

  • 2014-12-03 03:54:52
  • gifford
  • 78 View
  • 1 Score
  • 0 Answer
  • Tags:   php nginx

1 Answered Questions

Nginx downloads php file in SOME cases (not all)

  • 2014-10-27 14:30:04
  • user1652792
  • 818 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx php

1 Answered Questions

[SOLVED] Vagrant / puppet config for complex vhost setting (if statements etc.)?

1 Answered Questions

[SOLVED] migrating from lighttpd to nginx

1 Answered Questions

nginx + php-fpm + pidora(fedora) on raspberry

  • 2013-10-17 07:11:07
  • GergA
  • 1593 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx fedora php

Sponsored Content