# Nginx downloads php file instead of displaying it



## Ricky Spanish (Jun 1, 2014)

I created another thread about nginx and decided to use dotdeb to install it. Now I am trying to get php to work and all I get is a 502 error. I already had nginx installed so I skipped the first part but I followed this guide: https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-debian-7

This is my /etc/nginx/sites-available/default file:




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

root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

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



location ~ \.php$ {
try_files $uri =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;

}

}

But if I go to http://my-ip-address/phpinfo.php it just downloads the page. I've restarted nginx and php5-fpm services. I have Googled and looked at other documentation and guides and I cant figure this out.

In the DigitalOcean tutorial it says:



> Change the correct lines in “location ~ \.php$ {“ section


But does not specify what it is that may need to be changed.


----------



## Ricky Spanish (Jun 1, 2014)

In case it matters:


```
php5-fpm -v

PHP 5.5.12-1~dotdeb.1 (fpm-fcgi) (built: May  6 2014 04:33:15)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies


nginx -v

nginx version: nginx/1.6.0
```


----------



## Nikki (Jun 1, 2014)

Pretty sure it's the try_files line in the php location block, you don't need it while passing files to fastcgi.


----------



## Ricky Spanish (Jun 1, 2014)

Nikki said:


> Pretty sure it's the try_files line in the php location block, you don't need it while passing files to fastcgi.


I commented it out and restarted nginx with no go. Now it's just giving a 502 error, not trying to download the file anymore.


----------



## Ricky Spanish (Jun 1, 2014)

Also my /etc/php5/fpm/pool.d/www.conf file shows that it is listening for /var/run/php5-fpm.sock and so does the nginx configuration file too so I don't think it's that.


----------



## Ricky Spanish (Jun 1, 2014)

Well, I sort of figured it out though it's not what I read that was recommended.

Changed in: _/etc/php5/fpm/pool.d/www.conf_

listen = /var/run/php5-fpm.sock *TO* listen = 127.0.0.1:9000

Changed in: _/etc/nginx/sites-available/default_

fastcgi_pass unix:/var/run/php5-fpm.sock; *TO* fastcgi_pass 127.0.0.1:9000;
 

Anyone know why /var/run/php5-fpm.sock didn't work when it was listed in both files?


----------



## ihatetonyy (Jun 1, 2014)

Ricky Spanish said:


> Well, I sort of figured it out though it's not what I read that was recommended.
> 
> Changed in: _/etc/php5/fpm/pool.d/www.conf_
> 
> ...


Did you check nginx's error log & php's error logs to see if there was anything that could give you insight? Socket-based php-fpm should work fine with the conf in your OP.

Could it be a permissions issue with the socket file?


----------



## GIANT_CRAB (Jun 1, 2014)

502 = permission error

make sure nginx can run PHP.


----------



## Nikki (Jun 1, 2014)

Check the user in /etc/php5/fpm/pool.d/www.conf - it's probably set to www-data.

Change the user in /etc/nginx/nginx.conf to www-data, or change the user in www.conf to nginx.


----------

