-
How to Set Up a Mail Server on VPS Using
mail.domain.com
and Roundcube WebmailIntroduction
This guide provides a comprehensive walkthrough for setting up a mail server on your VPS, usingmail.domain.com
for secure email communication and Roundcube webmail for account management. We'll also cover opening and checking ports to ensure smooth operation.
System Requirements
- VPS Configuration:
- CPU: 1 core
- RAM: 2 GB minimum
- Storage: 20 GB minimum
- Domain Name: Fully qualified domain like
example.com
. - DNS Management: Access to add DNS records.
- Operating System: CentOS 7/8, AlmaLinux, Ubuntu 20.04, or Debian 11.
Step 1: Log in to Your VPS
Log in using SSH:
ssh root@your-server-ip
Step 2: Update and Prepare Your System
Update your server packages:
yum update -y # CentOS/AlmaLinux apt update && apt upgrade -y # Ubuntu/Debian
Disable
NetworkManager
(if applicable):systemctl stop NetworkManager systemctl disable NetworkManager
Enable the traditional networking service:
systemctl enable network systemctl start network
Step 3: Configure DNS Records for
mail.domain.com
Update your DNS records in your domain registrar or DNS management panel:
Record Type Name Value TTL A mail Your VPS IP address 300 MX @ (root domain) mail.domain.com (priority 10) 300 TXT @ "v=spf1 mx ~all" 300 TXT default._domainkey Public DKIM key (generated later) 300 TXT _dmarc "v=DMARC1; p=none" 300
Step 4: Install and Configure Postfix
Install Postfix:
yum install postfix -y # CentOS/AlmaLinux apt install postfix -y # Ubuntu/Debian
Edit the configuration file:
nano /etc/postfix/main.cf
Update or add the following:
myhostname = mail.domain.com mydomain = domain.com myorigin = $mydomain inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain relayhost = mynetworks = 127.0.0.0/8 [::1]/128 home_mailbox = Maildir/ smtpd_tls_cert_file = /etc/ssl/certs/mail.domain.com.pem smtpd_tls_key_file = /etc/ssl/private/mail.domain.com.key smtpd_use_tls = yes
Start and enable Postfix:
systemctl start postfix systemctl enable postfix
Step 5: Install and Configure Dovecot
Install Dovecot:
yum install dovecot -y # CentOS/AlmaLinux apt install dovecot-core dovecot-imapd -y # Ubuntu/Debian
Edit the configuration file:
nano /etc/dovecot/dovecot.conf
Add or update these lines:
protocols = imap pop3 lmtp listen = * mail_location = maildir:~/Maildir ssl = required ssl_cert = </etc/ssl/certs/mail.domain.com.pem ssl_key = </etc/ssl/private/mail.domain.com.key
Start and enable Dovecot:
systemctl start dovecot systemctl enable dovecot
Step 6: Open Required Ports
Mail servers require specific ports to function:
- Port 25: SMTP (sending emails, used by servers).
- Port 587: SMTP with STARTTLS (sending emails, used by clients).
- Port 993: IMAP over SSL (retrieving emails).
- Port 995: POP3 over SSL (optional).
Steps to Open Ports
For firewalld:
firewall-cmd --permanent --add-service=smtp firewall-cmd --permanent --add-service=smtps firewall-cmd --permanent --add-service=imap firewall-cmd --permanent --add-service=imaps firewall-cmd --reload
For UFW:
ufw allow 25 ufw allow 587 ufw allow 993 ufw allow 995 ufw reload
Step 7: Check Open Ports
Use online tools like MXToolbox Port Check to verify your ports are accessible.
You can also check locally with:
netstat -tuln | grep ':25\|:587\|:993\|:995'
Step 8: Secure the Mail Server with SSL/TLS
Install Let's Encrypt:
yum install certbot -y # CentOS/AlmaLinux apt install certbot -y # Ubuntu/Debian
Generate SSL certificates:
certbot certonly --standalone -d mail.domain.com
Link the certificates:
ln -s /etc/letsencrypt/live/mail.domain.com/fullchain.pem /etc/ssl/certs/mail.domain.com.pem ln -s /etc/letsencrypt/live/mail.domain.com/privkey.pem /etc/ssl/private/mail.domain.com.key
Restart Postfix and Dovecot:
systemctl restart postfix systemctl restart dovecot
Step 9: Install and Configure Roundcube Webmail
Install Apache and PHP:
yum install httpd php php-mysqlnd php-intl php-json -y # CentOS/AlmaLinux apt install apache2 php libapache2-mod-php php-mysql php-intl php-json -y # Ubuntu/Debian
Download Roundcube:
wget https://github.com/roundcube/roundcubemail/releases/download/1.6.1/roundcubemail-1.6.1-complete.tar.gz tar -xvzf roundcubemail-1.6.1-complete.tar.gz -C /var/www/html mv /var/www/html/roundcubemail-1.6.1 /var/www/html/roundcube
Set permissions and configure Roundcube as per your mail server details.
Troubleshooting
Emails Not Sending/Receiving
- Check logs:
tail -f /var/log/maillog # CentOS/AlmaLinux tail -f /var/log/mail.log # Ubuntu/Debian
Port Issues
- Use
telnet
to check if a port is open:telnet your-server-ip 25
- Ensure the firewall allows the required ports.
FAQ
-
How Do I Access Webmail?
Visithttp://mail.domain.com/roundcube
. -
Can I Use Custom Email Addresses?
Yes, create accounts using:useradd username passwd username
-
What Tools Can Test My Setup?
- MXToolbox for DNS and port verification.
mail-tester.com
for email configuration testing.
- VPS Configuration:
Conclusion
This guide provides a complete roadmap to set up a mail server on your VPS with Roundcube webmail. By properly configuring Postfix, Dovecot, DNS, and webmail, you can offer a reliable and secure email solution for your hosting clients. If you encounter any issues, refer to the troubleshooting section or contact our support team.