Home > Software design >  Docker compose nginx phpmyadmin seperate hostname
Docker compose nginx phpmyadmin seperate hostname

Time:11-20

I am trying to set up a docker environment with nginx, php, mysql, phpmyadmin, i would like to know how i can configure it so i can access phpmyadmin via another hostname.

For example to access the site i want to use site.com and to access phpmyadmin i want use database.site.com

my /etc/hosts is configured like this

127.0.0.1      localhost example.com database.example.com

My docker composer looks like this

version: '3'
services:
    web:
        image: nginx:alpine
        container_name: nginx
        volumes:
            - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
            - "./etc/ssl:/etc/ssl"
            - "./repo:/var/www/html"
            - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
        ports:
            - "80:80"
            - "443:443"
        environment:
            - NGINX_HOST=site.com
        networks:
            - my-network
        command: /bin/sh -c "envsubst '$site.com' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
        restart: always
        depends_on:
            - php
            - mysql
    php:
        image: bitnami/php-fpm:8.0.10
        container_name: php
        restart: always
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - "./repo:/var/www/html"
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin
        ports:
            - "8080:80"
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=mysql
        networks:
            - my-network
        restart: always
        depends_on:
            - mysql
    mysql:
        image: mysql:8.0.27
        container_name: mysql
        restart: always
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=database
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_USER=admin
            - MYSQL_PASSWORD=admin
        ports:
            - "8989:3306"
        volumes:
            - "./data/db/mysql:/var/lib/mysql"
networks:
  my-network: null

my nginx conf looks like this

server {
    listen 80;
    server_name example.com;
    root /var/www/html/public;
    ...
    location ~ \.php$ {
      fastcgi_split_path_info ^(. \.php)(/. )$;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
      fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
    ...
 }

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;
    root /var/www/html/public;
    ...
    location / {
      if ($args ~* _escaped_fragment_) {
        # Pass to phantom proxy
        proxy_pass http://127.0.0.1:443;
      }
      proxy_redirect off;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Client-Verify SUCCESS;
      proxy_set_header X-SSL-Subject $ssl_client_s_dn;
      proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
      proxy_set_header HTTPS on;
      proxy_buffers 16 4k;
      proxy_buffer_size 2k;
      proxy_pass http://127.0.0.1:433;
    }
    ...
 }

server {
    listen 80;
    server_name database.example.com;
    location / {       
         proxy_pass  http://phpmyadmin:80;
     }
 }

CodePudding user response:

Don't use site.com as an example for this reason. I have changed everything to example.com

  1. Change your port bindings so that nginx is binding the correct ports from your host machine 443:443 and 80:80 (instead of 9080:80 and 3000:443). Otherwise your web requests will need to include the port, e.g. https://example.com:3000.
  2. Connect your containers together with a common network (see here).
  3. Update your nginx config to proxy the traffic to your phpmyadmin container. It should be something like this:
...
server {
  listen        80;
  server_name   database.example.com;

  location / {
    proxy_pass  http://phpmyadmin;
  }
}
...

This should result in the following:

  • Request to http://database.example.com resolves to 127.0.0.1.
  • Web browser makes the request on port 80.
  • 127.0.0.1:80 is forwarded to port 80 in your nginx container.
  • nginx proxies the traffic to http://phpmyadmin, which it is able to resolve and connect to through the docker network.

You should be able to get https://database.example.com and the website working fairly easily after that. I believe https://example.com should work fine once you fix your port bindings. To fix the http://example.com endpoint, change your listener from 127.0.0.1:9080 to just 80 in the nginx configuration.

  • Related