You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何跨服务器部署PostfixAdmin对接远程Postfix与MySQL数据库?

Cross-Server Postfix + PostfixAdmin + Remote MySQL Configuration Guide

Got it, let's walk through the complete cross-server setup you need—separate Postfix, MySQL, and Dockerized PostfixAdmin servers. I’ll break this into actionable steps based on your existing progress (already connected PostfixAdmin and Postfix to the remote MySQL).

1. Pre-Flight Checks & Foundation Confirmation

First, make sure all network and permission basics are solid:

  • Network Connectivity: Verify:
    • Dockerized PostfixAdmin can reach the MySQL server on port 3306
    • Postfix server can reach the MySQL server on port 3306
    • PostfixAdmin server (Docker host) can reach the Postfix server on ports 25 (SMTP) and/or 587 (submission) (for test emails and admin actions)
  • MySQL User Permissions: Double-check that:
    • The Postfix MySQL user has SELECT access to all Postfix-related tables (domain, mailbox, alias, etc.) and is restricted to connecting only from the Postfix server’s IP:
      GRANT SELECT ON postfix.* TO 'postfix'@'your-postfix-server-ip' IDENTIFIED BY 'your-postfix-db-password';
      FLUSH PRIVILEGES;
      
    • The PostfixAdmin MySQL user has SELECT, INSERT, UPDATE, DELETE access to the postfix database and is restricted to connecting only from the PostfixAdmin Docker host’s IP:
      GRANT ALL PRIVILEGES ON postfix.* TO 'postfixadmin'@'your-postfixadmin-host-ip' IDENTIFIED BY 'your-postfixadmin-db-password';
      FLUSH PRIVILEGES;
      

2. Postfix Server Core Configuration

Edit your Postfix main.cf and add/modify these key directives to pull data from the remote MySQL:

2.1 Virtual Domain & Mailbox Lookups

Add these lines to /etc/postfix/main.cf:

# Virtual domain management
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf

# Mailbox storage settings
virtual_mailbox_base = /var/vmail
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

Now create the corresponding MySQL lookup files:

/etc/postfix/mysql-virtual-mailbox-domains.cf

hosts = your-mysql-server-ip
user = postfix
password = your-postfix-db-password
dbname = postfix
query = SELECT 1 FROM domain WHERE domain='%s' AND active='1'

/etc/postfix/mysql-virtual-mailbox-maps.cf

hosts = your-mysql-server-ip
user = postfix
password = your-postfix-db-password
dbname = postfix
query = SELECT mailbox FROM mailbox WHERE username='%s' AND active='1'

/etc/postfix/mysql-virtual-alias-maps.cf

hosts = your-mysql-server-ip
user = postfix
password = your-postfix-db-password
dbname = postfix
query = SELECT goto FROM alias WHERE address='%s' AND active='1'

2.2 SMTP Authentication (SASL)

To allow users to send authenticated emails, configure Postfix to use MySQL for SASL verification. First, enable SASL in main.cf:

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot  # Or use 'cyrus' if you prefer, but Dovecot is simpler for virtual mailboxes
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination

Note: If you haven’t set up Dovecot yet, you’ll need to install it on the Postfix server and configure it to pull user credentials from the remote MySQL (this is also required for POP3/IMAP access).

2.3 Reload Postfix

Apply the changes:

postfix reload

3. Dockerized PostfixAdmin Configuration Adjustments

Since PostfixAdmin is running in Docker, tweak its config.inc.php (mounted as a volume to persist changes) to align with the cross-server setup:

3.1 MySQL & Postfix Integration Settings

Update these values in config.inc.php:

// Remote MySQL connection
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'your-mysql-server-ip';
$CONF['database_user'] = 'postfixadmin';
$CONF['database_password'] = 'your-postfixadmin-db-password';
$CONF['database_name'] = 'postfix';

// Match Postfix's mailbox storage structure
$CONF['domain_path'] = 'NO';  // Set to 'YES' if you use domain-based subdirectories in /var/vmail
$CONF['domain_in_mailbox'] = 'YES';  // Should match Postfix's virtual_mailbox_maps query

// Allow PostfixAdmin to send test/admin emails via your Postfix server
$CONF['smtp_server'] = 'your-postfix-server-ip';
$CONF['smtp_port'] = 25;
$CONF['smtp_username'] = '';  // Leave empty if Postfix allows relay from PostfixAdmin's IP
$CONF['smtp_password'] = '';

3.2 Docker Container Tweaks

Ensure your Docker run command or docker-compose.yml maps the Web port (e.g., 80/443) to the host, and mounts the config.inc.php as a volume so changes persist:

# Example docker-compose snippet
services:
  postfixadmin:
    image: postfixadmin/postfixadmin:latest
    ports:
      - "8080:80"
    volumes:
      - ./config.inc.php:/var/www/html/config.inc.php
    restart: always

4. Test & Validate the Setup

Let’s confirm everything works end-to-end:

  1. Create a Test Domain/User: Log into PostfixAdmin, create a domain (e.g., test.com) and a mailbox (e.g., user@test.com).
  2. Test Postfix Lookups: On the Postfix server, run:
    postmap -q test.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
    postmap -q user@test.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
    
    Both should return valid results (1 for the domain, the mailbox path for the user).
  3. Test Email Delivery: Send an email to user@test.com from an external account. Check if it lands in /var/vmail/test.com/user (adjust path based on your domain_path setting).
  4. Test SMTP Authentication: Use an email client to connect to your Postfix server on port 587, use user@test.com and the password set in PostfixAdmin—you should be able to send an email successfully.
  5. Test PostfixAdmin Notifications: Create a new mailbox in PostfixAdmin and check if the welcome email is sent via your Postfix server.

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:26:19