TechBuzzPro

How to Set Up an Email Server on a VPS

· 6 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. Set the hostname. Run hostnamectl set-hostname mail.example.com (replace with your mail hostname), then edit /etc/hosts so that the server's public IP resolves to that hostname. A correct hostname is required for HELO checks and TLS certificates.
  3. Create DNS records. In your domain's DNS panel, add an A record for mail.example.com pointing to the VPS IP, an MX record for example.com pointing to mail.example.com with priority 10, a TXT SPF record such as v=spf1 mx ~all, and a DMARC TXT record at _dmarc.example.com such as v=DMARC1; p=none; rua=mailto:postmaster@example.com. Ask your provider to set the PTR record for the IP to mail.example.com. DNS changes can take time to propagate.
  4. 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.
  5. Configure Postfix for your domain. Edit /etc/postfix/main.cf and set myhostname = mail.example.com, mydomain = example.com, myorigin = $mydomain, inet_interfaces = all, inet_protocols = ipv4, mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, and home_mailbox = Maildir/. Add smtpd_tls_cert_file and smtpd_tls_key_file paths later after obtaining certificates. Restart Postfix with systemctl restart postfix.
  6. 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 at fullchain.pem and privkey.pem, and set up automatic renewal with a deploy hook that reloads both services.
  7. Configure Dovecot. In /etc/dovecot/conf.d/10-mail.conf, set mail_location = maildir:~/Maildir. In 10-ssl.conf, set ssl = required and the certificate paths. In 10-auth.conf, enable disable_plaintext_auth = yes and auth_mechanisms = plain login. In 10-master.conf, enable the IMAP and POP3 listeners and the LMTP or auth socket that Postfix uses. Restart Dovecot and check systemctl status dovecot.
  8. 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.
  9. Create a mailbox and test. Add a system user with adduser you and 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 address you@example.com as the username. Send a test message to an external address and reply to it. Check /var/log/mail.log for 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.com rather than the bare domain, so the main site can move independently.
  • Set up log monitoring for /var/log/mail.log so you notice delivery failures early.
  • Start with DMARC p=none and tighten to quarantine or reject only 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.