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:
sudo apt update
sudo apt install php-fpm
For CentOS/RHEL:
sudo yum install php-fpm
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:
sudo systemctl status php-fpm
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:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\\.ht {
deny all;
}
}
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.php
files.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
:
# /etc/nginx/snippets/fastcgi-php.conf or a similar path
fastcgi_split_path_info ^(.+\\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
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:
listen = /var/run/php/php7.x-fpm.soc
6. Restart Nginx and PHP-FPM
After making configuration changes, restart both Nginx and PHP-FPM to apply them.
Restart Nginx:
sudo systemctl restart nginx
Restart PHP-FPM:
sudo systemctl 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:
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \\;
sudo find /var/www/html -type f -exec chmod 644 {} \\;
8. Test PHP Execution
Create a test PHP file to verify that PHP is executed correctly.
Example Test PHP File:
<?php
phpinfo();
?>
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_pass
Path and PHP-FPM Socket Configuration. - Create or Verify
fastcgi-php.conf
Configuration 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.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github