Skip to content Skip to sidebar Skip to footer

NGINX Are Downloading PHP File Instead of Showing It in Browser

The Scenario

When you are building a website, you put your html, php, and css into your server. Some of them are work, but not with php files you are putting in. When you are accessing your web address, it only show blank white page and the php file you are putting in is downloaded to your client's computer.



What is the actual problem?

The Problem is because fastcgi pass is not finding directory of php-fpm server so instead of executing as web page, nginx downloading php files.

What?

I mean,

Some lines of script in your Nginx has a fast CGI configuration. Make sure that you are opening the comment or hashtag (#) symbol to make it run. To check it, open nginx by typing :

nano /etc/nginx/sites-enabled/your-server-block-file

and head to these blocks of line, make it same as this :

 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 #With php-fpm (or other unix sockets):
 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 #With php-cgi (or other tcp sockets):
 #fastcgi_pass 127.0.0.1:9000;
 }


Save by pressing ctrl + x and y.

Now, restart the nginx by typing :

service nginx restart

If nginx cannot restarting (there is an error line of text), try to troubleshot it by typing

nginx -t


And try solve what shows. After that, do restart nginx.

It is not solve my problem...

Don't worry,

Now try to make sure that your php-fpm is installed by typing :

service php(your-php-version)-fpm status


Your php version means php version that you are installing. fpm is more likely one of the dependencies to run php. For Example :

service php7-fpm status


If message show "there is no service running", then install it first by typing :

apt-get install php7-fpm

wait for a second, it will start to installing.

then, type previous service to know if it's successfully installed and running.

In this case, you can back to your browser and reload the pages.

If it's not working...

Open your nginx again, type :

nano /etc/nginx/sites-enabled/your-server-block-file

Head to fastcgi_pass code, make sure it is pointing to the right .sock file in php folder. Every version probably has the different location. Here i am using php version 7. If you are using below my version, you can try to figure out where is your .sock files in /var/run folder, and then point your nginx's fastcgi_pass to it. Here is mine :

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

Now, restart your server and try to load your browser again.

If things didn't workout,

Try this detailed step by visiting stackoverflow and ibcomputing. Huge thanks to both sites for helping me out.