Nginx serves .php files as downloads, instead of executing them
📝 Nginx Serving .php Files as Downloads? Here's How to Fix It!
So, you're trying to set up your website on Digital Ocean, following a tutorial, but when you try to run some .php files, they are just downloading instead of executing. 😞
Don't worry, I've got you covered! In this post, I'll address this common issue and provide you with easy solutions to get your .php files running smoothly on Nginx. Let's dive in! 💻
🚀 Understanding the Problem
First, let's understand why Nginx is downloading your .php files instead of executing them. Typically, this happens when the Nginx configuration is not set up correctly to handle PHP files. In your case, it seems like something is missing in the sites-available/default
file.
💡 The Solution: Configuring Nginx for PHP Execution
To fix this issue, follow these steps:
Open the
/etc/nginx/sites-available/default
file on your server.Look for the
location ~ \.php$ { ... }
block. In this block, Nginx defines how to handle PHP files.Make sure the
fastcgi_pass
directive points to the correct location of your PHP-FPM socket. In your case, it should befastcgi_pass unix:/var/run/php5-fpm.sock;
.Uncomment the line
include fastcgi_params;
within thelocation ~ \.php$ { ... }
block. This line includes essential FastCGI parameters needed for PHP execution.
That's it! Save the file and restart Nginx for the changes to take effect. In most cases, you can restart Nginx using the command sudo service nginx restart
.
😎 Verify and Test
Now, it's time to verify if our changes fixed the problem. Let's follow these steps:
Access your server's IP address in a browser, for example,
http://YourServerIP
.If your index.php file still downloads, there might be an issue with the root directory. Make sure the
root
directive in your Nginx configuration points to the correct folder where your index.php file resides.To test PHP execution, try accessing a specific .php file directly, like
http://YourServerIP/info.php
. If you see the PHP output instead of a file download, congratulations! You've successfully fixed the issue.
📣 Engage with the Community
If you're still facing any issues or have questions, feel free to leave a comment below. Our amazing community of tech enthusiasts and experts is always ready to help.
🎉 Conclusion
Now you know how to fix Nginx serving .php files as downloads. Properly configuring Nginx for PHP execution is crucial to ensure your website runs smoothly. Remember to double-check your Nginx configuration and restart the server to apply the changes.
Don't let those downloads discourage you, take action and get your PHP files executing flawlessly with Nginx. Happy coding! 💪🚀