Managing Mail Domains with PostfixAdmin
August 31, 2026
PostfixAdmin is a web interface for managing the MySQL database that Postfix and Dovecot query for virtual domains, mailboxes, and aliases. Without it, you'd manage mail accounts by running SQL queries directly. PostfixAdmin provides domain management, mailbox creation with quota settings, alias configuration, and domain-level alias forwarding. This post covers installing PostfixAdmin on Gentoo with Apache and deploying it as a virtual host.
Prerequisites
- Postfix and Dovecot are configured for virtual mailboxes
- MySQL is running with the mailserver database
- Apache is running with PHP-FPM
- TLS certificates are available
Setting USE Flags
cat > /etc/portage/package.use/postfixadmin << 'EOF'
www-apps/postfixadmin mysql vhosts
EOF
The vhosts flag enables deployment via webapp-config.
Installing PostfixAdmin
emerge -av www-apps/postfixadmin
Creating the Database Tables
PostfixAdmin creates its own database tables in the mailserver database on first setup. Create the PostfixAdmin database user with write access:
mysql << 'EOF'
GRANT ALL PRIVILEGES ON mailserver.* TO 'mailadmin'@'localhost' IDENTIFIED BY 'mailadmin-db-password';
FLUSH PRIVILEGES;
EOF
This user needs full access because PostfixAdmin creates, modifies, and deletes domain, mailbox, and alias records.
Deploying with webapp-config
Deploy PostfixAdmin to a virtual host directory:
webapp-config -I -h mailadmin.example.com -d / postfixadmin $(qatom -F "%{PV}" $(portageq best_visible / www-apps/postfixadmin))
This installs PostfixAdmin to /var/www/mailadmin.example.com/htdocs/.
Configuring PostfixAdmin
PostfixAdmin's configuration file is at the deployment root. Create or edit /var/www/mailadmin.example.com/htdocs/config.local.php:
<?php
$CONF['configured'] = true;
// Database
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'mailadmin';
$CONF['database_password'] = 'mailadmin-db-password';
$CONF['database_name'] = 'mailserver';
// Setup password (bcrypt hash)
$CONF['setup_password'] = 'your-setup-password-hash';
// Domain defaults
$CONF['default_aliases'] = array(
'abuse' => 'postmaster@example.com',
'hostmaster' => 'postmaster@example.com',
'postmaster' => 'postmaster@example.com',
'webmaster' => 'postmaster@example.com'
);
// Mailbox settings
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['maildir_name_struc'] = 'Maildir/';
// Quota (in MB, 0 = unlimited)
$CONF['maxquota'] = 0;
$CONF['quota'] = 'YES';
$CONF['used_quotas'] = 'YES';
// Password scheme (matches Dovecot)
$CONF['encrypt'] = 'dovecot:SHA512-CRYPT';
$CONF['dovecotpw'] = '/usr/bin/doveadm pw -s SHA512-CRYPT';
// Allowed domains
$CONF['domain_limit'] = 0; // unlimited
$CONF['alias_limit'] = 0; // unlimited
// Footer
$CONF['show_footer_text'] = 'NO';
// Logging
$CONF['logging'] = 'YES';
?>
Key Settings
$CONF['encrypt'] = 'dovecot:SHA512-CRYPT' — passwords are hashed using Dovecot's SHA512-CRYPT scheme. This means PostfixAdmin calls doveadm pw to hash passwords, ensuring compatibility with Dovecot's authentication.
$CONF['domain_path'] = 'YES' — mailboxes are stored under domain-specific directories: /var/vmail/example.com/user/Maildir/.
$CONF['default_aliases'] — when a new domain is added, these aliases are created automatically. RFC 5321 requires postmaster@domain to be deliverable.
Generating the Setup Password
Generate the setup password hash by navigating to:
https://mailadmin.example.com/setup.php
Enter a setup password and PostfixAdmin will display the hash. Copy it into the $CONF['setup_password'] line in the config file.
Configuring the Apache Virtual Host
Create /etc/apache2/vhosts.d/mailadmin.example.com.conf:
<VirtualHost *:80>
ServerName mailadmin.example.com
DocumentRoot /var/www/mailadmin.example.com/htdocs/public
# Trust reverse proxy headers
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
<Directory /var/www/mailadmin.example.com/htdocs/public>
Require all granted
AllowOverride All
Options FollowSymLinks
</Directory>
ErrorLog /var/log/apache2/mailadmin.example.com_error.log
CustomLog /var/log/apache2/mailadmin.example.com_access.log combined
</VirtualHost>
Note: newer versions of PostfixAdmin serve from the public/ subdirectory.
Restart Apache:
apache2ctl configtest
rc-service apache2 restart
Running the Setup
Navigate to https://mailadmin.example.com/setup.php and:
- Verify all requirements are met (database connection, PHP extensions)
- Enter the setup password to create the admin account
- Create a superadmin email address and password
PostfixAdmin will create all necessary database tables in the mailserver database.
Adding Domains and Mailboxes
Adding a Domain
- Log in to PostfixAdmin with the superadmin account
- Go to Domain List > New Domain
- Enter the domain name (e.g.,
example.com) - Set mailbox limits and quota defaults
- Click Add Domain
PostfixAdmin creates the domain entry and the default aliases (abuse, hostmaster, postmaster, webmaster).
Creating a Mailbox
- Go to Virtual List > Add Mailbox
- Enter the username (local part, e.g.,
user) - Select the domain
- Set the password
- Optionally set a quota
- Click Add Mailbox
The mailbox is immediately available. Postfix can deliver to it and Dovecot can authenticate it.
Creating Aliases
- Go to Virtual List > Add Alias
- Enter the alias address (e.g.,
info@example.com) - Enter the destination address (e.g.,
user@example.com) - Click Add Alias
Domain Aliases
If example.org should accept mail for all the same addresses as example.com:
- Go to Domain List > Add Domain Alias
- Select
example.orgas the alias domain - Select
example.comas the target domain - Click Add
Mail to user@example.org will be delivered to user@example.com's mailbox.
Testing the Setup
Verify Database Tables
mysql mailserver -e "SHOW TABLES;"
You should see tables including domain, mailbox, alias, and alias_domain.
Verify a Mailbox Exists
mysql mailserver -e "SELECT username, domain, maildir FROM mailbox LIMIT 5;"
Test Postfix Lookups
postmap -q example.com mysql:/etc/postfix/mysql-virtual-domains.cf
postmap -q user@example.com mysql:/etc/postfix/mysql-virtual-mailboxes.cf
Both should return results if the domain and mailbox exist.
Test Dovecot Authentication
doveadm auth test user@example.com password
Send a Test Email
echo "Test message body" | mail -s "PostfixAdmin test" user@example.com
Check the mailbox:
ls /var/vmail/example.com/user/Maildir/new/
If a file appears, delivery is working end-to-end.
Command-Line Management
PostfixAdmin includes a CLI tool for managing domains and mailboxes without the web interface. This is useful for scripting and automation.
Adding a Domain via CLI
php /var/www/mailadmin.example.com/htdocs/scripts/postfixadmin-cli.php domain add example.com
Creating a Mailbox via CLI
php /var/www/mailadmin.example.com/htdocs/scripts/postfixadmin-cli.php mailbox add user@example.com \
--password "secure-password" --password2 "secure-password" --name "User Name" --active 1
Viewing Domain Details
php /var/www/mailadmin.example.com/htdocs/scripts/postfixadmin-cli.php domain view example.com
Creating a Superadmin
php /var/www/mailadmin.example.com/htdocs/scripts/postfixadmin-cli.php admin add admin@example.com \
--password "admin-password" --password2 "admin-password" --superadmin 1 --active 1
The CLI tool communicates directly with the database using the same config.local.php configuration, so it respects all password hashing and domain settings.
Backing Up PostfixAdmin Data
PostfixAdmin stores all data in MySQL. Back up the mailserver database regularly:
mysqldump mailserver > /var/backups/mailserver-$(date +%Y%m%d).sql
The critical tables are domain, mailbox, alias, and alias_domain. The configuration file (config.local.php) should also be backed up since it contains the database credentials and password scheme settings.
Securing PostfixAdmin
Restrict Access
PostfixAdmin should only be accessible to administrators. If it's behind a reverse proxy, restrict access by IP:
<Directory /var/www/mailadmin.example.com/htdocs/public>
Require ip 192.168.1.0/24
</Directory>
Remove setup.php Access
After initial setup, block access to the setup page:
<Location /setup.php>
Require all denied
</Location>
Lock Down the Installer Directory
PostfixAdmin ships with an installer/ directory. Remove access to it after setup:
chmod 0400 -R /var/www/mailadmin.example.com/htdocs/installer/
Password Policy
PostfixAdmin enforces a minimum password length by default. Configure it in config.local.php:
$CONF['password_validation'] = '/^.{8,}/';
This regex requires at least 8 characters. Adjust the pattern if you need stronger requirements (e.g., requiring mixed case or special characters).
Summary
After completing these steps:
- PostfixAdmin provides a web interface for managing virtual domains, mailboxes, and aliases
- Passwords are hashed with SHA512-CRYPT compatible with Dovecot
- Default aliases (postmaster, abuse, hostmaster, webmaster) are created automatically
- Domain aliases forward entire domains to a primary domain
- Quotas can be set per-mailbox and per-domain
- Postfix and Dovecot read directly from the PostfixAdmin-managed database
The next step is Roundcube webmail for users to access their email through a browser.