Question

PHP execution on nginx - files being downloaded instead

Hi,

I am attempting to execute PHP pages within my Nginx root directory, however despite many attempts to fix the issue they continue to be downloaded instead of executing. My nginx/sites-available/default file is:



server {
        listen 80 default_server;
        listen [::]:80 default_server;

         index index.php index.html index.htm index.nginx-debian.html;

         server_name _;

         # Serve up static content directly via nginx

         # Note: /var/www is a symlink to ~/qewd/www

         location / {
                  root /var/www;
                  try_files $uri $uri/ @qewd;

                  expires max;
                  access_log off;
          }

          location ~ \.php$ {
                  try_files $uri $uri/ ;
                  fastcgi_split_path_info ^(.+\.php)(/.+)$;
                  fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                  fastcgi_index index.php;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                  include fastcgi_params;
                  access_log /var/www/php-access.log;
          }

No events are written to the php-access.log and on each occasion I attempt to access a php page in the root directory it is downloaded still. Nginx is correctly routing php to the location block as I have successfully tested 404 messages within the location block. The php7.2-fpm.sock file is within /var/run/php/php7.2-fpm.sock and the php7.2-fpm service is active.

I have tried changing the nginx.conf file for default_type from application/octet-stream to text/html and text/plain but this does not seem to have made a difference also

It seems as though nginx is not passing the php page to fpm but I’m not sure as to why or if I have missed something silly. My nginx user is www-data, which is also the owner of my php7.2-fpm.sock file.

Any help with this is greatly appreciated.

Thank you very much.

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
June 12, 2023
Pinned Answer

Heya all,

The Nginx configuration looks correct. However, there are a few possibilities why PHP files might be downloading instead of being executed:

  1. Incorrect FastCGI params: The FastCGI parameters specified in your fastcgi_param directive might not be correct, resulting in the PHP files not being passed correctly to PHP-FPM.

  2. Incorrect file permissions or ownership: Ensure the Nginx user (www-data or nginx typically) has the correct permissions to access the php7.2-fpm.sock file and the PHP files in your web directory.

  3. The PHP-FPM service isn’t running: Verify that the PHP-FPM service is running correctly. You can do this by running systemctl status php7.2-fpm or service php7.2-fpm status, depending on your system.

  4. Incorrect MIME type for PHP files: Ensure that Nginx is serving PHP files with the correct MIME type. This is typically text/html.

  5. Try_files directive issue: In your location ~ \.php$ block, try_files $uri $uri/ ; might be causing problems. This could be causing Nginx to try and serve a directory instead of passing the request to PHP-FPM. Change it to try_files $uri =404;.

  6. Mismatch in PHP versions: Make sure that the PHP version that your PHP-FPM is running is the same as the one Nginx is configured to use.

After making changes, always remember to restart the Nginx and PHP-FPM services for the changes to take effect. You can do this with systemctl restart nginx and systemctl restart php7.2-fpm or service nginx restart and service php7.2-fpm restart depending on your system.

If anyone else stumbles upon this and it’s encountering problems, checking the Nginx and PHP-FPM error logs might provide more insight into what’s going wrong.

I’m not sure what is wrong with yours, but this worked for me when I was having that problem:

server {

   server_name my-domain.com;
   root /usr/share/nginx/html;
   location = favicon.ico { access_log off; log_not_found off; }
   index  index.php;
   location / {
       try_files $uri $uri/ =404;
   }
   location ~ \.php$ {
       fastcgi_pass unix:/run/php-fpm/www.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }

NOTE: I left out the listen statements in this example since mine is using port 443 (for HTTPS). This is the relevant part for what you are trying to solve.

jarland
DigitalOcean Employee
DigitalOcean Employee badge
January 22, 2019

Hey friend,

From reviewing other situations this is what I pulled together, worth trying to see if it helps:

  1. Edit /etc/php7.2/fpm/php.ini and set cgi.fix_pathinfo to 0.
  2. Restart Nginx & PHP-FPM.

Beyond that maybe just going over this tutorial as a reference and making sure you can’t find anything out of place: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04

Because, frankly, I can’t see the cause clearly either.

Jarland

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel