Skip to content

phpMyAdmin NixOS container setup

Introduction

phpMyAdmin is a free web-based software tool designed to manage and administer MariaDB. It provides a user-friendly interface for various database management tasks, making it easier to work with MariaDB databases.

Setup

  1. Add virtualisation to configuration.nix

    Add virtualisation and the import to a seperate nix file for the container to configuration.nix:

    /etc/nixos/configuration.nix
    # To edit use your text editor application, for example Nano
    virtualisation = {
    podman = {
    enable = true;
    dockerCompat = true; # Create a `docker` alias for podman, to use it as a drop-in replacement
    defaultNetwork.settings.dns_enabled = true; # release 23.05
    };
    oci-containers = {
    backend = "podman";
    containers = {
    phpmyadmin = import ./containers/phpmyadmin.nix;
    };
    };
    };
  2. Add the macvlan network to configuration.nix

    The container will use a macvlan network (net_macvlan) with a dedicated IP address. Add the following to configuration.nix:

    /etc/nixos/configuration.nix
    # To edit use your text editor application, for example Nano
    systemd.services.create-podman-network = with config.virtualisation.oci-containers; {
    serviceConfig.Type = "oneshot";
    wantedBy = [ "${backend}-phpmyadmin.service" ];
    script = ''${pkgs.podman}/bin/podman network exists net_macvlan || \ ${pkgs.podman}/bin/podman network create --driver=macvlan --gateway=192.168.1.1 --subnet=192.168.1.0/24 -o parent=ens18 net_macvlan'';
    };
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace 192.168.1.1 with your gateway IP address
    • Required Replace 192.168.1.0 with your subnet
    • Required Replace ens18 with the name of own network interface
  3. Add a script to create folders to configuration.nix

    Make sure the folders for use with the container are created by adding the following to configuration.nix:

    /etc/nixos/configuration.nix
    # To edit use your text editor application, for example Nano
    system.activationScripts = {
    script.text = ''
    install -d -m 755 /home/<username>/phpmyadmin -o root -g root
    test -f /home/<username>/phpmyadmin/config.user.inc.php || echo -e "<?php\n\n\$cfg['ShowPhpInfo'] = true; // Adds a link to phpinfo() on the home page\n\$cfg['SendErrorReports'] = 'never';" > /home/<username>/phpmyadmin/config.user.inc.php
    '';
    };
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace <username> with your NixOS username
    • Optional The setting $cfg['ShowPhpInfo'] = true is used as example entry in the config.user.inc.php. You can remove this entry or adjust it to false
    • Optional You can remove the setting $cfg['SendErrorReports'] = 'never' or adjust it to ask or always
  4. Create the containers folder

    Run the following command:

    # Open your terminal application
    mkdir -p /etc/nixos/containers # Make sure the directory exists
  5. Add the container configuration to phpmyadmin.nix

    Add the following to phpmyadmin.nix:

    /etc/nixos/containers/phpmyadmin.nix
    # To edit use your text editor application, for example Nano
    {
    image = "phpmyadmin:latest";
    environment = {
    "TZ" = "Europe/Amsterdam";
    "MYSQL_ROOT_PASSWORD" = "<password>";
    "PMA_HOST" = "<DB IP address>";
    "PMA_ABSOLUTE_URI" = "https://phpmyadmin.home.arpa";
    "UPLOAD_LIMIT" = "10M";
    };
    volumes = [
    "/home/<username>/phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php"
    ];
    extraOptions = [
    "--pull=newer" # Pull if the image on the registry is newer than the one in the local containers storage
    "--name=phpmyadmin"
    "--hostname=phpmyadmin"
    "--network=net_macvlan"
    "--ip=<IP address>"
    "--mac-address=<MAC address>"
    ];
    dependsOn = [ "mariadb" ];
    }
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace Europe/Amsterdam with your own timezone
    • Required Replace <password> with your MariaDB root password
    • Required Replace <DB IP address> with the IP address of your database instance, for example MariaDB
    • Optional Replace https://phpmyadmin.home.arpa with your domain name. Only needed when running a reverse proxy
    • Optional Replace 10M with the upload limit. The default value is 2048K. I always set it a little higher so that I can import SQL files without problems
    • Required Replace <username> with your NixOS username
    • Optional Replace --pull=newer with --pull=never if you do not want the image to be automatically replaced by new versions
    • Optional Replace net_macvlan with the name of your macvlan network if needed
    • Required Replace <IP address> with the IP address of this container. Make sure it is within the range of the macvlan network
    • Required Replace <MAC address> a (randomly generated) MAC address. Otherwise, every time the container is started, a new mac address will be used, which for example will be created as a new device within the Unifi Network Application. Or temporarily disable this option, and add the MAC address that is generated the first time when this container is started. Use inspect to get the MAC address if needed: sudo podman inspect <container name> |grep MacAddress|tr -d ' ,"'|sort -u
    • Optional The setting dependsOn = [ "mariadb" ]; is added because this container depends on the MariaDB NixOS container
  6. Switch NixOS configuration

    Now you can switch to the new configuration within NixOS, the image will be downloaded and the container will be created:

    Run the following command:

    # Open your terminal application
    sudo nix-collect-garbage # Optional: clean up
    sudo nixos-rebuild switch
  7. Check the results

    Run the following command to check if the container is working properly:

    # Open your terminal application
    journalctl -u podman-phpmyadmin.service

    Now you can browse to the phpMyAdmin web interface by opening a web browser and going to: http://localhost. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier.

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact