Question

PHP files are DOWNLOADING instead of EXECUTING on nginx

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

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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?

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!

In my case, I forgot didn’t install PHP yet. Make sure you already installed it by typing php -v. If there is no version information about your PHP, then you should install it with:

  1. sudo apt update
  2. sudo apt install php-fpm