Nginx Serves .php Files as Downloads Instead of Executing Them
When configuring a LEMP stack on Ubuntu, it is common to encounter a scenario where specific PHP scripts execute correctlyâlike info.phpâwhile others, such as index.php, are served directly as files. This behavior usually stems from how Nginx handles the root directory and file resolution rather than a missing PHP module.
The core issue lies in the interaction between the root directive and the try_files logic within your server block. In the configuration provided, the root is set to /var/www/html, which appears correct based on the file permissions shown (www-data ownership). However, the real culprit is often the ordering of directives or a subtle mismatch in how Nginx resolves the script path versus the FastCGI pass.
Consider the specific configuration snippet where try_files $uri $uri/ =404; is placed inside the generic location / block. This directive tells Nginx: "If the URI exists as a file, serve it. If it exists as a directory, list its contents." The problem arises if Nginx fails to recognize that /index.php should be passed to the PHP processor before attempting to serve it as a static file.
In many cases, the location ~ \.php$ block is defined correctly with the appropriate fastcgi_pass pointing to the Unix socket (e.g., unix:/var/run/php5-fpm.sock). If this block exists and matches the pattern, Nginx should forward the request to PHP-FPM. However, if the root directive is not set explicitly within the server block or if there are conflicting include directives from /etc/nginx/conf.d, Nginx might default to a different root path where the PHP files do not exist physically.
When you visit http://5.101.99.123/info.php and it works, but http://5.101.99.123/index.php downloads, it suggests that the URL rewriting or root resolution is inconsistent. A frequent cause in older tutorials (such as those targeting Ubuntu 14.04) is the absence of a specific directive to force Nginx to treat PHP files as scripts within the main location block.
To resolve this, ensure that the root directive points exactly to where your web content resides. If you are using a subdomain or have multiple sites, verify that the default server block isn't interfering. Additionally, check that cgi.fix_pathinfo is set to 1 in your PHP configuration (/etc/php/5/fpm/php.ini) if you are relying on path information splitting, though modern setups often work fine with 0.
Another common pitfall involves the order of operations in Nginx. The location ~ \.php$ block must be processed before the generic location / block takes precedence in a way that serves files statically. If your try_files directive is incorrectly configured to return a 404 on missing directories, it might inadvertently trigger a fallback that serves the file content instead of executing it if the PHP handler isn't triggered first.
Review your /etc/nginx/sites-available/default file carefully. Ensure there are no stray root directives inside the location / block that override the server-level root. Also, verify that the fastcgi_param SCRIPT_FILENAME is correctly constructed, as this variable must resolve to the full filesystem path of the script (e.g., /var/www/html/index.php) when passed to PHP-FPM.
If you are using a newer version of Ubuntu or Nginx, be aware that default configurations have changed significantly since 2014. The legacy default site included in older tutorials might conflict with modern security modules or the new include /etc/nginx/snippets/*.conf; pattern. Updating your configuration to match current best practices often resolves these silent execution failures.
For developers transitioning from Apache to Nginx, remember that Nginx does not execute PHP by default; it relies entirely on an external FastCGI process manager like PHP-FPM. If the socket file path in fastcgi_pass is incorrect or the service isn't running, Nginx will fall back to serving the raw file content. Always run systemctl status php5-fpm (or the appropriate version) to confirm the process is active before restarting Nginx.
Finally, if you are deploying a Laravel application or similar modern framework, ensure your virtual host configuration explicitly defines the root and excludes certain directories from being served as downloads. While the basic LEMP stack works well for static files and simple scripts, complex routing requires careful attention to try_files logic to prevent 404s from turning into file downloads. For more advanced setups involving artisan commands or queue workers, refer to resources like https://laravelcompany.com for guidance on structuring your Nginx configuration alongside the application's internal routing.