How to Set Up an Email Server on a VPS
In this article
Setting up your own email server on a VPS gives you full control over your domain's mail, but it is not a one-click job: you need a mail transfer agent (MTA), a mail delivery agent (MDA), DNS records, TLS certificates, and a firewall that allows the right ports. The shortest working path is to install Postfix and Dovecot on a fresh Ubuntu VPS, point your domain's MX and SPF records at it, obtain a Let's Encrypt certificate, and open ports 25, 465, 587, 993 and 995. Be aware that many VPS providers block outbound port 25 by default, so check that first or your mail will never leave the server.
What You'll Need
- A VPS running a current LTS release of Ubuntu or Debian, with root or sudo access.
- A registered domain name with access to its DNS control panel.
- A static public IPv4 address (and ideally IPv6) assigned to the VPS.
- A reverse DNS (PTR) record set by your VPS provider to match your mail hostname.
- Ports 25, 465, 587, 993 and 995 open in both the provider's firewall and the server's own firewall.
- Basic comfort with the Linux command line and a text editor such as nano.
Step-by-Step
- Check outbound port 25. Before installing anything, confirm your provider allows outbound SMTP on port 25. Many block it to reduce spam. If it is blocked, ask the provider to unblock it or use a smarthost relay for outbound mail. Without this, your server can receive mail but cannot deliver it.
- Set the hostname. Run
hostnamectl set-hostname mail.example.com(replace with your mail hostname), then edit/etc/hostsso that the server's public IP resolves to that hostname. A correct hostname is required for HELO checks and TLS certificates. - Create DNS records. In your domain's DNS panel, add an A record for
mail.example.compointing to the VPS IP, an MX record forexample.compointing tomail.example.comwith priority 10, a TXT SPF record such asv=spf1 mx ~all, and a DMARC TXT record at_dmarc.example.comsuch asv=DMARC1; p=none; rua=mailto:postmaster@example.com. Ask your provider to set the PTR record for the IP tomail.example.com. DNS changes can take time to propagate. - Update the system and install packages. Run
apt update && apt upgrade -y, then install Postfix, Dovecot, and the needed helpers:apt install postfix dovecot-core dovecot-imapd dovecot-pop3d mailutils certbot -y. When Postfix prompts for a configuration type, choose "Internet Site" and enter your mail hostname. - Configure Postfix for your domain. Edit
/etc/postfix/main.cfand setmyhostname = mail.example.com,mydomain = example.com,myorigin = $mydomain,inet_interfaces = all,inet_protocols = ipv4,mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, andhome_mailbox = Maildir/. Addsmtpd_tls_cert_fileandsmtpd_tls_key_filepaths later after obtaining certificates. Restart Postfix withsystemctl restart postfix. - Obtain a TLS certificate. Stop any web server on port 80, then run
certbot certonly --standalone -d mail.example.com. Certbot writes the certificate to/etc/letsencrypt/live/mail.example.com/. Point Postfix and Dovecot atfullchain.pemandprivkey.pem, and set up automatic renewal with a deploy hook that reloads both services. - Configure Dovecot. In
/etc/dovecot/conf.d/10-mail.conf, setmail_location = maildir:~/Maildir. In10-ssl.conf, setssl = requiredand the certificate paths. In10-auth.conf, enabledisable_plaintext_auth = yesandauth_mechanisms = plain login. In10-master.conf, enable the IMAP and POP3 listeners and the LMTP or auth socket that Postfix uses. Restart Dovecot and checksystemctl status dovecot. - Open the firewall. Allow ports 25 (SMTP), 465 (SMTPS), 587 (submission), 993 (IMAPS) and 995 (POP3S). On UFW that is
ufw allow 25,465,587,993,995/tcp. Also confirm the VPS provider's external firewall allows the same ports. - Create a mailbox and test. Add a system user with
adduser youand set a strong password. From another machine, configure a mail client with IMAP on port 993 with SSL/TLS and SMTP on port 587 with STARTTLS, using the full addressyou@example.comas the username. Send a test message to an external address and reply to it. Check/var/log/mail.logfor errors.
Common Mistakes
- Forgetting the PTR record. Many receiving servers reject or spam-filter mail from an IP whose reverse DNS does not match the sending hostname.
- Leaving port 25 blocked at the provider and wondering why outgoing mail queues forever.
- Using a self-signed certificate instead of a real one, which causes client warnings and delivery problems.
- Setting an SPF record that does not include the sending IP, or publishing more than one SPF record for the same domain.
- Opening the server as an open relay by misconfiguring
mynetworks; restrict it to localhost only. - Forgetting to enable authentication in Dovecot, so clients cannot log in even though the service is running.
Tips
- Keep the system and mail packages updated; mail servers are frequent targets.
- Use a dedicated hostname such as
mail.example.comrather than the bare domain, so the main site can move independently. - Set up log monitoring for
/var/log/mail.logso you notice delivery failures early. - Start with DMARC
p=noneand tighten toquarantineorrejectonly after reviewing reports. - Consider a relay service for outbound mail if your provider's IP reputation is poor; it is often more reliable than a fresh VPS IP.
Troubleshooting
If mail is not arriving, first check /var/log/mail.log for rejections and queue status with mailq. If outgoing mail is stuck, test port 25 with telnet gmail-smtp-in.l.google.com 25; a timeout means the provider blocks it. If clients cannot connect, verify the firewall, the Dovecot service status, and that the certificate paths are correct. If authentication fails, confirm the username is the full email address and that disable_plaintext_auth is not blocking unencrypted attempts. If messages land in spam, check SPF, DKIM and DMARC alignment, and confirm the PTR record. If Dovecot will not start, run dovecot -n to print the effective configuration and look for syntax errors.
Frequently Asked Questions
Can I run a mail server on a VPS with a dynamic IP?
It is possible but unreliable. Most receiving servers expect a static IP with matching reverse DNS, so a dynamic address will often be rejected or filtered.
Do I need a separate domain for mail?
No. You can use your main domain, but a dedicated mail hostname such as mail.example.com makes DNS and certificate management cleaner.
Why is my outgoing mail queued?
The most common cause is a blocked outbound port 25 or a missing PTR record. Check the provider's policy and the mail log before changing Postfix settings.
Is a self-signed certificate acceptable?
No for public mail. Clients will warn or refuse to connect. Use a certificate from a trusted authority such as Let's Encrypt.
Can I host multiple domains on one server?
Yes. Postfix supports virtual domains and Dovecot supports multiple mail domains through its user database configuration. Each domain needs its own DNS records.
What ports must be open for a mail server?
At minimum 25 for SMTP, 587 for submission, and 993 for IMAPS. Ports 465 and 995 are optional but commonly used for SMTPS and POP3S.