When Nginx serves .php files as downloads instead of executing them, it usually indicates that PHP processing is not correctly configured. Unlike Apache, which can handle PHP directly through its modules, Nginx relies on external processors like PHP-FPM (FastCGI Process Manager) to handle PHP files. Here's a step-by-step guide to resolve this issue:
1. Install PHP and PHP-FPM
Make sure that PHP and PHP-FPM are installed on your server. You can install them using the package manager for your distribution.
For Ubuntu/Debian:
For CentOS/RHEL:
2. Configure PHP-FPM
Ensure that PHP-FPM is correctly configured and running. The default configuration usually works, but you can verify and adjust settings in the PHP-FPM configuration file.
Check PHP-FPM status:
Configuration file location (common paths):
- Ubuntu/Debian:
/etc/php/7.x/fpm/php-fpm.conf - CentOS/RHEL:
/etc/php-fpm.conf
3. Configure Nginx to Pass Requests to PHP-FPM
You need to set up Nginx to pass .php requests to PHP-FPM. Here’s a basic configuration example for Nginx to handle PHP files:
Example Nginx Server Block:
Explanation:
try_files $uri $uri/ =404;: Tries to serve the requested file or directory. If not found, returns a 404 error.location ~ \\.php$: Matches requests for.phpfiles.include snippets/fastcgi-php.conf;: Includes a configuration snippet for FastCGI parameters.fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;: Passes requests to PHP-FPM via a Unix socket. Adjust the path if necessary.fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;: Sets the SCRIPT_FILENAME parameter, required by PHP-FPM to execute scripts.
4. Create the fastcgi-php.conf Snippet
The fastcgi-php.conf snippet is typically included by default but can be customized. Ensure it exists at the specified path or create it if necessary.
Example fastcgi-php.conf:
5. Verify PHP-FPM Socket Path
Ensure that the fastcgi_pass directive points to the correct PHP-FPM socket or TCP address. The socket path can be found in the PHP-FPM configuration.
Check PHP-FPM configuration file (common paths):
- Ubuntu/Debian:
/etc/php/7.x/fpm/pool.d/www.conf - CentOS/RHEL:
/etc/php-fpm.d/www.conf
Look for the listen directive:
6. Restart Nginx and PHP-FPM
After making configuration changes, restart both Nginx and PHP-FPM to apply them.
Restart Nginx:
Restart PHP-FPM:
7. Check File Permissions
Ensure that the web server user (e.g., www-data, nginx, or apache) has read and execute permissions for PHP files and directories.
Set Permissions:
8. Test PHP Execution
Create a test PHP file to verify that PHP is executed correctly.
Example Test PHP File:
Save this as info.php in your web root (/var/www/html) and access it via your browser: http://yourdomain.com/info.php
You should see the PHP information page if PHP is configured correctly.
Summary
To resolve the issue of Nginx serving .php files as downloads:
- Ensure PHP and PHP-FPM are Installed and Running.
- Configure Nginx to Pass PHP Requests to PHP-FPM.
- Verify the
fastcgi_passPath and PHP-FPM Socket Configuration. - Create or Verify
fastcgi-php.confConfiguration Snippet. - Restart Nginx and PHP-FPM Services.
- Check File Permissions.
- Test PHP Execution with a Sample PHP File.
By following these steps, you should be able to configure Nginx to handle PHP files properly and execute them instead of serving them as downloads.