0Day Forums
Nginx serves .php files as downloads, instead of executing them - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+---- Forum: WordPress (https://zeroday.vip/Forum-WordPress)
+---- Thread: Nginx serves .php files as downloads, instead of executing them (/Thread-Nginx-serves-php-files-as-downloads-instead-of-executing-them)

Pages: 1 2 3


Nginx serves .php files as downloads, instead of executing them - zadiejberidmw - 07-26-2023

I am installing a website in a droplet (Digital Ocean). I have an issue for install NGINX with PHP properly. I did a tutorial

[To see links please register here]

but when I try to run some .php files it's just downloading it...
for example... `http://5.101.99.123/info.php` it's working but... If I go to the main `http://5.101.99.123` it's downloading my index.php :/

Any idea?

-rw-r--r-- 1 agitar_user www-data 418 Jul 31 18:27 index.php
-rw-r--r-- 1 agitar_user www-data 21 Aug 31 11:20 info.php

My **/etc/nginx/sites-available/default**



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

root /var/www/html;
index index.html index.htm index.php;

# Make site accessible from

[To see links please register here]

server_name agitarycompartir.com;

location ~ \.php$ {
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;
}


location / {

try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
...

Other "locations" are commented on...



.




RE: Nginx serves .php files as downloads, instead of executing them - hallel930452 - 07-26-2023

The answer above seemed to comment out too much for the solution I reached. This is what my file looked like:

/etc/nginx/sites-available/default

location ~ \.php$ {
# 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;
}

Hope this helps some folks who are frustrated on a sunday afternoon (c:



RE: Nginx serves .php files as downloads, instead of executing them - play874 - 07-26-2023

What worked for me with Ubuntu 16.04, and php7 was deleting this line

`fastcgi_split_path_info ^(.+\.php)(/.+)$;`

It stopped downloading php files after that.


RE: Nginx serves .php files as downloads, instead of executing them - corrigendum744 - 07-26-2023

If any of the proposed answers is not working, try this:

##1.fix

[To see links please register here]

in etc/php5/fpm/pool.d:
`listen = 127.0.0.1:9000;(delete all line contain listen= )`
##2.fix nginx.conf in usr/local/nginx/conf:
`remove server block server{} (if exist) in block html{} because we use server{} in default (config file in etc/nginx/site-available) which was included in nginx.conf.`
## 3. fix default file in etc/nginx/site-available
`location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}`
##4.restart nginx service
>sudo service nginx restart

##5.restart php service
>service php5-fpm restart

##6.enjoy
>Create any php file in /usr/share/nginx/html and run in "server_name/file_name.php"
(server_name depend on your config,normaly is localhost, file_name.php is name of file which created in /usr/share/nginx/html ).


I am using Ubuntu 14.04


RE: Nginx serves .php files as downloads, instead of executing them - conjoinedsnagz - 07-26-2023

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


RE: Nginx serves .php files as downloads, instead of executing them - abandonee849368 - 07-26-2023

For anyone having same issue with PHP 7, this is what I done to make nginx execute php files properly in CentOS 7, posted here so in case of anyone having same problem:

- Follow step by step this document on [Digital Ocean][1].

- Open the `/etc/nginx/conf.d/default.conf` (by default I don't have sites-enabled nor sites-available, you can edit accordingly).

- Edit the `location` parameter as below:


*default.conf*:



location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

#instruct nginx execute php7 files instead download them :D
fastcgi_pass unix:/var/run/php-fpm/www.sock;

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

- Restart Nginx and PHP services `sudo systemctl restart php-fpm` and `sudo systemctl restart nginx`.

- Last but most important, clear browser cache or running in `incognito (Chrome)` or `Private Browsing (Firefox)` etc...

Hope this helpful and happy coding


[1]:

[To see links please register here]





RE: Nginx serves .php files as downloads, instead of executing them - pallu402723 - 07-26-2023

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

if you are using php7 use this

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

if you are using php5 use this

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

Visit here for complete detail [Detail here][1]


[1]:

[To see links please register here]




RE: Nginx serves .php files as downloads, instead of executing them - atomize362 - 07-26-2023

**Uncomment the \.php location in /etc/nginx/sites-available/default**

> sudo vi /etc/nginx/sites-available/default:


location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}



RE: Nginx serves .php files as downloads, instead of executing them - grasps288881 - 07-26-2023

My solution was to add

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

to my custom configuration file, for example `etc/nginx/sites-available/example.com.conf`

Adding to `/etc/nginx/sites-available/default` didn't work for me.


RE: Nginx serves .php files as downloads, instead of executing them - lieproofswce - 07-26-2023

I had the same issue and none of the answers solved the problem.

I ran:

sudo nginx -t

to test the config file at /etc/nginx/sites-available/default.

It gave me these errors:

nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:115
nginx: configuration file /etc/nginx/nginx.conf test failed

So I went into the config file and on the last line there was

#}

I uncommented, ran the test command again and it worked