Skip to content

PostfixAdmin for virtual domains — multi-domain mail with a real admin UI

Install PostfixAdmin against MariaDB, wire it to Postfix and Dovecot for virtual domains and mailboxes, and use the admin UI to manage tenants without editing config files.

PostfixAdmin is the long-running PHP admin UI for Postfix + Dovecot virtual-domain deployments. This article is the install we run when a client has more than one domain on a single Postfix and doesn’t want a docker-bundled stack like mailcow-self-hosted-stack — a MariaDB-backed schema, virtual domains and mailboxes managed via a web UI, and Postfix + Dovecot pulling identities from SQL.

How to verify

Before installing, check what is already wired.

dpkg -l | grep -E 'postfixadmin|postfix-mysql|dovecot-mysql|mariadb'
sudo postconf | grep -E 'virtual_mailbox|virtual_alias|virtual_transport'
sudo doveconf -n | grep -E 'auth|driver'
ss -ltnp | grep -E ':(3306|443)\b'

A Postfix that already has virtual_mailbox_domains set as a flat file does not interoperate cleanly with the SQL schema PostfixAdmin needs — plan the migration.

What’s happening

PostfixAdmin owns a relational schema with tables for admin, alias, alias_domain, domain, mailbox, vacation, and log. The admin UI is PHP; the backend is MariaDB/MySQL/PostgreSQL. The trick is that Postfix and Dovecot also speak to the same SQL backend — Postfix via mysql: lookup tables for virtual_alias_maps, virtual_mailbox_maps, etc., and Dovecot via the SQL passdb/userdb drivers.

The data flow per inbound message:

  1. Postfix receives RCPT TO: alice@example.com.
  2. virtual_mailbox_maps = mysql:/etc/postfix/sql/virtual-mailbox.cf queries the mailbox table for username='alice@example.com'.
  3. If the row exists, Postfix accepts; routes to virtual_transport = lmtp:unix:private/dovecot-lmtp.
  4. Dovecot’s LMTP receives, looks up the user via the SQL userdb, and stores the message under the right Maildir path.

The admin UI lets a tenant administrator create mailboxes, aliases, and quotas without touching /etc/. The whole config-as-data pattern is what makes PostfixAdmin worth the install on multi-domain boxes.

The procedure

  1. Install MariaDB and the SQL-aware mail packages:

    sudo apt update
    sudo apt install -y mariadb-server postfix-mysql dovecot-mysql postfixadmin

    The postfixadmin Debian package drops PHP files under /usr/share/postfixadmin/. You’ll point a webserver vhost at it.

  2. Create the database and user:

    sudo mariadb <<SQL
    CREATE DATABASE postfix CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'pa-db-pw';
    CREATE USER 'postfixadmin'@'localhost' IDENTIFIED BY 'pa-admin-pw';
    GRANT SELECT ON postfix.* TO 'postfix'@'localhost';
    GRANT ALL PRIVILEGES ON postfix.* TO 'postfixadmin'@'localhost';
    FLUSH PRIVILEGES;
    SQL

    Two users — the postfix user has read-only for the daemons; the postfixadmin user has write for the admin UI. Least privilege.

  3. Configure PostfixAdmin. Edit /etc/postfixadmin/config.local.php:

    <?php
    $CONF['configured'] = true;
    $CONF['database_type'] = 'mysqli';
    $CONF['database_host'] = 'localhost';
    $CONF['database_user'] = 'postfixadmin';
    $CONF['database_password'] = 'pa-admin-pw';
    $CONF['database_name'] = 'postfix';
    $CONF['encrypt'] = 'dovecot:ARGON2ID';
    $CONF['default_aliases'] = ['abuse' => 'postmaster', 'hostmaster' => 'postmaster', 'webmaster' => 'postmaster'];
    $CONF['domain_path'] = 'YES';
    $CONF['domain_in_mailbox'] = 'YES';
    $CONF['maildir_format'] = 'maildir';

    The dovecot:ARGON2ID encryption scheme means PostfixAdmin will hash passwords using Dovecot’s doveadm pw helper — the same hash Dovecot validates against.

  4. Initialize the schema via the setup page. Browse to https://mail.example.com/postfixadmin/setup.php. The page runs the migrations and prompts for a setup password and the first super-admin user. Once setup finishes, delete setup.php or rename it — leaving it accessible is an attack surface.

  5. Configure Postfix to read from SQL. Create /etc/postfix/sql/:

    sudo mkdir -p /etc/postfix/sql
    sudo chmod 750 /etc/postfix/sql

    Then write three lookup configs:

    /etc/postfix/sql/virtual-mailbox-domains.cf:

    user = postfix
    password = pa-db-pw
    hosts = 127.0.0.1
    dbname = postfix
    query = SELECT domain FROM domain WHERE domain='%s' AND active='1'

    /etc/postfix/sql/virtual-mailbox.cf:

    user = postfix
    password = pa-db-pw
    hosts = 127.0.0.1
    dbname = postfix
    query = SELECT maildir FROM mailbox WHERE username='%s' AND active='1'

    /etc/postfix/sql/virtual-alias.cf:

    user = postfix
    password = pa-db-pw
    hosts = 127.0.0.1
    dbname = postfix
    query = SELECT goto FROM alias WHERE address='%s' AND active='1'

    Then in /etc/postfix/main.cf:

    virtual_mailbox_domains = mysql:/etc/postfix/sql/virtual-mailbox-domains.cf
    virtual_mailbox_maps    = mysql:/etc/postfix/sql/virtual-mailbox.cf
    virtual_alias_maps      = mysql:/etc/postfix/sql/virtual-alias.cf
    virtual_mailbox_base    = /var/vmail
    virtual_minimum_uid     = 5000
    virtual_uid_maps        = static:5000
    virtual_gid_maps        = static:5000
    virtual_transport       = lmtp:unix:private/dovecot-lmtp

    Lock down permissions on the SQL files:

    sudo chown root:postfix /etc/postfix/sql/*.cf
    sudo chmod 640 /etc/postfix/sql/*.cf
  6. Configure Dovecot to read from SQL. Edit /etc/dovecot/conf.d/auth-sql.conf.ext and the main 10-auth.conf to include it:

    passdb {
        driver = sql
        args = /etc/dovecot/dovecot-sql.conf.ext
    }
    userdb {
        driver = sql
        args = /etc/dovecot/dovecot-sql.conf.ext
    }

    Then /etc/dovecot/dovecot-sql.conf.ext:

    driver = mysql
    connect = host=127.0.0.1 dbname=postfix user=postfix password=pa-db-pw
    default_pass_scheme = ARGON2ID
    password_query = SELECT username AS user, password, '/var/vmail/%d/%n' AS userdb_home, 5000 AS userdb_uid, 5000 AS userdb_gid FROM mailbox WHERE username='%u' AND active='1'
    user_query = SELECT '/var/vmail/%d/%n' AS home, 5000 AS uid, 5000 AS gid FROM mailbox WHERE username='%u' AND active='1'
  7. Reload, verify, add a domain through the UI. Reload Postfix and Dovecot:

    sudo postfix check && sudo systemctl reload postfix
    sudo systemctl reload dovecot

    Browse to /postfixadmin/, log in, Domain List → New Domain, fill in example.com. Then Virtual List → New Mailbox, create alice@example.com. Send a test message; confirm it lands in /var/vmail/example.com/alice/Maildir/new/.

Common pitfalls

  • The Dovecot SQL passdb returns rows with the password column missing or wrong-cased — default_pass_scheme is set but the column is empty; check the mailbox.password field after a UI create.
  • Postfix logs warning: dict_mysql:/etc/postfix/sql/virtual-mailbox.cf: table lookup failure because the postfix MySQL user wasn’t granted SELECT on the right tables — SHOW GRANTS FOR 'postfix'@'localhost'; is the diagnostic.
  • setup.php is left in the webroot — an attacker can re-run the install wizard against a live DB; delete or 403-protect it.
  • The virtual_minimum_uid = 5000 and virtual_uid_maps = static:5000 match the vmail user from a manual install; PostfixAdmin uses these UIDs by default, but if you previously used a different UID, the Maildir tree won’t be readable.
  • The SQL files in /etc/postfix/sql/ are world-readable on a default install — they contain the DB password; chmod 640 root:postfix is the fix.

Stack Harbor stands up PostfixAdmin as the admin layer for multi-tenant Postfix in the engagements we run — SQL schema initialized from version-controlled DDL, Postfix and Dovecot pointed at it, the UI behind authenticated Nginx with setup.php removed before handing the box back. If you have a Postfix that has grown to a dozen virtual domains all in flat files, that consolidation work is part of our Managed Operations practice.