Fixing Nginx PHP File Download Issue: Proper FastCGI Configuration for PHP-FPM


2 views

When PHP files are being downloaded instead of executed in Nginx with PHP-FPM, it typically indicates a misconfiguration in how Nginx handles PHP requests. The issue stems from Nginx failing to properly pass PHP files to the FastCGI process manager (PHP-FPM).

From your configuration, I notice several potential issues:

location ~ \\.php$ {
    try_files $uri =404;
    root           html;
    fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

Here's the corrected version with explanations:

location ~ \\.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+?\\.php)(/.*)$;
    
    # Ensure this matches your actual document root
    root /srv/http/arch/httpdocs;
    
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    
    # Critical parameters
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    
    include fastcgi_params;
    
    # Security hardening
    fastcgi_param HTTP_PROXY "";
    fastcgi_intercept_errors off;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
}

After making these changes:

  1. Test Nginx configuration: sudo nginx -t
  2. Reload Nginx: sudo systemctl reload nginx
  3. Check PHP-FPM status: sudo systemctl status php-fpm

These often cause the download behavior:

  • Incorrect socket path (verify with ps aux | grep php-fpm)
  • Mismatched document root between Nginx and PHP-FPM
  • Missing SCRIPT_FILENAME parameter
  • Improper file permissions (PHP files should be readable by the Nginx worker user)

If issues persist, create a test PHP file:

<?php
phpinfo();
?>

Then check:

  • Nginx error logs: tail -f /var/log/nginx/error.log
  • PHP-FPM logs: journalctl -u php-fpm

When PHP files are being downloaded instead of executed in Nginx, this typically indicates a misconfiguration in either:

  • The PHP-FPM service setup
  • Nginx's server block configuration
  • MIME type handling
  • File permissions

For proper PHP execution with Nginx, these elements must be correctly configured:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Here's a complete working configuration example for Arch Linux:

server {
    listen 80;
    server_name localhost;
    root /srv/http/arch/httpdocs;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}
  1. Check PHP-FPM status:
    systemctl status php-fpm
  2. Test socket connection:
    ls -l /run/php-fpm/php-fpm.sock
  3. Verify Nginx configuration:
    nginx -t

MIME Type Issues: Ensure your mime.types contains:

text/html html htm shtml;
text/css css;
text/javascript js;
application/x-httpd-php php;

Permission Problems: The nginx worker process must have execute permissions for the PHP files. Run:

chmod +x /srv/http/arch/httpdocs/*.php

For deeper investigation, enable detailed logging in your Nginx configuration:

error_log /var/log/nginx/error.log debug;

This will provide comprehensive information about how Nginx is handling PHP requests and where exactly the processing chain might be breaking down.