Postfix and Dovecot Complete Mail Server Setup is a common requirement for VPS administrators. This guide provides practical instructions that you can follow on Ubuntu 22.04/24.04 or Debian 12, though most steps apply to other distributions as well.
Prerequisites
- A registered domain name (for public-facing services)
- A clean IP address not on any blacklists
- A VPS running Ubuntu 22.04 or later (2GB+ RAM recommended)
- Basic familiarity with the Linux command line
Server Installation
Before making changes to the configuration, always create a backup of the existing files. This ensures you can quickly roll back if something goes wrong during the setup process.
# Install Postfix and Dovecot
sudo apt update
sudo apt install -y postfix dovecot-core dovecot-imapd dovecot-lmtpd
# Configure Postfix main.cf
sudo postconf -e 'myhostname = mail.example.com'
sudo postconf -e 'mydomain = example.com'
sudo postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem'
sudo postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem'
This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.
- Use connection pooling for database connections
- Implement caching at every appropriate layer
- Start with the minimum required resources
- Profile before optimizing - measure first
- Scale vertically before scaling horizontally
Authentication Setup (SPF/DKIM)
Security should be a primary consideration when configuring postfix. Always use strong passwords, keep software updated, and restrict network access to only the necessary ports and IP addresses.
# Generate DKIM key
sudo apt install -y opendkim opendkim-tools
sudo mkdir -p /etc/opendkim/keys/example.com
sudo opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/example.com -s default -v
# Add the DNS TXT record from:
cat /etc/opendkim/keys/example.com/default.txt
This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.
Client Configuration
Before making changes to the configuration, always create a backup of the existing files. This ensures you can quickly roll back if something goes wrong during the setup process.
# Install Postfix and Dovecot
sudo apt update
sudo apt install -y postfix dovecot-core dovecot-imapd dovecot-lmtpd
# Configure Postfix main.cf
sudo postconf -e 'myhostname = mail.example.com'
sudo postconf -e 'mydomain = example.com'
sudo postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem'
sudo postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem'
These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.
Spam Filtering
When scaling this setup, consider vertical scaling (adding more RAM/CPU) first, as it's simpler to implement. Horizontal scaling adds complexity but may be necessary for high-traffic applications.
# Generate DKIM key
sudo apt install -y opendkim opendkim-tools
sudo mkdir -p /etc/opendkim/keys/example.com
sudo opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/example.com -s default -v
# Add the DNS TXT record from:
cat /etc/opendkim/keys/example.com/default.txt
These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.
- Use connection pooling for database connections
- Scale vertically before scaling horizontally
- Implement caching at every appropriate layer
- Start with the minimum required resources
Conclusion
This guide covered the essential steps for working with postfix on a VPS environment. For more advanced configurations, refer to the official documentation. Don't hesitate to reach out to our support team if you need help with your specific setup.