Nginx download “*.php” instead of display it contents in a Docker environment

dockernginxphp-fpm

I am working in a new Docker environment running PHP71 + Nginx. The Dockerfile is inherit from million12/docker-nginx.

This is the content of /etc/nginx/hosts.d/vhost.conf (which I took from here):

server {
    listen      80  default_server;
    listen      81  default_server http2 proxy_protocol; ## Needed when behind HAProxy with SSL termination + HTTP/2 support
    listen      443 default_server ssl http2;

    ssl_certificate       /etc/nginx/ssl/dummy.crt;
    ssl_certificate_key   /etc/nginx/ssl/dummy.key;

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

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    include     /etc/nginx/conf.d/stub-status.conf;
    include     /etc/nginx/conf.d/default-*.conf;
    include     /data/conf/nginx/conf.d/default-*.conf;
}

I can't find why the file is downloaded instead of display it's content.

Inside the container this is what I have at /var/run:

# ls -la /var/run/
total 8
drwxr-xr-x 11 root root 185 Dec 14 21:12 .
drwxr-xr-x 19 root root 299 Dec 14 21:12 ..
drwxr-xr-x  2 root root   6 Sep  5 10:19 blkid
drwxr-xr-x  2 root root   6 Jul 29 14:05 console
drwxr-xr-x  2 root root   6 Jul 29 14:05 faillock
drwxr-xr-x  4 root root  35 Jul 29 14:05 lock
drwxr-xr-x  2 root root   6 Jul 29 14:05 log
-rw-r--r--  1 root root   2 Dec 14 21:12 php-fpm.pid
drwxr-xr-x  2 root root   6 Jul 29 14:05 sepermit
drwxr-xr-x  2 root root   6 Jul 29 14:05 setrans
-rw-r--r--  1 root root   3 Dec 14 21:12 supervisord.pid
drwxr-xr-x  9 root root 113 Jul 29 14:05 systemd
drwxr-xr-x  2 root root   6 Jul 29 14:05 user
-rw-rw-r--  1 root utmp   0 Jul 29 14:05 utmp

Maybe this line:

 fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;

Isn't working properly. Changing it to:

fastcgi_pass    php-upstream;

Where php-upstream is:

upstream php-upstream {
    server 127.0.0.1:9000;
}

Doesn't work either. I am stuck at this point. I have checked all of this posts before open another a new one:

Can any give me some help on this one? Remember all this is running inside a Docker container so maybe some commands aren't available.

Best Answer

Here is the php-fpm checklist that I use.

  1. Make sure php-fpm is installed and running

    sudo systemctl status php-fpm
    
  2. Check the listen directive in php-fpm configuration

    sudo cat /etc/php-fpm.d/www.conf | grep -Ei '^listen'
    

    and make sure that the listen directive matches the upstream that you have set in your nginx configuration

  3. If using unix sockets, make sure the socket is readable/writable by the php-fpm user (php-fpm user is set by the user and group directives in www.conf). If using TCP/IP socket, check to make sure php-fpm is listening on the port with:

    sudo lsof -i :9000 # or whatever the port number you have specified