Nginx return php file

nginx downloads php files instead of rendering them

I found a similar question here: https://stackoverflow.com/questions/25591040/nginx-serves-php-files-as-downloads-instead-of-executing-them But unfortunately the answer there doesn’t help me. So here goes my story. What Works When I navigate to myipaddress/index.html, the welcome to nginx page renders correctly. When I navigate to myipaddress/widgets/index.html, the welcome to nginx page renders correctly. NB: I have slightly altered this index.html from the one above so the welcome message is different. This way, I prove to myself it really is finding the two different index.html files / folder structure. What Doesn’t Works When i navigate to myipaddress/widgets/info.php, instead of showing the details of phpinfo() command, it downloads the php file. Installed Packages I am running alpinelinux. Installed the following packages:

lab-1:/var/www/localhost/htdocs# cat /etc/apk/world curl nginx php7-common php7-fpm 
lab-1:/etc/php7# nginx -v nginx version: nginx/1.14.0 
lab-1:/var/www/localhost/htdocs# rc-status Dynamic Runlevel: manual nginx [ started ] php-fpm7 [ started ] 
lab-1:/var/www/localhost/htdocs# ls -lah total 16 drwxr-xr-x 3 www www 4.0K Sep 13 14:22 . drwxr-xr-x 3 www www 4.0K Sep 13 13:17 .. -rw-r--r-- 1 www www 624 Sep 12 20:35 index.html drwxr-xr-x 2 www www 4.0K Sep 13 17:40 widgets lab-1:/var/www/localhost/htdocs# ls -lah widgets/ total 16 drwxr-xr-x 2 www www 4.0K Sep 13 17:40 . drwxr-xr-x 3 www www 4.0K Sep 13 14:22 .. -rw-r--r-- 1 root root 632 Sep 13 14:23 index.html -rwxr-xr-x 1 www www 27 Sep 13 15:45 info.php 
  • /etc/nginx/conf.d/default.conf =>https://pastebin.com/QWspV6DF
  • /etc/nginx/conf.d/widgets.conf =>https://pastebin.com/9XR5mRUJ
  • /etc/php7/php-fpm.d/www.conf =>https://pastebin.com/c8HKj50W
  • /etc/nginx/fastcgi.conf =>https://pastebin.com/WR5w1j3P
  • /etc/nginx/fastcgi_params =>https://pastebin.com/3WSG6FWx
  • /etc/php7/php-fpm.conf =>https://pastebin.com/k3dqwrDg

NOTE: nginx install put both fastcgi.conf and fastcgi_params in the folder. they look pretty similar. I was playing around and included both.

netstat to prove php fpm is running

lab-1:/etc/php7# netstat -lnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4268/php-fpm.conf) tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4323/nginx.conf tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 346/sshd tcp 0 0 . 80 . * LISTEN 4323/nginx.conf tcp 0 0 . 22 . * LISTEN 346/sshd 
4268 root 0:00 php-fpm: master process (/etc/php7/php-fpm.conf) 4275 nginx 0:00 php-fpm: pool www 4276 nginx 0:00 php-fpm: pool www 4323 root 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 4324 nginx 0:00 nginx: worker process 4325 nginx 0:00 nginx: worker process 4326 nginx 0:00 nginx: worker process 4328 nginx 0:00 nginx: worker process 4329 nginx 0:00 nginx: worker process 4330 nginx 0:00 nginx: worker process 4331 nginx 0:00 nginx: worker process 4332 nginx 0:00 nginx: worker process 

I tried to make sure the listen owner and listen group is the same user that nginx runs as — which is «nginx». Also I know that using «127.0.0.1:9000» is not as good as using a unix socket. But I’m trying to use the default install settings for now for php7-fpm. (Truth be told, I also don’t know how to change it)

  1. can you see where I’ve gone wrong?
  2. Does the fact that it’s downloading the php file prove it’s finding the «location» directive in the right conf file? so is the problem limited to content of my location<> directive within the widgets.conf file?
  3. once I’ve fixed the php problem, if there are really huge gaffs in my set up that you can see, can you point them out? Thanks.
Читайте также:  Обычно разрешается только одно использование адреса сокета php

Источник

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

Источник

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.

Источник

Оцените статью