Here’s a comprehensive post on how to install and configure a mail server on your VPS using Postfix:
How to Install and Configure a Mail Server on Your VPS
A reliable mail server on your VPS can handle your website’s email needs, such as sending transactional emails, welcome messages, or notifications. Here’s how to set up a mail server using Postfix.
1. Prerequisites
Before starting, ensure the following:
- You have a VPS with root or sudo access.
- Your domain name is configured, and you can manage DNS records (e.g., via your registrar or hosting provider).
- A basic understanding of Linux commands.
2. Install Postfix
Step 1: Update Your Server
First, update the package list and upgrade any outdated packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Postfix
Install Postfix and related mail utilities:
sudo apt install postfix mailutils -y
3. Configure Postfix
During installation, you’ll be prompted with a configuration screen:
- General type of mail configuration: Select
Internet Site
. - System mail name: Enter your domain name (e.g.,
example.com
).
You can always modify this later by editing the Postfix configuration file.
4. Postfix Main Configuration
Edit the Postfix configuration file:
sudo nano /etc/postfix/main.cf
Update the following fields:
myhostname = mail.example.com # Replace with your mail server hostname
mydomain = example.com # Your domain
myorigin = $mydomain
mydestination = $myhostname, $mydomain, localhost.$mydomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::1]/128 # Localhost only
home_mailbox = Maildir/ # Use Maildir format for storing emails
mailbox_size_limit = 0 # No limit
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4 # Use only IPv4 (or 'all' for both IPv4 and IPv6)
Save and exit the file (Ctrl + O
, then Ctrl + X
).
5. Configure DNS Records
To ensure proper mail delivery, set up the following DNS records for your domain:
- MX Record:
- Host:
@
- Value:
mail.example.com
- Priority:
10
- Host:
- SPF Record:
- Type: TXT
- Name:
@
- Value:
v=spf1 mx ~all
- DKIM Record:
- Install opendkim and generate a key (explained below).
- DMARC Record:
- Type: TXT
- Name:
_dmarc
- Value:
v=DMARC1; p=none; rua=mailto:postmaster@example.com
6. Install Dovecot (Optional: For Receiving Emails)
If you also want to receive emails, install Dovecot:
sudo apt install dovecot-imapd dovecot-pop3d -y
Update its configuration to use Maildir
:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Change:
mail_location = maildir:~/Maildir
Restart Dovecot:
sudo systemctl restart dovecot
7. Test Sending Emails
Use the mail
command to test your setup:
echo "Test email from Postfix" | mail -s "Postfix Test" recipient@example.com
If the email doesn’t arrive, check the logs:
sudo tail -f /var/log/mail.log
8. Secure Postfix with TLS
Install an SSL certificate for secure email transmission:
sudo apt install certbot
sudo certbot certonly --standalone -d mail.example.com
Configure Postfix to use the certificate:
sudo nano /etc/postfix/main.cf
Add:
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_use_tls=yes
Restart Postfix:
sudo systemctl restart postfix
9. Troubleshooting
- Emails Going to Spam:
- Ensure SPF, DKIM, and DMARC are correctly configured.
- Use Mail Tester to analyze your email score.
- Check Logs:
- Use
sudo tail -f /var/log/mail.log
for live logging.
- Use
- Firewall Configuration:
- Open the following ports on your VPS firewall:
- Port 25 (SMTP)
- Port 587 (SMTP Secure)
- Port 993 (IMAP Secure)
- Open the following ports on your VPS firewall:
10. Additional Tools
- PostfixAdmin: A web-based GUI to manage Postfix users and domains.
- Roundcube: A webmail client for user access.
Conclusion
By following these steps, you now have a functional and secure mail server on your VPS using Postfix. You can use this server to handle WordPress emails, transactional notifications, or even personal email accounts. Ensure you regularly monitor the logs and maintain your DNS records for optimal email deliverability.
Leave a Reply