How To Fix Nginx Serving PHP Files As Downloads?

Published July 19, 2024

Problem: Nginx Serving PHP Files Incorrectly

When Nginx serves PHP files as downloads instead of running them, it stops PHP-based websites from working properly. This problem usually happens because of wrong settings in Nginx, which makes the server treat PHP files as static files instead of scripts that need to be processed.

Solution: Configuring Nginx to Process PHP Files

Editing the Nginx Configuration File

To fix Nginx serving PHP files as downloads, edit the Nginx configuration file. Open the default server block file, usually at /etc/nginx/sites-available/default. Use a text editor with root privileges.

In the server block, find the PHP processing section. It often starts with location ~ \.php$ {.

Adjusting PHP Processing Directives

Enable PHP file handling by removing any # symbols at the start of relevant lines.

Set the correct FastCGI pass directive. For PHP-FPM, use:

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

Replace "7.4" with your PHP version if needed.

Add or uncomment this line to include FastCGI parameters:

include fastcgi_params;

Tip: Verify FastCGI Parameters

Make sure the fastcgi_params file exists in your Nginx configuration directory. You can check its contents to see if it includes necessary parameters like SCRIPT_FILENAME. If it's missing or incomplete, you may need to create or update it.

Verifying PHP-FPM Settings

Check if PHP-FPM is running:

sudo systemctl status php7.4-fpm

Replace "7.4" with your PHP version if needed.

Make sure the PHP-FPM socket path in your Nginx configuration matches the actual socket file. Find the socket path in the PHP-FPM configuration file, usually at /etc/php/7.4/fpm/pool.d/www.conf.

After making changes, save the file and test Nginx for syntax errors:

sudo nginx -t

If no errors appear, restart Nginx:

sudo systemctl restart nginx

These steps should configure Nginx to process PHP files correctly.

Additional Steps for Troubleshooting

Checking File Permissions

To fix Nginx serving PHP files as downloads, check the file permissions of your PHP files. Make sure the web server has the right permissions to read and execute these files.

Verify the ownership of PHP files:

ls -l /path/to/your/php/files

The files should be owned by the web server user, usually www-data or nginx.

Set the correct permissions:

sudo chown www-data:www-data /path/to/your/php/files
sudo chmod 644 /path/to/your/php/files

This gives read and write permissions to the owner, and read-only access to others.

Tip: Checking Directory Permissions

Remember to also check the permissions of the directory containing your PHP files. The web server needs execute permission on directories to access their contents. Use this command:

sudo chmod 755 /path/to/your/php/directory

Reviewing PHP Configuration

Check your PHP configuration file (php.ini) for settings that might affect how PHP files are processed.

Find your php.ini file:

php -i | grep "Loaded Configuration File"

Open the file and look for the cgi.fix_pathinfo setting. Set it to 0:

cgi.fix_pathinfo=0

This prevents PHP from trying to execute scripts with similar names if the requested script is not found.

Restarting Services

After making changes, restart both Nginx and PHP-FPM to apply the new settings.

Restart Nginx:

sudo systemctl restart nginx

Restart PHP-FPM (replace '7.4' with your PHP version if different):

sudo systemctl restart php7.4-fpm

These steps should help fix any remaining issues with Nginx serving PHP files as downloads.