Skip to content

[Question] Serving fpm-alpine image with nginx #253

Open

Description

@lonix1

I am trying to get the fpm-alpine image to work with nginx.

docker-compose.yml

version: "3.7"

networks:
  mynet:

volumes:
  mariadb_data:
  phpmyadmin_data:

services:

  mariadb:
    image: mariadb:10.4.8-bionic
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
    volumes:
      - mariadb_data:/var/lib/mysql/
    networks:
      - mynet

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:fpm-alpine
    volumes:
      - phpmyadmin_data:/var/www/html/
    networks:
      - mynet
    depends_on:
      - mariadb

  nginx:
    image: nginx:1.17.4-alpine
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf:ro
      - phpmyadmin_data:/var/www/html/:ro
    ports:
      - "80:80"
    networks:
      - mynet
    depends_on:
      - mariadb
      - phpmyadmin

default.conf

resolver 127.0.0.11 valid=15s;

server {

  listen 80;
  server_name www.example.com;

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

  set $upstream phpmyadmin:9000;

  location ~ \/pma {
    fastcgi_pass $upstream;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

Then: docker-compose up.

When I access http://www.example.com/pma I get

502 Bad Gateway

I also tried to get it working using a port instead of path, i.e. http://www.example.com:8080 instead of http://www.example.com/pma.

The docs show examples using traefik and haproxy, but not for nginx.

Can someone spot the error in my setup, or does someone have a working example to share?

Activity

  1. williamdes commented on Oct 18, 2019

    @williamdes
    Member

    @lonix1 try port 9000 on host phpmyadmin for fastcgi_pass since you are sending the script to the cgi and not to the web server

    If it does not work comment back here :)

  2. lonix1 commented on Oct 18, 2019

    @lonix1
    Author

    @williamdes Thanks - yes you are right it should be 9000! I edited it above.

    However, it still does not work :-(

    curl http://www.example.com/pma gives:

    File not found.

    The docker log shows:

    "GET /pma/index.php" 404

  3. williamdes commented on Oct 18, 2019

    @williamdes
    Member

    maybe you should change /var/www/html to /usr/src/phpmyadmin

    See: https://github.com/phpmyadmin/docker/blob/master/fpm-alpine/Dockerfile#L77

    I assume you have data into the volume phpmyadmin_data

    https://superuser.com/a/435969/609233
    I would write the nginx config block like that

      location ~ \/pma {
        rewrite ^/pma(/.*)$ $1 last;
        fastcgi_pass $upstream;
        fastcgi_index index.php;
        include snippets/fastcgi-php.conf;
        # remove line below ?
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }

    You are maybe missing the rewrite part since the files are in the root folder and not in /pma

  4. lonix1 commented on Oct 18, 2019

    @lonix1
    Author

    When I do docker-compose up for the first time, it says

    phpMyAdmin not found in /var/www/html - copying now...
    Complete! phpMyAdmin has been successfully copied to /var/www/html

    That's why I used that path. But even if I use /usr/src/phpmyadmin it fails with same error. I checked that there are files in there, looks okay.

    I tried the new location block also. Same error (file not found).

    Does that actually work for you?


    I will carry on fiddling to get it working. Maybe we can then add it to the docs.

  5. williamdes commented on Oct 18, 2019

    @williamdes
    Member

    After hours (since my last post) I could not make a rewrite folder that worked.

    Light version

    resolver 127.0.0.11 valid=15s;
    server {
        listen 80;
        server_name williamdes.local default_server;
        root /var/www/html/;
        index index.php index.html index.htm;
        set $upstream phpmyadmin:9000;
    
        location ~ \.php$ {
            try_files $uri = 404;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    version: "3.7"
    
    networks:
      mynet:
    
    volumes:
      mariadb_data:
      phpmyadmin_data:
    
    services:
    
      mariadb:
        image: mariadb:10.4.8-bionic
        environment:
          MYSQL_DATABASE: mydb
          MYSQL_ROOT_PASSWORD: password
          MYSQL_USER: admin
          MYSQL_PASSWORD: password
        volumes:
          - mariadb_data:/var/lib/mysql/
        networks:
          - mynet
    
      phpmyadmin:
        image: phpmyadmin/phpmyadmin:fpm-alpine
        environment:
          PMA_HOST: mariadb
        volumes:
          - phpmyadmin_data:/var/www/html/
        networks:
          - mynet
        depends_on:
          - mariadb
    
      nginx:
        image: nginx:1.17.4-alpine
        volumes:
          - ./default.conf:/etc/nginx/conf.d/default.conf:ro
          - phpmyadmin_data:/var/www/html/:ro
        ports:
          - "90:80"
        networks:
          - mynet
        depends_on:
          - mariadb
          - phpmyadmin
  6. williamdes commented on Oct 18, 2019

    @williamdes
    Member

    Using a proxy

    resolver 127.0.0.11 valid=15s;
    server {
        listen 80;
        server_name williamdes.local default_server;
        root /var/www/html/;
        index index.php index.html index.htm;
        set $upstream phpmyadmin:9000;
    
        location ^~ /pma {
            rewrite /pma/(.*) /$1 last;
            proxy_pass http://localhost:80;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
        }
    
        location ~ \.php$ {
            try_files $uri = 404;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    

    Using CGI

    Thanks to: https://stackoverflow.com/a/29213330/5155484

    resolver 127.0.0.11 valid=15s;
    server {
        listen 80;
        server_name williamdes.local default_server;
        root /var/www/html/;
        index index.php index.html index.htm;
        set $upstream phpmyadmin:9000;
    
        location ^~ /pma {
            rewrite /pma/(.*) /$1 last;
            try_files $uri = 404;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
        location ~ \.php$ {
            try_files $uri = 404;
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

    @lonix1 the CGI version is what you want

  7. lonix1 commented on Oct 19, 2019

    @lonix1
    Author

    @williamdes wow thanks! I used the "Using CGI" section, and got it working finally, but on the wrong path:

    curl http://www.example.com/pma gives 404
    but
    curl http://www.example.com/ gives the phpmyadmin login page


    BTW, I assume your "Using a proxy" section is for the latest image, and "Using CGI" is for the fpm-alpine and fpm images?

  8. williamdes commented on Oct 19, 2019

    @williamdes
    Member

    I think you are correct on what you assume

    @lonix1 can you paste your final configuration?

    I made it work with the ^~ and position can be important from what Internet says

  9. lonix1 commented on Oct 20, 2019

    @lonix1
    Author

    Okay I got it to work, thanks to this!

    Notes:

    • No rewriting necessary
    • I did not need to set PMA_ABSOLUTE_URI - not sure why?
    resolver 127.0.0.11 valid=15s;
    
    server {
    
      listen 80;
      server_name www.example.com example.com;
      set $upstream phpmyadmin:9000;
    
      location ^~ /phpmyadmin {
        alias /var/www/html/;
        index index.php;
        location ~ \.php$ {
          try_files $uri = 404;
          fastcgi_pass $upstream;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
        }
      }
    
    }

    This should be in the docs. If someone knows how to do that, please add @williamdes' code for latest image and my code for fpm-alpine and fpm images.

  10. lonix1 commented on Oct 20, 2019

    @lonix1
    Author

    @williamdes BTW thanks for taking the time to help me... Appreciated!

  11. williamdes commented on Oct 20, 2019

    @williamdes
    Member

    @lonix1 I think it is because all files need to know their base path?

  12. lonix1 commented on Oct 20, 2019

    @lonix1
    Author

    @williamdes Yes... so we need to correctly set SCRIPT_FILENAME.

  13. williamdes commented on Oct 20, 2019

    @williamdes
    Member

    @lonix1 please post the config when you have a working example ;)

  14. 25 remaining items

  15. pzystorm commented on Sep 1, 2020

    @pzystorm

    Argh... the problematic line is:

        volumes:
          - ./phpmyadmin/:/var/www/html/

    without that line, phpmyadmin is being extracted and inside /var/www/html/
    So, it seems, the mount of that directory is later than the Dockerfile RUN scripts are executed and downloading and extracting phpmyadmin.
    Is there any solution for that?

  16. williamdes commented on Sep 2, 2020

    @williamdes
    Member

    Maybe changing the nginx config block?
    Why do you want to have a volume?

  17. pzystorm commented on Sep 2, 2020

    @pzystorm

    Maybe changing the nginx config block?
    Why do you want to have a volume?

    a seperate nginx container and this phpmyadmin-fpm container need to share the document root with the phmyadmin contents, otherwise it will not work. fpm can only serve php files, so nginx need to handle css/js/html files (all other stuff)

  18. williamdes commented on Sep 2, 2020

    @williamdes
    Member

    I would suggest you to have a look to the configuration of @woosungchoi #299 (comment)

  19. pzystorm commented on Sep 2, 2020

    @pzystorm

    it's the same. because of the volume (which is mounted when starting and not when building), the folder /var/www/html is empty at first startup

        volumes:
          - phpmyadmin:/var/www/html
    

    The container itself is ok. So the only thing to do is to rerun the steps from the Dockerfile:
    https://github.com/phpmyadmin/docker/blob/master/fpm-alpine/Dockerfile#L78-L101

  20. williamdes commented on Sep 2, 2020

    @williamdes
    Member

    I understand, you are talking about #284 (comment) on #285

  21. pzystorm commented on Oct 17, 2020

    @pzystorm

    Please have a look f.ex. at the nextcloud project and get some ideas there.
    They are doing all the magic inside the docker-entrypoint.sh file: First extracting the source files to /usr/src/nextcloud and then doing some rsync to /var/www/html
    https://github.com/nextcloud/docker/blob/master/docker-entrypoint.sh

    You could do the same.
    There is no need to do all the magic inside the Dockerfile :)

  22. williamdes commented on Oct 17, 2020

    @williamdes
    Member

    Hi @pzystorm
    Could you open a pull-request here so we can discuss and review the change you need please?

  23. osteel commented on Oct 23, 2020

    @osteel

    @pzystorm do you also have an Nginx service in your docker-compose.yml file, that is using the same phpmyadmin volume?

    If that is the case, it's possible that the Nginx container is the first to start, initialising the volume as an empty folder. If you make sure the phpMyAdmin container starts first with depends_on, that might fix your issue:

    version: "3.7"
    
    networks:
      pma_net:
    
    services:
    
      nginx:
        ...
        volumes:
          - ./phpmyadmin/:...
        ...
        depends_on:
          - pma_php
    
      pma_php:
        image: phpmyadmin/phpmyadmin:fpm-alpine
        container_name: pma_php
        volumes:
          - ./phpmyadmin/:/var/www/html/
          - ./sessions/:/sessions/
        networks:
          - pma_net
  24. capitalfuse commented on Jan 21, 2021

    @capitalfuse

    Working correctly phpmyadmin-fpm container with nginx container, if volumes sections of these are set to "phpmyadmin: /var/www/html" (NOT ./phpmyadmin: /var/www/html). But in advance, need to create the external volume by the following command: "$ docker volume create --name=phpmyadmin".

  25. SergeShkurko commented on Jan 22, 2021

    @SergeShkurko

    Site located at /* and phpmyadmin located at /phpmyadmin
    phpMyAdmin location /var/www/html/ reqired
    other site can be located at /var/www/site_name

    I resolve problem with same configs:

    version: '3.9'
    
    volumes:
      phpmyadmin_data:
    
    services:
      web:
        image: nginx:alpine
        container_name: 'hostinpl-web'
        ports:
          - '${HOST_MACHINE_UNSECURE_HOST_PORT}:80'
          - '${HOST_MACHINE_SECURE_HOST_PORT}:443'
        working_dir: /var/www/
        volumes:
          - phpmyadmin_data:/var/www/html/ # target pma location at html dir required!
          - ./:/var/www/panel/ # another site should be located in /var/www
          - ${PHP_STORAGE_DIR-./docker/data/php}:/var/www/storage
          - ./docker/config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
          - ${NGINX_CONFIG-./docker/config/nginx/default.conf}:/etc/nginx/conf.d/default.conf:ro
          - ${NGINX_LOG_DIR-./docker/logs/nginx}:/var/log/nginx/
        depends_on:
          - php
          - phpmyadmin
      php:
        build: 
          context: .
          dockerfile: ./docker/bin/php/Dockerfile # php:7.4.14-fpm
        container_name: 'hostinpl-php'
        working_dir: /var/www/panel/
        depends_on:
          - database
        volumes: 
          - ./:/var/www/panel/ # another site location
          - /var/www/panel/vendor/
          - ${PHP_STORAGE_DIR-./docker/data/php}:/var/www/storage
          - ${PHP_INI-./docker/config/php/php.ini}:/usr/local/etc/php/php.ini
          - ${PHP_LOG_DIR-./docker/logs/php}:/var/log/php
      database:
        image: mariadb:10.3
        command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8 --collation-server=utf8_general_ci
        container_name: 'hostinpl-database'
        ports:
          - '127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306'
        volumes: 
          - ${MYSQL_DATA_DIR-./docker/data/mysql}:/var/lib/mysql
          - ${MYSQL_LOG_DIR-./docker/logs/mysql}:/var/log/mysql
          - ${MYSQL_DUMP-./docker/dump.sql}:/docker-entrypoint-initdb.d/dump.sql
        environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      phpmyadmin:
        image: phpmyadmin/phpmyadmin:fpm-alpine
        container_name: 'hostinpl-phpmyadmin'
        depends_on:
          - database
        environment:
          PMA_HOST: database
          PMA_PORT: 3306
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
        volumes: 
          - phpmyadmin_data:/var/www/html/ # target pma location at html dir required!
          - /sessions
          - ${PHP_INI-./docker/config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini

    default.conf

    server {
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /var/www/;
    
        location ^~ /phpmyadmin/ {
            gzip_static on;
            alias /var/www/html/;
            index index.php;
    
            location ~ \.php$ {
                try_files $uri = 404;
                fastcgi_pass phpmyadmin:9000;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
        }
    
        location / {
            gzip_static on;
            root /var/www/panel/public/;
            index index.php;
    	try_files $uri $uri/ /index.php$is_args$args; 
    
            location ~ \.php$ {
                fastcgi_pass php:9000;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
        }
    
        location /storage {
            alias /var/www/storage/;
        }
    }
  26. gitown commented on Sep 15, 2021

    @gitown

    when I run my docker compose file I'm getting this weird rendering of phpmyadmin:fpm-alpine, I tried different web browsers and clearing the cache but the issue is the same:
    image

    Here is my docker-compose.yml:

    version: "3.9"
    
    services:
      aspnetcoreapp:
        container_name: aspnetcoreapp
        build:
          context: ./aspnetcoreapp
          dockerfile: Dockerfile
        expose:
          - "5080"
        restart: always
        depends_on:
          - mysql
        networks:
          - mynet
    
      nginx:
        container_name: nginx
        build:
          context: ./nginx
          dockerfile: Dockerfile
        volumes:
          - ./src:/var/www/php:ro
          - phpmyadmin_data:/var/www/html/:ro
        restart: always
        ports:
          - "60000:8282"
          - "60001:8383"
        depends_on:
          - aspnetcoreapp
          - phpmyadmin
          - mysql
        networks:
          - mynet
    
      mysql:
        container_name: mysql
        image: mysql:8.0.26
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: rootPass
          MYSQL_DATABASE: demoHello
        volumes:
          - sql_data:/var/lib/mysql
        ports:
          - 42333:3306
        networks:
          - mynet
    
      phpmyadmin:
        container_name: phpmyadmin
        image: phpmyadmin/phpmyadmin:fpm-alpine
        restart: always
        environment:
          PMA_USER: root
          PMA_PASSWORD: rootPass
          PMA_HOST: mysql
          MYSQL_ROOT_PASSWORD: rootPass
        expose:
          - "9000"
        depends_on:
          - mysql
        volumes:
          - phpmyadmin_data:/var/www/html/
        networks:
          - mynet
    
    volumes:
      sql_data:
      phpmyadmin_data:
    
    networks:
      mynet:
        driver: bridge

    And my Nginx Dockerfile:

    FROM nginx:1.21-alpine AS base
    
    ENV TERM xterm
    RUN apk update && apk add vim
    
    WORKDIR /
    COPY nginx.conf /etc/nginx/nginx.conf

    And finally my nginx.conf:

    worker_processes 5;
    
    events { worker_connections 1024; }
    
    http {
        sendfile on;
    
        upstream docker_aspnetcoreapp {
            server aspnetcoreapp:5080;
        }
    
        server {
            listen 8282;
            # server_name www.domain.com;
            location / {
                proxy_pass http://docker_aspnetcoreapp;
            }
        }
    
        resolver 127.0.0.11 valid=15s;
        
        server {
            listen 8383;
            #server_name williamdes.local default_server;
            root /var/www/html/;
            index index.php index.html index.htm;
            set $upstream phpmyadmin:9000;
    
            location ^~ /pma {
                rewrite /pma/(.*) /$1 last;
                try_files $uri = 404;
                fastcgi_pass $upstream;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
    
            location ~ \.php$ {
                try_files $uri = 404;
                fastcgi_pass $upstream;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
        }
    }

    Any idea what's causing this issue?

  27. williamdes commented on Sep 16, 2021

    @williamdes
    Member

    Any idea what's causing this issue?

    Since you are using the config I made, it should work. It could be the contents of the volume.
    Could you check the contents of the volume ? js/dist folder should exist and each theme should have css files in the css folder

  28. gitown commented on Sep 18, 2021

    @gitown

    Any idea what's causing this issue?

    Since you are using the config I made, it should work. It could be the contents of the volume.
    Could you check the contents of the volume ? js/dist folder should exist and each theme should have css files in the css folder

    Hi William,
    I found how to fix this, I just have to add this line to the http block in the nginx.conf file:
    include /etc/nginx/mime.types;

  29. williamdes commented on Sep 18, 2021

    @williamdes
    Member

    Any idea what's causing this issue?

    Since you are using the config I made, it should work. It could be the contents of the volume.
    Could you check the contents of the volume ? js/dist folder should exist and each theme should have css files in the css folder

    Hi William,
    I found how to fix this, I just have to add this line to the http block in the nginx.conf file:
    include /etc/nginx/mime.types;

    Thank you so much for letting me know and anyone searching about this !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions