NginX Not Executing PHP: Diagnosing FastCGI Failures
When a PHP file is served as raw HTML code rather than executed script, the issue almost always lies in the intersection of your web server configuration and your PHP-FPM process manager. In the scenario described, where nginx.conf appears standard but execution fails, the root cause often stems from how the location blocks are defined or how the FastCGI parameters are passed.
A critical first step is verifying that the try_files directive inside the .php$ location block isn't inadvertently blocking requests before they reach the PHP handler. If a file exists at $uri, try_files serves it; if not, it returns a 404. This logic can sometimes interfere with PHP execution if the file path resolution is ambiguous, though in most standard setups, the syntax shown in the /sites-available/default example is correct provided the socket exists.
The configuration snippet using include snippets/fastcgi-php.conf; suggests you are likely running a Debian/Ubuntu system where Nginx includes a pre-configured set of FastCGI parameters. This is generally safer than manually including fastcgi_params, as it ensures specific variables like $script_filename and $request_filename are correctly mapped to the PHP interpreter. However, this inclusion only works if the snippets directory exists and contains the file. If you have manually written out fastcgi_params or included a non-existent file, the parameters will be empty, leading to a 502 Bad Gateway error or silent failure where Nginx just outputs the source code.
Another common pitfall involves the root directive mismatch between your main configuration and your server block. In the provided examples, one block sets root /usr/share/nginx/html while another sets it to /var/www/html. While this might seem minor, if your PHP application resides in a different directory than what is declared in server_name localhost, Nginx will look for files in the wrong place. Ensure that the root directive matches the physical location of your PHP files exactly.
Furthermore, check the listening socket. The configuration shows fastcgi_pass unix:/var/run/php5-fpm.sock;. This path must match exactly what is defined in your PHP-FPM pool configuration file (usually found in /etc/php/.../fpm/pool.d/www.conf). If Nginx cannot connect to this Unix socket, it will time out. You can verify the socket exists and has the correct permissions by running ls -la /var/run/php5-fpm.sock. If the socket is missing or owned by a different user, you may need to adjust the GID in your PHP pool configuration to match the Nginx worker user (often www-data).
Finally, consider the index directive. The main server block includes index index.php ..., but the custom virtual host snippet at the bottom removes this line entirely and only lists a commented-out version. If you are accessing a directory URL without an explicit filename, Nginx will serve the default index file. If that file is a .php file but lacks the proper execution logic or if the index.php handler isn't triggered correctly due to the missing index declaration, you might see raw content or errors.
To resolve persistent issues where PHP files return as HTML, validate that the FastCGI pass directive connects to an active process and that all required environment variables are inherited correctly via the included parameter file. If you are using a framework like Laravel, ensure your public directory is set as the root in Nginx, not the application root, to prevent exposing sensitive files and maintain proper routing logic.