ThisCalculator

Everyday

Set Up Web & Mail Server on Two VPS

Updated · Standard formula, shown below

This guide walks you through setting up a web server on one VPS and a mail server on a separate VPS. This separation improves security (a compromised web server doesn’t directly expose your mail data), simplifies resource allocation, and makes troubleshooting easier. You’ll need two VPS instances with Ubuntu 22.04 or 24.04 LTS, root access, and a domain name with DNS control.

What You’ll Need

  • Two VPS instances (any provider) with Ubuntu 22.04 or 24.04 LTS installed.
  • Root or sudo access to both servers.
  • A domain name (e.g., example.com) with DNS management access.
  • Basic familiarity with the command line and SSH.
  • A static public IP for each VPS (most providers assign one automatically).

Step-by-Step

  1. Set up DNS records. In your DNS provider’s panel, create an A record for web.example.com pointing to the web server’s IP, and another A record for mail.example.com pointing to the mail server’s IP. Also create an MX record for example.com pointing to mail.example.com with priority 10. Add a TXT record for SPF: v=spf1 mx a:mail.example.com -all. If you plan to use DKIM, you’ll add the key later.
  2. Configure the web server VPS. SSH into the web VPS. Update the system: sudo apt update && sudo apt upgrade -y. Install Nginx (or Apache) and PHP: sudo apt install nginx php-fpm php-mysql -y. Start and enable Nginx: sudo systemctl enable --now nginx.
  3. Create a web directory and test page. Create a directory for your site: sudo mkdir -p /var/www/example.com/html. Set ownership: sudo chown -R $USER:$USER /var/www/example.com. Create an index.html file with a simple test message. Configure a server block (virtual host) in /etc/nginx/sites-available/example.com with the server name web.example.com and root pointing to that directory. Enable it: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ and test: sudo nginx -t. Reload Nginx: sudo systemctl reload nginx.
  4. Install and configure the mail server on the second VPS. SSH into the mail VPS. Update and install Postfix and Dovecot: sudo apt update && sudo apt install postfix dovecot-imapd dovecot-pop3d -y. During Postfix installation, choose “Internet Site” and enter example.com as the mail name.
  5. Configure Postfix. Edit /etc/postfix/main.cf. Set myhostname = mail.example.com, mydomain = example.com, myorigin = $mydomain, inet_interfaces = all, and mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain. Add a line for the virtual alias domain if needed. Restart Postfix: sudo systemctl restart postfix.
  6. Configure Dovecot. Edit /etc/dovecot/dovecot.conf to enable IMAP and POP3 (they are usually enabled by default). Ensure the mail location is set to maildir:~/Maildir in /etc/dovecot/conf.d/10-mail.conf. Restart Dovecot: sudo systemctl restart dovecot.
  7. Create a mail user. Create a system user for email: sudo useradd -m -s /bin/bash user@example.com (or just user if you prefer). Set a password: sudo passwd user. This user will be able to log in via IMAP/POP3.
  8. Set up SSL/TLS (recommended). Install Certbot on both servers: sudo apt install certbot python3-certbot-nginx (web) and sudo apt install certbot python3-certbot-postfix (mail). Obtain certificates for web.example.com and mail.example.com respectively. For Postfix, use sudo certbot certonly --standalone -d mail.example.com (stop Postfix temporarily) or use the webroot method. Configure Postfix to use the certificate paths in main.cf and set smtpd_tls_cert_file and smtpd_tls_key_file. Restart Postfix and Dovecot.
  9. Test the web server. From your local machine, visit http://web.example.com (or https if you set up SSL). You should see your test page.
  10. Test the mail server. Use a mail client (e.g., Thunderbird) to connect to mail.example.com with the user you created. Send a test email to an external address and also from an external address to your new mailbox. Check the mail logs: sudo tail -f /var/log/mail.log for errors.

Common Mistakes

  • Incorrect DNS propagation. DNS changes can take time. Verify with dig web.example.com and dig mx example.com.
  • Firewall blocking ports. Ensure ports 80/443 (web) and 25, 587, 993, 995 (mail) are open in your VPS firewall (ufw).
  • Postfix not listening on all interfaces. If inet_interfaces is set to localhost, external connections fail.
  • Missing SPF record. Without SPF, emails may be flagged as spam.
  • Using the same SSL certificate for both servers. Each server needs its own certificate for its own hostname.

Tips

  • Use strong passwords and consider SSH key authentication for both VPS.
  • Set up a firewall (UFW) on both servers. Allow only necessary ports.
  • For the mail server, consider adding DKIM: install opendkim, generate a key, add a TXT record with the public key, and configure Postfix to sign outgoing mail.
  • Regularly update both systems: sudo apt upgrade.
  • Monitor logs: web server access/error logs and mail logs for issues.

Troubleshooting

  • Web server not reachable. Check Nginx status: sudo systemctl status nginx. Check firewall: sudo ufw status. Test locally with curl localhost.
  • Mail server not accepting connections. Check Postfix status: sudo systemctl status postfix. Test with telnet mail.example.com 25. Look for errors in /var/log/mail.log.
  • Emails going to spam. Verify SPF, DKIM, and reverse DNS (PTR) for your mail server IP. Some providers require you to set a PTR record in their control panel.
  • SSL certificate errors. Ensure the certificate paths are correct and the certificate is not expired. Test with openssl s_client -connect mail.example.com:993.
  • Dovecot authentication fails. Check that the user exists and the password is correct. Check Dovecot logs: sudo journalctl -u dovecot.

Frequently Asked Questions

Can I use the same VPS for both web and mail?

Yes, but the guide separates them for security and performance. If you use one VPS, you can install both Nginx and Postfix on it, but you lose the isolation benefits.

Do I need a dedicated IP for the mail server?

Not strictly, but a dedicated IP helps with email deliverability and avoids blacklisting due to other users on the same IP.

How do I create multiple email accounts?

Create additional system users on the mail VPS. Each user gets a mailbox in /home/username/Maildir.

Can I use a subdomain for mail like mail.example.com?

Yes, that’s the standard approach. The MX record points to that subdomain.

What is the difference between IMAP and POP3?

IMAP keeps emails on the server and syncs across devices. POP3 downloads emails to a single client and removes them from the server by default.

How do I renew SSL certificates?

Certbot can set up automatic renewal with a cron job or systemd timer. Test with sudo certbot renew --dry-run.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.