Question

PHP files keep downloading instead of loading with nginx ubuntu

my configuration file is virtually identical to step four of this tutorial here

here is what my code looks like as of configuration file

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	root /usr/share/nginx/html;
	index index.php index.html index.htm;

	# Make site accessible from http://localhost/
	server_name 128.199.226.219;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

	#error_page 404 /404.html;

	# redirect server error pages to the static page /50x.html
	#
	#error_page 500 502 503 504 /50x.html;
	#location = /50x.html {
	#	root /usr/share/nginx/html;
	#}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	#location ~ \.php$ {
	# 	try_files $uri =404;
	#	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
	#	fastcgi_index index.php;
	#	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	#	include fastcgi_params;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

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 4, 2023
Pinned Answer

Heya,

In case anyone stumbles upon the question here is a summarized answer:

The issue you’re experiencing is due to the configuration for PHP files in your Nginx server block being commented out. The location ~ \.php$ block is responsible for processing PHP files and passing them to a FastCGI process manager (like PHP-FPM), which executes the PHP scripts and returns the result to Nginx. If this block is commented out or missing, Nginx will treat PHP files as static files and try to download them, instead of processing them.

To fix this, you’ll need to uncomment (remove the # at the beginning of) the lines related to PHP processing in your server block. Based on your posted configuration, the relevant block should look something like this:

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

Also, please ensure that PHP-FPM is installed and running, and that the socket path in fastcgi_pass (currently unix:/var/run/php5-fpm.sock) matches the one configured in your PHP-FPM configuration. If you’re using a different version of PHP, the socket path may be different (for example, unix:/var/run/php/ph8.0-fpm.sock for PHP 8).

Once you’ve made these changes, you can test your configuration with the command sudo nginx -t to make sure there are no syntax errors, and then reload or restart Nginx with sudo service nginx reload or sudo service nginx restart.

Hi. I have the same issue and have spent some days trying to work out what is going wrong.

I have always used apache however this application needs to run on nginx.

(I would like to say that the Digital Ocean tutorials are exceptionally good!!!)

My Default file is as follows:

Default server configuration

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782


    root /var/www/html;

root /usr/share/nginx/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 54.252.213.xxx;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:var/run/php/php7.0-fpm.sock; }

location ~ /\.ht {
    deny all;
}


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #       include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
    #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}

}

Virtual Host configuration for example.com

You can move that to a different file under sites-available/ and symlink that

to sites-enabled/ to enable it.

#server {

listen 80;

I have tried varying combinations of this without luck. Have tried with SSL without.

I am obviously missing something here.

Any assistance greatly appreciated.

Thank you

Ryan Quinn
DigitalOcean Employee
DigitalOcean Employee badge
June 17, 2015

You are missing most of the php configuration in your nginx conf file. This (taken from the end of step 4 in that tutorial) is what you should have in place. Be sure to have the Location / block which tells nginx to use php-fpm to process .php files.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

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