PHP files are DOWNLOADING instead of EXECUTING on nginx

July 18, 2014 61.4k views

Hi All,

It looks like Nginx can't pass the php file to php-fpm.sock which results in download of php files instead of execution.
Please help.

I set cgi.fix_pathinfo=0 inside /etc/php5/fpm/php.ini

I set listen = /var/run/php5-fpm.sock inside /etc/php5/fpm/pool.d/www.conf

My Droplet: Ubuntu 14.04 x64\Laravel 4\nginx\PHP-FPC

All packages seem to be latest - I got everything yesterday via apt-get update/upgrade.

//////////////////Start nginx config /etc/nginx/sites-available/default////////////

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

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

        server_name MY_SERVER_IP_IS_HERE;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

//////////////////End nginx config/////////////////

4 comments
5 Answers

Your config file looks proper. Try clearing your browser's cache or using Incognito Mode (if you're using Chrome) or even a different browser, does it download the file as well?

Kamal, thank you for your answer.
I cleared my cache prior to posting...
What solved it, is the REBOOT of the server!

Perhaps, this documentation (https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04) can be updated by adding "server reboot" if users can't see the changes

Hey guys, I have the same problem but the restart & reboot didn't work. I keep downloading the index.php file at the root of my server.

Here's my nginx config :

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

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

    # Make site accessible from http://localhost/
    server_name 162.243.30.53 lesmauvaisgarcons.ca www.lesmauvaisgarcons.ca;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # NEW CONFIG
    location ~ \.php$ {
        try_files $uri =404;
        #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;
    }

    # OLD CONFIG
    # location ~ \.php$ {
    #    try_files $uri =404;
    #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #    fastcgi_index index.php;
    #    include fastcgi_params;
    #}
}

I have LEMP on Ubuntu 14.04.

Yeah. I have this issue too... Same config... My droplet is www.agitarycompartir.com but If u use www.agitarycompartir.com/? it's loading my index page. I DONT KNOW WHY :S

Hey Folks, just ran into this myself working with Wordpress. Try using:

location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

Referenced it from a working server, then ran a quick search for additional info. Chris_O from StackExchange recommends adding this to your php location block as well if you're running php-fpm. – If you don't already have it in your php-fpm.conf.

location ~ \.php {
        try_files $uri =404;

Cheers!

Have another answer? Share your knowledge.