After setting up an environment with Nginx 0.8.54, PHP-FPM (PHP 5.3.2), and MySQL, PHP files aren't being processed while static HTML files work perfectly. The most telling symptom is that accessing PHP files returns a 404 error with PHP headers, suggesting the request reaches PHP-FPM but fails to execute properly.
The current Nginx PHP location block configuration:
location ~ \\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/eman/$fastcgi_script_name;
include fastcgi_params;
}
And the fastcgi parameters:
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
Several potential problems emerge from this configuration:
- The SCRIPT_FILENAME path might not match the actual document root
- Missing essential PHP-FPM pool configuration checks
- Potential file permission issues
- Possible socket vs TCP port communication mismatch
Here's an improved configuration that addresses these issues:
# First, define your root directory at server level
root /var/www/eman;
location ~ \\.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock; # Using Unix socket
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# Additional security parameters
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
fastcgi_param REMOTE_ADDR $remote_addr;
}
After implementing these changes:
- Create a test PHP file:
echo "<?php phpinfo(); ?>" > /var/www/eman/info.php
- Set proper permissions:
chown -R www-data:www-data /var/www/eman
- Check PHP-FPM pool configuration matches socket/port settings
- Test connectivity:
curl -I http://localhost/info.php
If issues persist:
1. Check Nginx error logs: tail -f /var/log/nginx/error.log
2. Verify PHP-FPM is running: ps aux | grep php-fpm
3. Test PHP-FPM directly: SCRIPT_NAME=/info.php SCRIPT_FILENAME=/var/www/eman/info.php REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000
4. Confirm file existence and permissions
For production environments, consider these enhancements:
location ~ [^/]\\.php(/|$) {
fastcgi_split_path_info ^(.+?\\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass php-fpm;
include fastcgi.conf;
# Mitigate security issues
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
Remember to restart both services after changes:
service nginx restart
service php5-fpm restart
When setting up a new Nginx + PHP-FPM stack, one of the most common issues is PHP files not being processed correctly. Here's what we typically observe:
- HTML files serve normally
- PHP files either return blank pages or 404 errors
- PHP files don't trigger standard Nginx 404 pages
- Server headers may show PHP version (indicating some PHP processing)
The core issue usually lies in the PHP handler configuration. Let's examine the critical components:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/eman/$fastcgi_script_name;
include fastcgi_params;
}
1. Path Resolution Issues
The most frequent culprit is incorrect SCRIPT_FILENAME
:
# Problematic:
fastcgi_param SCRIPT_FILENAME /var/www/eman/$fastcgi_script_name;
# Recommended:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
2. Socket vs Port Configuration
Verify your PHP-FPM is listening on the correct interface:
# Check PHP-FPM config (typically in /etc/php/fpm/pool.d/www.conf)
listen = 127.0.0.1:9000
# OR
listen = /var/run/php-fpm.sock
# Corresponding Nginx config:
fastcgi_pass 127.0.0.1:9000;
# OR for sockets:
fastcgi_pass unix:/var/run/php-fpm.sock;
3. File Permissions
PHP-FPM worker must have read access to the files:
chown -R www-data:www-data /var/www/eman
find /var/www/eman -type d -exec chmod 755 {} \;
find /var/www/eman -type f -exec chmod 644 {} \;
1. Verify PHP-FPM Status
systemctl status php-fpm
journalctl -u php-fpm --no-pager -n 20
2. Test PHP Processing Directly
Create a test script:
# /var/www/eman/info.php
<?php phpinfo(); ?>
3. Network Connectivity Check
netstat -tulnp | grep 9000
telnet 127.0.0.1 9000
curl -I http://localhost/info.php
Here's a verified working configuration:
server {
listen 80;
server_name eman.id.au;
root /var/www/eman;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
- Verify PHP-FPM service is running
- Confirm Nginx and PHP-FPM use matching connection methods
- Check SELinux/AppArmor permissions if applicable
- Ensure all config changes are followed by service reloads