Set Up Web & Mail Server on Two VPS
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
- Set up DNS records. In your DNS provider’s panel, create an A record for
web.example.compointing to the web server’s IP, and another A record formail.example.compointing to the mail server’s IP. Also create an MX record forexample.compointing tomail.example.comwith 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. - 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. - 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 anindex.htmlfile with a simple test message. Configure a server block (virtual host) in/etc/nginx/sites-available/example.comwith the server nameweb.example.comand 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. - 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 enterexample.comas the mail name. - Configure Postfix. Edit
/etc/postfix/main.cf. Setmyhostname = mail.example.com,mydomain = example.com,myorigin = $mydomain,inet_interfaces = all, andmydestination = $myhostname, localhost.$mydomain, localhost, $mydomain. Add a line for the virtual alias domain if needed. Restart Postfix:sudo systemctl restart postfix. - Configure Dovecot. Edit
/etc/dovecot/dovecot.confto enable IMAP and POP3 (they are usually enabled by default). Ensure the mail location is set tomaildir:~/Maildirin/etc/dovecot/conf.d/10-mail.conf. Restart Dovecot:sudo systemctl restart dovecot. - Create a mail user. Create a system user for email:
sudo useradd -m -s /bin/bash user@example.com(or justuserif you prefer). Set a password:sudo passwd user. This user will be able to log in via IMAP/POP3. - Set up SSL/TLS (recommended). Install Certbot on both servers:
sudo apt install certbot python3-certbot-nginx(web) andsudo apt install certbot python3-certbot-postfix(mail). Obtain certificates forweb.example.comandmail.example.comrespectively. For Postfix, usesudo certbot certonly --standalone -d mail.example.com(stop Postfix temporarily) or use the webroot method. Configure Postfix to use the certificate paths inmain.cfand setsmtpd_tls_cert_fileandsmtpd_tls_key_file. Restart Postfix and Dovecot. - Test the web server. From your local machine, visit
http://web.example.com(orhttpsif you set up SSL). You should see your test page. - Test the mail server. Use a mail client (e.g., Thunderbird) to connect to
mail.example.comwith 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.logfor errors.
Common Mistakes
- Incorrect DNS propagation. DNS changes can take time. Verify with
dig web.example.comanddig 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_interfacesis set tolocalhost, 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 withcurl localhost. - Mail server not accepting connections. Check Postfix status:
sudo systemctl status postfix. Test withtelnet 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.