Nginx serves .php downloads instead of executing

Anton Lytvynov
2 min readApr 13, 2021

--

How to run php files on Nginx? PhpFpm & FastCgi

This article based on two previous

Why I download .php files instead of execute?

The simple answer is

Your Nginx server does not know how to trait .php

Our site configuration file from the previous guide updated with lines 9 and 20–27

server {
listen 80;
server_name test.loc;

#access_log logs/host.access.log main;

location / {
root /var/www;
index index.php index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You need to add lines 20–27 to explain how to execute .php files

1st : add index.php in index

Add index.php to index.

2nd : add location .php

Explain to Nginx how to execute your .php files

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

What Next?

I regularly add other posts. If you want to be notified you can also subscribe here.

Thank you for your attention

Thank you for your attention.

Thank you for your attention.

Do you need help with web or mobile development? Feel free to contact me here.

P.S. I really appreciate your like or share 🙏.

--

--

Anton Lytvynov

CEO & Founder of Lytvynov Production, Senior web developer, architect, cryptocurrencies trader, https://lytvynov-production.com