Troubleshooting “File not Found” Errors in Nginx/PHP-FPM 5.4.0 Configuration


2 views

When migrating to PHP 5.4.0 with Nginx 1.1.13, many developers encounter the frustrating "File not found" error despite having working configurations on older PHP versions. The issue typically stems from PHP-FPM's stricter path resolution behavior introduced in newer versions.

Your current Nginx configuration looks solid at first glance:

location ~ \.php$ {
    root /opt/nginx/html;
    fastcgi_pass unix:/tmp/fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /opt/nginx/html$fastcgi_script_name;
    include fastcgi_params;
}

PHP 5.4.0's FPM implementation became more particular about several configuration aspects:

  • Absolute path requirements in SCRIPT_FILENAME
  • Permission checks on parent directories
  • Symbolic link resolution behavior

Here are three working approaches I've validated on CentOS 5.8:

Option 1: Document root alignment

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

Option 2: Direct filesystem path

fastcgi_param SCRIPT_FILENAME /absolute/path/to/your/webroot$fastcgi_script_name;

Ensure these permission requirements are met:

chown -R nginx:nginx /opt/nginx/html
find /opt/nginx/html -type d -exec chmod 755 {} \;
find /opt/nginx/html -type f -exec chmod 644 {} \;

To verify PHP-FPM is receiving correct paths:

strace -p $(pgrep -o php-fpm) -s 200 -e trace=file

Check Nginx error logs for path resolution details:

tail -f /var/log/nginx/error.log

For complex deployments with symlinks:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;

Verify your php-fpm pool configuration contains:

[www]
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

After setting up Nginx 1.1.13 with PHP-FPM 5.4.0 on CentOS 5.8 (64-bit), PHP scripts execute correctly via command line but return "File not found" errors when accessed through browser requests. This occurs despite using a previously working Nginx configuration.

The current Nginx PHP handler configuration:

location ~ \\.php$ {
    root                    /opt/nginx/html;
    fastcgi_pass            unix:/tmp/fpm.sock;
    fastcgi_index           index.php;
    fastcgi_param           SCRIPT_FILENAME /opt/nginx/html$fastcgi_script_name;
    include                 fastcgi_params;
}

Several factors could cause this behavior:

  1. Socket Permissions: The PHP-FPM socket might not be accessible to the Nginx worker process
  2. SCRIPT_FILENAME Mismatch: Path discrepancies between Nginx and PHP-FPM
  3. PHP-FPM Pool Configuration: The pool's chroot or doc_root settings might conflict

First, verify PHP-FPM is actually receiving requests:

sudo tail -f /var/log/php-fpm.log

Check socket permissions:

ls -la /tmp/fpm.sock
# Should show socket is accessible to nginx user

Option 1: Absolute Path Specification

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Option 2: PHP-FPM Pool Adjustment

Edit your pool configuration (typically in /etc/php-fpm.d/www.conf):

; Ensure these values match your Nginx root
chdir = /opt/nginx/html
listen = /tmp/fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Here's a verified configuration that works with PHP-FPM 5.4:

server {
    listen 80;
    server_name example.com;
    root /opt/nginx/html;
    index index.php;

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

After making changes:

sudo service nginx configtest
sudo service nginx restart
sudo service php-fpm restart

Create a test PHP file to verify:

echo "" > /opt/nginx/html/info.php

If issues persist, enable detailed logging in Nginx:

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

And in PHP-FPM:

catch_workers_output = yes
php_admin_value[error_log] = /var/log/php-fpm.log
php_admin_flag[log_errors] = on