Configure Multi-Domain Email Hosting on Ubuntu VPS: Complete Setup

Planning Your Multi-Domain Email Architecture
Running email for multiple domains on a single Ubuntu VPS requires careful planning. Unlike single-domain setups, you'll need a database to track domains, users, and aliases.
Each domain can have different policies, quotas, and security settings. This approach works well for agencies managing client email or businesses consolidating multiple brands under one server.
The key is setting up Postfix with MySQL lookup tables and Dovecot with proper virtual domain handling. Before starting, verify your VPS has at least 2GB RAM. Email services become resource-intensive when handling multiple domains.
Hostperl VPS hosting provides the performance and reliability needed for professional email hosting across domains.
Installing Core Email Components
Start by installing the essential packages for multi-domain email hosting. You'll need Postfix for SMTP, Dovecot for IMAP/POP3, and MySQL for virtual domain management.
sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql mysql-server postfix-mysql
During Postfix installation, select "Internet Site" and enter your primary domain. Don't worry about the domain name - you'll configure virtual domains separately.
Install additional security and management tools:
sudo apt install spamassassin clamav clamav-daemon amavisd-new
These tools provide spam filtering and virus scanning across all hosted domains.
Setting Up MySQL Database Structure
Create a dedicated database for virtual email domains. This centralizes user management and allows per-domain configuration.
sudo mysql -u root -p
Create the database and user:
CREATE DATABASE emailserver;
CREATE USER 'emailadmin'@'localhost' IDENTIFIED BY 'secure_password_here';
GRANT ALL PRIVILEGES ON emailserver.* TO 'emailadmin'@'localhost';
FLUSH PRIVILEGES;
USE emailserver;
Create the domain table:
CREATE TABLE virtual_domains (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);
Create the users table with quota support:
CREATE TABLE virtual_users (
id INT AUTO_INCREMENT PRIMARY KEY,
domain_id INT NOT NULL,
password VARCHAR(106) NOT NULL,
email VARCHAR(120) NOT NULL UNIQUE,
quota BIGINT DEFAULT 0,
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
);
Create the aliases table for forwarding:
CREATE TABLE virtual_aliases (
id INT AUTO_INCREMENT PRIMARY KEY,
domain_id INT NOT NULL,
source VARCHAR(100) NOT NULL,
destination VARCHAR(100) NOT NULL,
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
);
Configure Multi-Domain Email Hosting on Ubuntu VPS with Postfix
Configure Postfix to use MySQL for virtual domain lookups. Edit the main configuration file:
sudo nano /etc/postfix/main.cf
Add these virtual domain settings:
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
virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_mailbox_base = /var/mail/vhosts
Create the virtual domain lookup file:
sudo nano /etc/postfix/mysql-virtual-mailbox-domains.cf
Add the MySQL connection details:
user = emailadmin
password = secure_password_here
hosts = 127.0.0.1
dbname = emailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
Create the virtual user lookup file:
sudo nano /etc/postfix/mysql-virtual-mailbox-maps.cf
user = emailadmin
password = secure_password_here
hosts = 127.0.0.1
dbname = emailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'
Create the virtual alias lookup file:
sudo nano /etc/postfix/mysql-virtual-alias-maps.cf
user = emailadmin
password = secure_password_here
hosts = 127.0.0.1
dbname = emailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'
Configuring Dovecot for Multiple Domains
Configure Dovecot to handle virtual users across multiple domains. Start with the main configuration:
sudo nano /etc/dovecot/dovecot.conf
Enable the required protocols:
protocols = imap pop3 lmtp
Configure the authentication system:
sudo nano /etc/dovecot/conf.d/10-auth.conf
Disable system users and enable SQL authentication:
disable_plaintext_auth = yes
auth_mechanisms = plain login
!include auth-sql.conf.ext
Configure the SQL authentication file:
sudo nano /etc/dovecot/conf.d/auth-sql.conf.ext
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
Create the SQL configuration file:
sudo nano /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 dbname=emailserver user=emailadmin password=secure_password_here
default_pass_scheme = SHA512-CRYPT
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u'
user_query = SELECT '/var/mail/vhosts/%d/%n' as home, 'maildir:/var/mail/vhosts/%d/%n' as mail, 5000 AS uid, 5000 AS gid FROM virtual_users WHERE email='%u'
Creating Virtual Mail Directory Structure
Set up the directory structure for virtual mailboxes. Create a dedicated user for mail handling:
sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 vmail -d /var/mail/vhosts -m
Create the mail storage directory:
sudo mkdir -p /var/mail/vhosts
sudo chown -R vmail:vmail /var/mail/vhosts
Configure Dovecot mailbox settings:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Set the mail location and user:
mail_location = maildir:/var/mail/vhosts/%d/%n
mail_uid = vmail
mail_gid = vmail
first_valid_uid = 5000
last_valid_uid = 5000
Adding Your First Domains and Users
Add domains to your email server through the MySQL database. Connect to MySQL:
sudo mysql -u emailadmin -p emailserver
Add your first domain:
INSERT INTO virtual_domains (name) VALUES ('yourdomain.com');
INSERT INTO virtual_domains (name) VALUES ('clientdomain.com');
Create email accounts for each domain. Generate password hashes first:
doveadm pw -s SHA512-CRYPT
Enter a password when prompted, then copy the generated hash. Add users to the database:
INSERT INTO virtual_users (domain_id, password, email, quota) VALUES
(1, 'generated_password_hash_here', 'admin@yourdomain.com', 1073741824),
(2, 'another_password_hash_here', 'support@clientdomain.com', 2147483648);
The quota values are in bytes (1GB = 1073741824 bytes). Set different quotas per domain based on your hosting plans.
Add some aliases for professional email setup:
INSERT INTO virtual_aliases (domain_id, source, destination) VALUES
(1, 'info@yourdomain.com', 'admin@yourdomain.com'),
(1, 'contact@yourdomain.com', 'admin@yourdomain.com'),
(2, 'sales@clientdomain.com', 'support@clientdomain.com');
Testing Multi-Domain Email Setup
Test your configuration before going live. First, verify Postfix can resolve virtual domains:
sudo postmap -q yourdomain.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
sudo postmap -q admin@yourdomain.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
Both commands should return "1" if working correctly. Test Dovecot authentication:
doveadm auth test admin@yourdomain.com password_here
Create mail directories for your test domains:
sudo mkdir -p /var/mail/vhosts/yourdomain.com/admin
sudo mkdir -p /var/mail/vhosts/clientdomain.com/support
sudo chown -R vmail:vmail /var/mail/vhosts/
Restart services to apply changes:
sudo systemctl restart postfix
sudo systemctl restart dovecot
Send test emails between domains to verify delivery and storage. Check the mail logs for any issues:
sudo tail -f /var/log/mail.log
SSL Configuration for All Domains
Secure email connections across all hosted domains with SSL certificates. Install Certbot for Let's Encrypt:
sudo apt install certbot
Generate certificates for your email domains:
sudo certbot certonly --standalone -d mail.yourdomain.com -d mail.clientdomain.com
Configure Postfix to use SSL:
sudo nano /etc/postfix/main.cf
Add SSL settings:
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls = yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
Configure Dovecot SSL:
sudo nano /etc/dovecot/conf.d/10-ssl.conf
ssl = required
ssl_cert = </etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
This tutorial builds on our Ubuntu VPS mail server setup guide but extends it for multiple domain hosting scenarios.
Managing Email Quotas and Policies
Implement different email policies per domain. Create a quotas management script:
sudo nano /usr/local/bin/email-quota-check.sh
#!/bin/bash
for user in $(mysql -u emailadmin -pYOUR_PASSWORD -D emailserver -e "SELECT email FROM virtual_users;" -s); do
usage=$(doveadm quota get -u $user | awk '/STORAGE/{print $3}')
limit=$(mysql -u emailadmin -pYOUR_PASSWORD -D emailserver -e "SELECT quota FROM virtual_users WHERE email='$user';" -s)
echo "$user: ${usage}KB / ${limit}KB"
done
Make it executable and run periodically:
sudo chmod +x /usr/local/bin/email-quota-check.sh
sudo crontab -e
Add a daily quota check:
0 2 * * * /usr/local/bin/email-quota-check.sh > /var/log/email-quotas.log
Set up automated warnings for users approaching quota limits. This proactive approach prevents email delivery failures and maintains service quality.
DNS Configuration for Multiple Domains
Configure DNS records for each hosted email domain. Each domain needs proper MX, SPF, and DKIM records.
For yourdomain.com:
MX record: @ -> mail.yourdomain.com (priority 10)
A record: mail -> YOUR_VPS_IP
TXT record: @ -> "v=spf1 mx ~all"
For clientdomain.com:
MX record: @ -> mail.clientdomain.com (priority 10)
A record: mail -> YOUR_VPS_IP
TXT record: @ -> "v=spf1 mx ~all"
You can use the same IP for all domains but use different mail subdomain names. This makes SSL certificate management easier and provides better organization.
Our email authentication setup guide covers DKIM and DMARC configuration for professional email delivery.
Ready to deploy multi-domain email hosting? Hostperl VPS hosting provides the reliable infrastructure and support you need for professional email services. Our team can help with complex email migrations and multi-domain configurations.
Frequently Asked Questions
Can I host unlimited domains on one VPS?
Technically yes, but resource limits apply. Each domain adds CPU and memory overhead. Most 2GB VPS instances handle 10-20 active email domains comfortably. Monitor server performance and upgrade when needed.
How do I migrate existing email from other servers?
Use imapsync or similar tools to copy mailboxes. Create the domains and users first, then sync the email data. Our hosting migration service guide covers the complete process.
What happens if one domain gets blacklisted?
All domains share the same IP, so blacklisting affects everyone. Use dedicated IPs for high-risk domains or implement proper email authentication to prevent blacklisting.
Can I set different storage quotas per domain?
Yes, the MySQL structure supports per-user quotas. You can also modify the database to add per-domain default quotas for easier management of large client accounts.
How do I backup emails for multiple domains?
Backup the /var/mail/vhosts directory and MySQL database together. This ensures you can restore complete email functionality. Schedule automated backups to prevent data loss.
