Reverse proxy phpmyadmin through apache with a PHP container

Viewed 37

I'm trying to setup a docker environment with the the following containers: apache, php, mysql, phpmyadmin.

I have a problem making both phpmyadmin and my applications work at the same time, and I was wondering if it wasn't because I use a PHP container to run my PHP instead of phpmyadmin that also have PHP installed.

I understood that phpmyadmin runs with PHP (hence the name), though I was wondering if it was possible to have a docker image without PHP and then to proxy the requests to php files from phpmyadmin to the php container.

I've already established a reverse proxy from apache to phpmyadmin because I don't need phpmyadmin to run it's own apache server, and I would like to do the same with PHP. I tried to proxy php requests from apache to my php container, but I doesn't work.

Here is my full compose.yaml

version: '3.8'

services:
  php:
    build:
      context: './php/'
      args:
        - PHP_VERSION=${PHP_VERSION}
    volumes:
      - "${APPS_VOLUME:-apps}:/var/www/html/"
    networks:
      - backend
    restart: unless-stopped
    stdin_open: true
    tty: true
    container_name: php
  apache:
    build:
      context: './apache/'
      args:
        - APACHE_VERSION=${APACHE_VERSION}
    ports:
      - '${APACHE_PORT:-8000}:80'
    volumes:
      - "${APPS_VOLUME:-apps}:/usr/local/apache2/htdocs/"
      - "pma:/usr/local/apache2/htdocs/phpmyadmin/"
    networks:
      - frontend
      - backend
    depends_on:
      - php
      - mysql
    working_dir: /usr/local/apache2/htdocs/
    restart: unless-stopped
    container_name: apache
  mysql:
    image: mysql:${MYSQL_VERSION}
    ports:
      - '${MYSQL_PORT:-3307}:3306'
    volumes:
      - "db-data:/var/lib/mysql"
    networks:
      - backend
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    command: --federated
    working_dir: /usr/
    restart: unless-stopped
    container_name: mysql
  phpmyadmin:
    image: phpmyadmin:${PHPMYADMIN_VERSION:-latest}
    volumes:
      - "pma:/var/www/html/"
    networks:
      - backend
    environment:
      - PMA_HOST=mysql
      - PMA_ABSOLUTE_URI=http://localhost:${APACHE_PORT:-8000}/phpMyAdmin/
    restart: unless-stopped
    container_name: phpmyadmin

volumes:
  apps:
    name: apps
  db-data:
    name: db-data
  pma:
    name: pma

networks:
  frontend:
    name: frontend
  backend:
    name: backend

And my apache conf

ServerName localhost

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so

<VirtualHost *:80>
    # Proxy .php requests to port 9000 of the php-fpm container
    #ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://php:9000/var/www/html/"
    </FilesMatch>
    DocumentRoot /usr/local/apache2/htdocs/
    <Directory /usr/local/apache2/htdocs/>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Proxy phpmyadmin request to port 80 of the phpmyadmin container
    ProxyPass "/phpMyAdmin/" "http://phpmyadmin/"
    ProxyPassReverse /phpMyAdmin/ http://phpmyadmin/

    # Send apache logs to stdout and stderr
    CustomLog /proc/self/fd/1 common
    ErrorLog /proc/self/fd/2
</VirtualHost>

Is my configuration wrong? Do I have to use the phpmyadmin container to handle PHP requests or is there a way to separate them?

Thanks

Edit: Changed the title of the issue after Nigel Ren observation


I seem to have found a solution, I don't know if it is how I'm supposed to do but I can now use the phpmyadmin interface and open my apps. There's still some problems with both of them, but I'm not sure if it have anything to do with the apache configuration

I used the fpm-alpine image of phpmyadmin instead of the normal one and changed my conf file like so:

ServerName localhost

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so
LoadModule proxy_http_module /usr/local/apache2/modules/mod_proxy_http.so

<VirtualHost *:80>

    # Proxy phpmyadmin request to the phpmyadmin container
    ProxyPassMatch ^/phpMyAdmin/(.*\.php(/.*)?)$ fcgi://phpmyadmin:9000/var/www/html/$1
    ProxyPassReverse /phpMyAdmin/ fcgi://phpmyadmin:9000/var/www/html/

    # Proxy .php requests to port 9000 of the php-fpm container
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
    ProxyPassReverse / fcgi://php:9000/var/www/html/$1

    DocumentRoot /usr/local/apache2/htdocs/
    <Directory /usr/local/apache2/htdocs/>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Send apache logs to stdout and stderr
    CustomLog /proc/self/fd/1 common
    ErrorLog /proc/self/fd/2
</VirtualHost>

I'm not sure the ProxyPassReverse are required as I seem to have the same result without them.


I just realized that phpmyadmin is simply an app. The docker image just provides you the latest version along with an environment to make it ready to use, but you can really just copy the source files into your apps directory and parse the php files with your php container like you do for any other app.

So to answer one of my first questions, you don't need to have both containers because you don't even need the phpmyadmin image.

I feel stupid.

Hope it helps someone so I can at least feel like it was not all in vain.

0 Answers
Related