0

The issue is: php files are downloaded instead of executing.

I know there are some other similar questions out there to this one. I read many of them but either their scenerios are different or they have no correct answers. I have spent days and tried many ways but still can't figure it out. Therefore I am asking for help. The following are the scenerio, together with the .yml .conf files.

network: nginx-proxy

nginx-proxy:
- nginx (proxy) container
- docker-gen container
- letsencrypt container

web1: (this works fine)
- mysql container
- wordpress container (from wordpress image)

web2: (this has the issue: php files downloaded instead of executing)
- mysql container
- phpfpm container
- nginx container (env: VIRTUAL_HOST, LETSENCRYPT_HOST)

As mentioned above, if I use a Wordpress image, it works successfully. No hassle at all.

However, when I try to set up one with php, nginx and mysql containers myself, those php files will be downloaded instead of executing. (html files can be executed)

create network: docker network create nginx-proxy

nginx-proxy: docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:1.13.1
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - ./sites-enabled:/etc/nginx/sites-enabled
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"

  dockergen:
    image: jwilder/docker-gen:0.7.3
    container_name: nginx-proxy-gen
    depends_on:
      - nginx
    command: -notify-sighup nginx-proxy -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro
      - ./sites-enabled:/etc/nginx/sites-enabled

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nginx-proxy-le
    depends_on:
      - nginx
      - dockergen
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
      NGINX_DOCKER_GEN_CONTAINER: nginx-proxy-gen
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./sites-enabled:/etc/nginx/sites-enabled

volumes:
  conf:
  vhost:
  html:
  certs:

networks:
  default:
    external:
      name: nginx-proxy

nginx.conf

# web1.local
upstream web1.local {
                ## Can be connected with "nginx-proxy" network
            # wordpress_web1
            server 192.168.96.3:80;
}
server {
    server_name web1.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    include /etc/nginx/vhost.d/default;
    location / {
        proxy_pass http://web1.local;
    }
}
server {
    server_name web1.local;
    listen 443 ssl http2 ;
    access_log /var/log/nginx/access.log vhost;
    return 500;
    ssl_certificate /etc/nginx/certs/default.crt;
    ssl_certificate_key /etc/nginx/certs/default.key;
}
# web2.local
upstream web2.local {
                ## Can be connected with "nginx-proxy" network
            # nginx_web2
            server 192.168.96.7:80;
}
server {
    server_name web2.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://web2.local;
    }
}
server {
    server_name web2.local;
    listen 443 ssl http2 ;
    access_log /var/log/nginx/access.log vhost;
    return 500;
    ssl_certificate /etc/nginx/certs/default.crt;
    ssl_certificate_key /etc/nginx/certs/default.key;
}

sites-enabled/web2.conf

(I also tried to put this part in default.conf but it didn't work)

server {
    index index.php index.html index.htm;
    root /var/www/html;    
    location ~ \.php$ {
        try_files $uri $uri/ /index.php$is_args$args;
        fastcgi_pass   phpfpm_web2:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
}

apps:

web1 (Wordpress): docker-compose.yml (This works fine)

version: "3"
services:
   mysql_web1:
     container_name: mysql_web1
     image: mysql:5.7
     restart: always
     volumes:
        - db_data_web1:/var/lib/mysql
     environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: db1
        MYSQL_USER: user1
        MYSQL_PASSWORD: password

   wordpress_web1:
     container_name: wordpress_web1
     image: wordpress:latest
     restart: always
     expose:
        - 80
     depends_on:
        - mysql_web1
     environment:
        VIRTUAL_HOST: web1.local
        LETSENCRYPT_HOST: web1.local
        LETSENCRYPT_EMAIL: foo@web1.local
        WORDPRESS_DB_HOST: mysql_web1:3306
        WORDPRESS_DB_USER: user1
        WORDPRESS_DB_PASSWORD: password

volumes:
  db_data_web1:

networks:
  default:
    external:
      name: nginx-proxy

web2: docker-compose.yml

version: "3"
services:
   mysql_web2:
      container_name: mysql_web2
      image: mysql:5.7
      restart: always
      ports:
         - 3306
      expose:
         - 3306
      volumes:
         - db_data_web2:/var/lib/mysql
      environment:
         MYSQL_ROOT_PASSWORD: password
         MYSQL_DATABASE: db2
         MYSQL_USER: user2
         MYSQL_PASSWORD: password

   phpfpm_web2:
      container_name: phpfpm_web2
      image: php-fpm:latest
      restart: always
      ports:
         - 9000
      expose:
         - 9000
      links:
         - mysql_web2
      depends_on:
         - mysql_web2
      volumes: 
         - ./code:/var/www/html

   nginx_web2:
      container_name: nginx_web2
      image: nginx:1.13.1
      restart: always
      ports: 
         - 80
      expose: 
         - 80
      links: 
         - phpfpm_web2
      depends_on: 
         - phpfpm_web2
      volumes: 
         - ./code:/usr/share/nginx/html
      environment:
         VIRTUAL_HOST: web2.local
         VIRTUAL_PORT: 80
         LETSENCRYPT_HOST: web2.local
         LETSENCRYPT_EMAIL: foo@web2.local

volumes:
   db_data_web2:

networks:
   default:
      external:
         name: nginx-proxy

in php-fpm.d/www.conf, I tried the following ways, but none of them worked.

  1. listen = 127.0.0.1:9000
  2. listen = php_container_ipaddress:9000
  3. listen = 9000

Many thanks in advance for your help!

redstone
  • 63
  • 1
  • 8
  • It would be useful to see Wordpress Docker script for comparison, perhaps you can figure it from comparing them yourself. Otherwise please add logs from you second web container. – Daniel Protopopov May 25 '20 at 05:29
  • Hi Daniel, I just added the Wordpress docker-compose.yml. Can you please have a look again? I tried wordpress first and it worked so I followed it's docker-compose file to set up my own with nginx + phpfpm + mysql containers. Regarding logs, I mounted the logs to local but it showed nothing when the php files got downloaded. – redstone May 25 '20 at 06:10

0 Answers0