0

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

Nginx Version

lab-1:/etc/php7# nginx -v
nginx version: nginx/1.14.0

rc-status

lab-1:/var/www/localhost/htdocs# rc-status
Dynamic Runlevel: manual
 nginx        [  started  ]
 php-fpm7     [  started  ]

Folder Structure

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

Configuration Files

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

ps -A

4268 root      0:00 {php-fpm7} php-fpm: master process (/etc/php7/php-fpm.conf)
4275 nginx     0:00 {php-fpm7} php-fpm: pool www
4276 nginx     0:00 {php-fpm7} 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

Comments

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)

Questions

  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.
5

3 Answers 3

1

I have not used "alpine linux" so please double check that both your cofig files are loaded.

For example place a syntax mistake in the config and run nginx -t if it catches it.

Try to avoid placing a root directory within one config to a subdirectory of another config.

Try to place them:

  • root /var/www/localhost; for /etc/nginx/conf.d/default.conf
  • root /var/www/widgets; for /etc/nginx/conf.d/widgets.conf

Please know that server_name directive needs to be a resolvable DNS or hosts entry (in /etc/hosts) in order for server block to be served properly.

For your 2nd part of the question try to look at https://linuxconfig.org/basic-php-7-and-nginx-configuration-on-ubuntu-16-04-linux

My advice is to try to read up on nginx config directives to get basic understanding what they mean.

2
  • hi there. thanks for responding roman. I have made sure there are no syntax errors. i will chew on the suggestion to move things out from being subdirectories. didn't consider that before. As far DNS, there is no DNS name associated with this IP address. it's just a lab. and in my /etc/hosts file, i just have mapped 127.0.0.1 to localhost. Anything else I should add to /etc/hosts?
    – dot
    Sep 13, 2018 at 20:58
  • 1
    In my opinion your php code is served within the server block defined under /etc/nginx/conf.d/default.conf which is "not PHP ready" because the client that is accessing the server does not addresses it as widgets but as localhost or directly via IP address. To have the server_name directives in order you will need the following entry in the client's /etc/hosts 127.0.0.1 widgets or 192.168.4.21 widgets (change that IP to the server IP) when accessing the site from "outside" the localhost. Verify the server block used by looking into your log files - namely the access logs. Sep 14, 2018 at 10:53
1

based on the tip from Roman Spiak's comments, (that the widgets.conf maybe wasn't being read at all), I just added another location stanza in the default.conf for now. And that has solved the problem with interpreting php files.

I'll have to read up on how to solve this for future cases. Ideally I'd like to be able have separate conf files for each application on lab servers... without associating a DNS name to the IP because realistically that won't happen for a lab.

default conf now looks like this: (with no changes the underlying folder structure)

location / {
    root /var/www/localhost/htdocs;
    location ~* \.php$ {
        if (!-f $document_root$fastcgi_script_name) { return 404; }
        include /etc/nginx/fastcgi.conf;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
       }
}
0

As per alexus link, replace try_files by

if (!-f $document_root$fastcgi_script_name) { return 404; }
1
  • hi there. just tried this but it didn't make a difference. no errors. was able to restart nginx ; but still downloading the PHP file.
    – dot
    Sep 14, 2018 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .