How to Set Up Multiple Mail Domains on a Single Server in Ubuntu Linux (with MySQL)

Prerequisites

Ensure your server is up-to-date and has Postfix, Dovecot, and MySQL installed. You can install MySQL with:

sudo apt update
sudo apt install mysql-server

Step 1: Install Necessary Packages

Update your package list and install Postfix, Dovecot, and the MySQL integration packages:

sudo apt update
sudo apt install postfix postfix-mysql dovecot-imapd dovecot-pop3d dovecot-mysql

Step 2: Configure MySQL

1. Secure MySQL installation:

sudo mysql_secure_installation

Follow the prompts to secure your MySQL installation.

2. Create the database and user for mail:

Log in to MySQL:

sudo mysql -u root -p

Run the following SQL commands to set up the database:

CREATE DATABASE mailserver;
GRANT ALL PRIVILEGES ON mailserver.* TO 'mailuser'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

3. Create the necessary tables:

Log in to MySQL and create the tables for domains, users, and aliases:

sudo mysql -u root -p mailserver

Run the following SQL commands:

CREATE TABLE `domains` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `domain` varchar(50) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `domain` (`domain`)
);

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `domain_id` int(11) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  FOREIGN KEY (`domain_id`) REFERENCES `domains`(`id`) ON DELETE CASCADE
);

CREATE TABLE `aliases` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `source` varchar(100) NOT NULL,
  `destination` varchar(100) NOT NULL,
  `domain_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`domain_id`) REFERENCES `domains`(`id`) ON DELETE CASCADE
);

Step 3: Configure Postfix

1. Edit the Postfix main configuration file:

sudo nano /etc/postfix/main.cf

Add the following configurations:

mydestination = localhost
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

2. Create MySQL lookup files:

mysql-virtual-mailbox-domains.cf:
sudo nano /etc/postfix/mysql-virtual-mailbox-domains.cf
user = mailuser
password = your_password
hosts = 127.0.0.1
dbname = mailserver
query = SELECT domain FROM domains WHERE domain='%s' AND active=1
mysql-virtual-mailbox-maps.cf:
sudo nano /etc/postfix/mysql-virtual-mailbox-maps.cf
user = mailuser
password = your_password
hosts = 127.0.0.1
dbname = mailserver
query = SELECT email FROM users WHERE email='%s' AND active=1
mysql-virtual-alias-maps.cf:
sudo nano /etc/postfix/mysql-virtual-alias-maps.cf
user = mailuser
password = your_password
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM aliases WHERE source='%s'

3. Set proper permissions for the MySQL lookup files:

sudo chown root:root /etc/postfix/mysql-virtual-*.cf
sudo chmod 640 /etc/postfix/mysql-virtual-*.cf

4. Restart Postfix:

sudo systemctl restart postfix

Step 4: Configure Dovecot

1. Edit the Dovecot main configuration file:

sudo nano /etc/dovecot/dovecot.conf

Ensure the following lines are present:

!include conf.d/*.conf

2. Configure Dovecot to use MySQL:

10-auth.conf:
sudo nano /etc/dovecot/conf.d/10-auth.conf

Uncomment the following line:

!include auth-sql.conf.ext
auth-sql.conf.ext:
sudo nano /etc/dovecot/conf.d/auth-sql.conf.ext

Add the following configuration:

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

userdb {
  driver = sql
  args = /etc/dovecot/dovecot-sql.conf.ext
}
dovecot-sql.conf.ext:
sudo nano /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 dbname=mailserver user=mailuser password=your_password
default_pass_scheme = SHA512-CRYPT

password_query = SELECT email as user, password FROM users WHERE email='%u' AND active=1
user_query = SELECT email as user, '/var/mail/vhosts/%d/%n' as home, 'maildir:/var/mail/vhosts/%d/%n' as mail, 5000 as uid, 5000 as gid FROM users WHERE email='%u' AND active=1

3. Set up mail directories and permissions:

sudo mkdir -p /var/mail/vhosts/example.com
sudo mkdir -p /var/mail/vhosts/anotherdomain.com
sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 vmail -d /var/mail
sudo chown -R vmail:vmail /var/mail
sudo chmod -R 770 /var/mail

4. Restart Dovecot:

sudo systemctl restart dovecot

Step 5: Testing Your Configuration

  1. Check Postfix and Dovecot status:
sudo systemctl status postfix
sudo systemctl status dovecot
  1. Send test emails to your configured domains to ensure they are being delivered correctly and can be accessed via IMAP/POP3.

Optional: Configure SSL/TLS

For secure email transmission, configure SSL/TLS in both Postfix and Dovecot.

1. For Postfix:

Add to /etc/postfix/main.cf:

smtpd_tls_cert_file=/etc/ssl/certs/your-cert.pem
smtpd_tls_key_file=/etc/ssl/private/your-key.pem
smtpd_use_tls=yes

2. For Dovecot:

Edit /etc/dovecot/conf.d/10-ssl.conf:

ssl = yes
ssl_cert = </etc/ssl/certs/your-cert.pem
ssl_key = </etc/ssl/private/your-key.pem

Restart both services after making these changes:

sudo systemctl restart postfix
sudo systemctl restart dovecot

Running into problems with SaslAuthd here is a link that helped me to solve it https://www.mail-archive.com/debian-bugs-dist@lists.debian.org/msg1934701.html

Leave a Reply

Your email address will not be published. Required fields are marked *