Nginx php files downloading

Nginx serves .php files as downloads, instead of executing them

  1. Edit /etc/nginx/sites-available/default
  2. Uncomment both listen lines to make nginx listen on port 80 IPv4 and IPv6.
 listen 80; ## listen for ipv4; this line is default and implied listen [::]:80 default_server ipv6only=on; ## listen for ipv6 
 # Make site accessible (. ) server_name localhost; 
 root /usr/share/nginx/www; index index.php index.html index.htm; 
 # pass the PHP scripts to FastCGI server listening on (. ) # location ~ \.php$ < try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(/.+)?$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; >

I have just started using Linux a week ago, so I really hope to help you on this. I am using nano text editor to edit the files. run apt-get install nano if you don’t have it. Google on it to know more.

Solution 2

I had similar problem which was resolved by emptying the browser cache (also worked fine with different browser).

Solution 3

You need to add this to /etc/nginx/sites-enabled/default to execute php files on Nginx Server:

Solution 4

I see a lot of solutions above and many worked correctly for me, but I didn’t understand what they were doing and was worried of just copy pasting the code, specifically, fastcgi. So here are my 2 cents,

  1. nginx is a web server (and not an application server) and thus, it can only serve static pages.
  2. whenever, we try rendering/returning a .php file, for example index.php, nginx doesn’t know what to do, since it just can’t understand a .php file (or for that matter any extension apart from a select few like .html, .js etc. which are static files)
  3. Thus in order to run other kinds of files we need something that sits between nginx and the application (here the php application). This is where common gateway interface (CGI) comes in. It’s a piece of software that manages this communication. CGIs can be implemented in any possible language Python (uWSGI), PHP (FPM) and even C. FastCGI is basically an upgraded version of CGI which is much much faster than CGI.

For some, servers like Apache, there is built in support to interpret PHP and thus no need for a CGI.

This digital ocean link, explains the steps to install FPM pretty well and I am not writing the steps needed to solve the issue of php files getting downloaded instead of rendering since the other answers IMHO pretty good.

Solution 5

Update nginx config /etc/nginx/sites-available/default or your config file

if you are using php7 use this

if you are using php5 use this

Visit here for complete detail Detail here

Источник

PHP files keep downloading instead of loading with nginx ubuntu

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!

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.

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

 # 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 ~ /\.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

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.

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.

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!

Источник

NGINX Are Downloading PHP File Instead of Showing It in Browser

When you are building a website, you put your html, php, and css into your server. Some of them are work, but not with php files you are putting in. When you are accessing your web address, it only show blank white page and the php file you are putting in is downloaded to your client’s computer.

What is the actual problem?

The Problem is because fastcgi pass is not finding directory of php-fpm server so instead of executing as web page, nginx downloading php files.

Some lines of script in your Nginx has a fast CGI configuration. Make sure that you are opening the comment or hashtag (#) symbol to make it run. To check it, open nginx by typing :

and head to these blocks of line, make it same as this :

location ~ \.php$ include snippets/fastcgi-php.conf;
#With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
>

Save by pressing ctrl + x and y.

Now, restart the nginx by typing :

If nginx cannot restarting (there is an error line of text), try to troubleshot it by typing

And try solve what shows. After that, do restart nginx.

It is not solve my problem.

Now try to make sure that your php-fpm is installed by typing :

service php(your-php-version)-fpm status

Your php version means php version that you are installing. fpm is more likely one of the dependencies to run php. For Example :

If message show «there is no service running», then install it first by typing :

wait for a second, it will start to installing.

then, type previous service to know if it’s successfully installed and running.

In this case, you can back to your browser and reload the pages.

Open your nginx again, type :

Head to fastcgi_pass code, make sure it is pointing to the right .sock file in php folder. Every version probably has the different location. Here i am using php version 7. If you are using below my version, you can try to figure out where is your .sock files in /var/run folder, and then point your nginx’s fastcgi_pass to it. Here is mine :

Now, restart your server and try to load your browser again.

Try this detailed step by visiting stackoverflow and ibcomputing. Huge thanks to both sites for helping me out.

Источник

Nginx serves .php files as downloads, instead of executing them

There are several reasons why Nginx may serve PHP files as downloads instead of executing them. Here are a few things you can try to troubleshoot the issue:

  1. Make sure that you have the fastcgi_pass directive configured correctly in your Nginx configuration file. This directive specifies the address and port of the FastCGI server that will handle PHP requests.
  2. Check that the fastcgi_index directive is correctly set to the name of the PHP entry script. This directive specifies the default file that should be served when a directory is requested.
  3. Make sure that the root directive is correctly set to the root directory of your website. This directive specifies the root directory that should be used to search for the requested file.
  4. Check that the index directive is correctly set to the name of the PHP entry script. This directive specifies the default file that should be served when a directory is requested.
  5. Make sure that the include directive is correctly set to include the fastcgi_params file. This file contains a set of default FastCGI parameters that are used to process PHP requests.
  6. Check the permissions on your PHP files. Nginx may not be able to execute the PHP files if they are not readable or executable by the user that Nginx is running as.
  7. Check your Nginx error logs for any error messages that may provide more information about the issue.

I hope these suggestions help! If you are still having trouble, please provide more information about your Nginx configuration and any error messages you are seeing, and I will do my best to help.

Источник

Читайте также:  Open in new page link html
Оцените статью