0

I found a similar question here: 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

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.

EDIT 1

I installed curl on my machine and tried to do this:

lab-1:/etc/php7# curl http://10.11.11.1111/widgets/info.php
<?php

     phpinfo();

?>

vs this:

lab-1:/etc/php7# curl http://widgets/index.html
curl: (6) Could not resolve host: widgets

But I don't quite know how to fix this. I want the system to always require that an IP address be specified and then the foldername / app name.

dot
  • 14,928
  • 41
  • 110
  • 218
  • This would be a better question for https://superuser.com or https://serverfault.com – Blue Sep 13 '18 at 18:14
  • ok. which one is better suited? I'll move it over... – dot Sep 13 '18 at 18:14
  • Read each of the descriptions, and decide for yourself. https://superuser.com/help/on-topic and https://serverfault.com/help/on-topic – Blue Sep 13 '18 at 18:15

2 Answers2

1

Have a look at the the documentation wrt server_name. server_name widgets; creates a virtual server called widgets, so it will expect an http request addressed at that server.

With myipaddress/widgets/info.php you will send a request at the server marked as "default_server" (as it's addressed via its ip address and not via a name), in your case the default, which has no support for php.

You can work around this by putting php support in the default block, but in my opinion that merely camouflages a dubious configuration.


So, based on your edit, I assume that you want the server to serve php pages on the IP address, you will want to add the location block from your widgets. conf file to the default server configuration block (above location /, otherwise that one would trap all requests for php files as it's defined first)

location ~* \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi.conf;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
}

and put the files for widgets in /var/www/localhost/widgets. That way they will be served as http://ipaddress/widgets/filename.php.

Note however that this should be considered a temporary workaround. Serving off an IP address is extremely limiting, and will among other things not allow you to set up an https website properly. But, to get up and running in dev it should help.

fvu
  • 32,488
  • 6
  • 61
  • 79
  • I did try to play around with the server name but couldn't figure it out/see problem. I will update my post to show you. But if i wanted to access the site as i do in the examples I gave how do i need to change the server_name value? Also can you explain why the system is correctly returning the right index.html if the server name is bad? – dot Sep 13 '18 at 18:22
  • please see EDIT 1. I think it demonstrates what you're trying to explain? I can recreate the problem but i don't know how to fix it. I've tried a few different things already – dot Sep 13 '18 at 18:31
0

Based on a tip 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;
       }
}
dot
  • 14,928
  • 41
  • 110
  • 218