My nginx + fastcgi configuration downloads php files instead of executing them

iRyanBell picture iRyanBell · Jul 22, 2013 · Viewed 19.7k times · Source

I'm using this configuration on a fresh install of php5-fpm and nginx on ubuntu 13.04:

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/ /index.html;
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    error_page 404 /404.html;

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

But, my web browser is seeing php as text instead of the executed results. Where should I look to troubleshoot?

Answer

Mohammad AbuShady picture Mohammad AbuShady · Jul 22, 2013

Your php code is being displayed directly because it's not being sent to the php engine, that means the location block is being matched and the php file is being served, but the php file isn't being captured by the php block, so your problem is in the php block.

In that block you have 2 fastcgi_pass, one with a port (9000) and the other to a unix socket, you can't have both together, but since you've tagged your question with fastcgi so I'll assume you are using fastcgi, try commenting this line

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