{"id":4877,"date":"2025-06-02T22:38:29","date_gmt":"2025-06-02T19:38:29","guid":{"rendered":"https:\/\/avenacloud.com\/blog\/?p=4877"},"modified":"2025-06-02T22:38:31","modified_gmt":"2025-06-02T19:38:31","slug":"set-up-a-mail-server-on-vps-easy-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/avenacloud.com\/blog\/set-up-a-mail-server-on-vps-easy-step-by-step-guide\/","title":{"rendered":"Set Up a Mail Server on VPS: Easy Step-by-Step Guide"},"content":{"rendered":"<p>Setting up your own mail server might sound like a daunting task, but it doesn\u2019t have to be. If you\u2019re looking to take control of your email hosting, maintain privacy, and avoid costly third-party services, you\u2019ve come to the right place. In this comprehensive guide, we\u2019ll walk you through <strong>how to easily set up a mail server on your VPS<\/strong> using straightforward techniques and proven best practices.<\/p>\n<p>This isn\u2019t just about getting email to work; it\u2019s about building a secure, reliable, and professional mailing system that scales with your needs. Whether you\u2019re a small business owner, developer, or tech enthusiast, this guide is tailored for you.<\/p>\n<h2>Why Set Up a Mail Server on Your VPS?<\/h2>\n<p>Before diving into the technical steps, it\u2019s important to understand why hosting your own mail server on a VPS is worth considering.<\/p>\n<ul>\n<li><strong>Full Control:<\/strong> Manage your email accounts, storage, and security protocols without restrictions.<\/li>\n<li><strong>Cost Savings:<\/strong> Avoid monthly fees on external email services by leveraging your VPS resources.<\/li>\n<li><strong>Privacy and Security:<\/strong> Keep your communication private without sharing data with third parties.<\/li>\n<li><strong>Customization:<\/strong> Tailor spam filters, forwarding rules, and user permissions to match your needs.<\/li>\n<li><strong>Professionalism:<\/strong> Use your own domain email addresses enhancing your brand identity.<\/li>\n<\/ul>\n<h2>Requirements and Preparations for Setting Up Your Mail Server<\/h2>\n<p>To start, ensure you meet the baseline requirements and understand what resources you will need for a successful mail server setup.<\/p>\n<h3>What You Need Before You Begin<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4882 aligncenter\" title=\"How to Easily Set Up a Mail Server on Your VPS. What You Need Before You Begin\" src=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/4d30e5132685d054e0b7d59bf04a82ee.jpg\" alt=\"How to Easily Set Up a Mail Server on Your VPS. What You Need Before You Begin\" width=\"1024\" height=\"768\" srcset=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/4d30e5132685d054e0b7d59bf04a82ee.jpg 1024w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/4d30e5132685d054e0b7d59bf04a82ee-300x225.jpg 300w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/4d30e5132685d054e0b7d59bf04a82ee-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<ul>\n<li><strong>A VPS with Linux OS:<\/strong> Preferably Ubuntu or Debian, known for strong community support.<\/li>\n<li><strong>A Domain Name:<\/strong> Required for sending and receiving professional emails.<\/li>\n<li><strong>Basic Linux <a href=\"https:\/\/avenacloud.com\/blog\/how-to-install-pip-on-windows\/\">Command<\/a> Line Knowledge:<\/strong> Comfort with SSH and terminal commands.<\/li>\n<li><strong>Root or Sudo Access:<\/strong> Full administrative rights on the VPS.<\/li>\n<li><strong>Updated DNS Settings:<\/strong> Ability to modify MX, SPF, DKIM, and DMARC records in your domain hosting panel.<\/li>\n<\/ul>\n<h3>Choosing the Right VPS Plan<\/h3>\n<p>Your VPS should have enough resources to handle your expected email volume efficiently. Here\u2019s a quick guideline:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Estimated Email Volume<\/th>\n<th>Recommended RAM<\/th>\n<th>Recommended CPU<\/th>\n<th>Storage<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Up to 100 emails\/day<\/td>\n<td>1 GB<\/td>\n<td>1 Core<\/td>\n<td>20 GB SSD<\/td>\n<\/tr>\n<tr>\n<td>Up to 1,000 emails\/day<\/td>\n<td>2-4 GB<\/td>\n<td>2 Cores<\/td>\n<td>50 GB SSD<\/td>\n<\/tr>\n<tr>\n<td>10,000+ emails\/day<\/td>\n<td>8+ GB<\/td>\n<td>4+ Cores<\/td>\n<td>100 GB+ SSD<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Pro tip: Start small and scale your VPS as your email volume and storage needs grow.<\/p>\n<h2>Step 1: Secure Your VPS for a Mail Server Environment<\/h2>\n<p>Security is non-negotiable when setting up your mail server. Early steps ensure your VPS is locked down against unauthorized access.<\/p>\n<h3>Update Your VPS Packages<\/h3>\n<p>Run these commands to update your <a href=\"https:\/\/avenacloud.com\/blog\/how-to-install-pip-on-windows\/\">package<\/a> index and upgrade installed <a href=\"https:\/\/avenacloud.com\/blog\/how-to-install-pip-on-windows\/\">packages<\/a>:<\/p>\n<pre><code>sudo apt update\nsudo apt upgrade -y<\/code><\/pre>\n<h3>Set Up a Strong Firewall<\/h3>\n<p>Limit open ports to only what the mail server requires (SMTP, IMAP\/POP3, Webmail if any).<\/p>\n<pre><code>sudo ufw allow ssh\nsudo ufw allow 25\/tcp    # SMTP\nsudo ufw allow 143\/tcp   # IMAP\nsudo ufw allow 993\/tcp   # IMAPS (secure)\nsudo ufw allow 587\/tcp   # SMTP Submission\nsudo ufw enable<\/code><\/pre>\n<p>This setup shields your VPS while allowing necessary mail traffic.<\/p>\n<h3>Create a New User for Mail Administration<\/h3>\n<p>It\u2019s best practice not to work as root. Create a dedicated admin user:<\/p>\n<pre><code>sudo adduser mailadmin\nsudo usermod -aG sudo mailadmin\n<\/code><\/pre>\n<p>Now, switch to this user before installing and configuring software.<\/p>\n<h2>Step 2: Install and Configure the Mail Server Software<\/h2>\n<p>Several software options exist for mail servers, but Postfix and Dovecot are industry standards for SMTP and IMAP\/POP3 respectively.<\/p>\n<h3>Install Postfix and Dovecot<\/h3>\n<pre><code>sudo apt install postfix dovecot-core dovecot-imapd -y<\/code><\/pre>\n<p>During Postfix installation, select \u201cInternet Site\u201d when prompted and enter your domain name.<\/p>\n<h3>Configure Postfix for Sending and Receiving Emails<\/h3>\n<p>Edit Postfix main configuration file:<\/p>\n<pre><code>sudo nano \/etc\/postfix\/main.cf<\/code><\/pre>\n<p>Adjust the following lines (replace example.com with your domain):<\/p>\n<pre><code>myhostname = mail.example.com\nmydomain = example.com\nmyorigin = \/etc\/mailname\nmydestination = $myhostname, localhost.$mydomain, localhost, $mydomain\nrelayhost = \nmynetworks = 127.0.0.0\/8\ninet_interfaces = all\ninet_protocols = ipv4<\/code><\/pre>\n<p>Save and exit. Reload Postfix:<\/p>\n<pre><code>sudo systemctl restart postfix<\/code><\/pre>\n<h3>Configure Dovecot for Email Retrieval<\/h3>\n<p>Edit Dovecot\u2019s main configuration files to enable IMAP and set mail <a href=\"https:\/\/avenacloud.com\/blog\/how-to-install-pip-on-windows\/\">directory<\/a>:<\/p>\n<pre><code>sudo nano \/etc\/dovecot\/dovecot.conf\n# Ensure this line is present\nprotocols = imap pop3 lmtp\n<\/code><\/pre>\n<p>Set mail location:<\/p>\n<pre><code>sudo nano \/etc\/dovecot\/conf.d\/10-mail.conf\n# mail_location = maildir:~\/Maildir\nmail_location = maildir:\/home\/%u\/Maildir<\/code><\/pre>\n<p>Create Maildir folder for your mail user:<\/p>\n<pre><code>sudo mkdir -p \/home\/mailadmin\/Maildir\nsudo chown -R mailadmin:mailadmin \/home\/mailadmin\/Maildir\nsudo chmod -R 700 \/home\/mailadmin\/Maildir<\/code><\/pre>\n<p>Restart Dovecot:<\/p>\n<pre><code>sudo systemctl restart dovecot<\/code><\/pre>\n<h2>Step 3: Secure Your Mail Server with SMTP Authentication and Encryption<\/h2>\n<p>To prevent unauthorized use and protect data in transit, securing your mail server is vital.<\/p>\n<h3>Enable SMTP Authentication in Postfix<\/h3>\n<p>Edit your Postfix configuration to enable SASL authentication:<\/p>\n<pre><code>sudo nano \/etc\/postfix\/main.cf<\/code><\/pre>\n<p>Add or update:<\/p>\n<pre><code>smtpd_sasl_type = dovecot\nsmtpd_sasl_path = private\/auth\nsmtpd_sasl_auth_enable = yes\nsmtpd_tls_security_level = may\nsmtpd_tls_auth_only = yes\n<\/code><\/pre>\n<p>Reload Postfix:<\/p>\n<pre><code>sudo systemctl restart postfix<\/code><\/pre>\n<h3>Install and Configure SSL\/TLS for Encryption<\/h3>\n<p>Encrypt email traffic with <a href=\"https:\/\/avenacloud.com\/blog\/ssl-for-e-commerce-website\/\">SSL certificates<\/a>. The easiest way is to use Let\u2019s Encrypt.<\/p>\n<ul>\n<li>Install Certbot:<\/li>\n<\/ul>\n<pre><code>sudo apt install certbot -y<\/code><\/pre>\n<ul>\n<li>Obtain SSL certificate (replace mail.example.com):<\/li>\n<\/ul>\n<pre><code>sudo certbot certonly --standalone -d mail.example.com<\/code><\/pre>\n<p>Configure Postfix to use the SSL certificate:<\/p>\n<pre><code>sudo nano \/etc\/postfix\/main.cf<\/code><\/pre>\n<pre><code>smtpd_tls_cert_file = \/etc\/letsencrypt\/live\/mail.example.com\/fullchain.pem\nsmtpd_tls_key_file = \/etc\/letsencrypt\/live\/mail.example.com\/privkey.pem\nsmtpd_use_tls = yes<\/code><\/pre>\n<p>For Dovecot also configure SSL:<\/p>\n<pre><code>sudo nano \/etc\/dovecot\/conf.d\/10-ssl.conf\nssl = required\nssl_cert = &lt;\/etc\/letsencrypt\/live\/mail.example.com\/fullchain.pem\nssl_key = &lt;\/etc\/letsencrypt\/live\/mail.example.com\/privkey.pem<\/code><\/pre>\n<p>Restart services:<\/p>\n<pre><code>sudo systemctl restart postfix dovecot<\/code><\/pre>\n<h2>Step 4: Configure DNS Records for Mail Deliverability and Spam Prevention<\/h2>\n<p>Correct DNS settings are crucial to ensure your emails reach inboxes and are not marked as spam.<\/p>\n<h3>Update Your Domain\u2019s DNS with These Records:<\/h3>\n<ol>\n<li><strong>MX Record:<\/strong> Points to your mail server.<\/li>\n<li><strong>SPF Record:<\/strong> Authorizes your server&#8217;s IP to send email for your domain.<\/li>\n<li><strong>DKIM Record:<\/strong> Signs outgoing messages to verify they\u2019re from you.<\/li>\n<li><strong>DMARC Record:<\/strong> Instructs receiving servers how to handle unauthorized emails.<\/li>\n<\/ol>\n<h3>Example DNS Records<\/h3>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Type<\/th>\n<th>Name\/Host<\/th>\n<th>Value\/Data<\/th>\n<th>TTL<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>MX<\/td>\n<td>@<\/td>\n<td>10 mail.example.com<\/td>\n<td>3600<\/td>\n<\/tr>\n<tr>\n<td>TXT (SPF)<\/td>\n<td>@<\/td>\n<td>&#8220;v=spf1 mx ip4:YOUR_VPS_IP -all&#8221;<\/td>\n<td>3600<\/td>\n<\/tr>\n<tr>\n<td>TXT (DKIM)<\/td>\n<td>default._domainkey<\/td>\n<td>&#8220;v=DKIM1; k=rsa; p=YOUR_PUBLIC_DKIM_KEY&#8221;<\/td>\n<td>3600<\/td>\n<\/tr>\n<tr>\n<td>TXT (DMARC)<\/td>\n<td>_dmarc<\/td>\n<td>&#8220;v=DMARC1; p=none; rua=mailto:postmaster@example.com&#8221;<\/td>\n<td>3600<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note: Generating DKIM keys requires additional steps using a tool such as OpenDKIM, which we\u2019ll cover next.<\/p>\n<h2>Step 5: Set Up DKIM for Email Authentication<\/h2>\n<p>DKIM (DomainKeys Identified Mail) adds a digital signature to your emails, improving trust and inbox placement.<\/p>\n<h3>Install OpenDKIM<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4880 aligncenter\" title=\"How to Easily Set Up a Mail Server on Your VPS. Install OpenDKIM\" src=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/707fb50f0eaac15c06dc3dcbb033adcf.jpg\" alt=\"How to Easily Set Up a Mail Server on Your VPS. Install OpenDKIM\" width=\"1024\" height=\"768\" srcset=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/707fb50f0eaac15c06dc3dcbb033adcf.jpg 1024w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/707fb50f0eaac15c06dc3dcbb033adcf-300x225.jpg 300w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/707fb50f0eaac15c06dc3dcbb033adcf-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<pre><code>sudo apt install opendkim opendkim-tools -y<\/code><\/pre>\n<h3>Configure OpenDKIM<\/h3>\n<p>Create and edit the configuration:<\/p>\n<pre><code>sudo nano \/etc\/opendkim.conf<\/code><\/pre>\n<p>Paste or update with:<\/p>\n<pre><code>AutoRestart             Yes\nAutoRestartRate         10\/1h\nSyslog                  yes\nUMask                   002\nCanonicalization        relaxed\/simple\nMode                    sv\nSubDomains              no\nSocket                  inet:12345@localhost\nPidFile                 \/var\/run\/opendkim\/opendkim.pid\nUserID                  opendkim\nKeyTable                \/etc\/opendkim\/key.table\nSigningTable            \/etc\/opendkim\/signing.table\nTrustedHosts            \/etc\/opendkim\/trusted.hosts<\/code><\/pre>\n<h3>Create Key Directories and Generate Keys<\/h3>\n<pre><code>sudo mkdir \/etc\/opendkim\/keys\/example.com\nsudo opendkim-genkey -b 2048 -d example.com -D \/etc\/opendkim\/keys\/example.com -s default -v\nsudo chown -R opendkim:opendkim \/etc\/opendkim\/keys\/example.com\n<\/code><\/pre>\n<h3>Configure KeyTable, SigningTable, and TrustedHosts<\/h3>\n<pre><code>echo \"default._domainkey.example.com example.com:default:\/etc\/opendkim\/keys\/example.com\/default.private\" | sudo tee \/etc\/opendkim\/key.table\necho \"*@example.com default._domainkey.example.com\" | sudo tee \/etc\/opendkim\/signing.table\necho \"127.0.0.1\nlocalhost\n192.168.1.0\/24\" | sudo tee \/etc\/opendkim\/trusted.hosts\n<\/code><\/pre>\n<h3>Integrate OpenDKIM with Postfix<\/h3>\n<pre><code>sudo nano \/etc\/postfix\/main.cf<\/code><\/pre>\n<p>Add:<\/p>\n<pre><code>milter_default_action = accept\nmilter_protocol = 6\nsmtpd_milters = inet:localhost:12345\nnon_smtpd_milters = inet:localhost:12345<\/code><\/pre>\n<p>Restart services:<\/p>\n<pre><code>sudo systemctl restart opendkim\nsudo systemctl restart postfix<\/code><\/pre>\n<h3>Add DKIM TXT Record to DNS<\/h3>\n<p>Retrieve your DKIM public key:<\/p>\n<pre><code>sudo cat \/etc\/opendkim\/keys\/example.com\/default.txt<\/code><\/pre>\n<p>Add the contents (without the selector and domain part) as a new TXT record named <code>default._domainkey<\/code> for your domain.<\/p>\n<h2>Step 6: Test Your Mail Server Setup<\/h2>\n<p>Before relying on your mail server, thorough testing assures everything is running smoothly.<\/p>\n<h3>Send a Test Email<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4881 aligncenter\" title=\"How to Easily Set Up a Mail Server on Your VPS. Send a Test Email\" src=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/88c14622d11822593de3d39cdf9ea8a4.jpg\" alt=\"How to Easily Set Up a Mail Server on Your VPS. Send a Test Email\" width=\"1024\" height=\"768\" srcset=\"https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/88c14622d11822593de3d39cdf9ea8a4.jpg 1024w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/88c14622d11822593de3d39cdf9ea8a4-300x225.jpg 300w, https:\/\/avenacloud.com\/blog\/wp-content\/uploads\/2025\/05\/88c14622d11822593de3d39cdf9ea8a4-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<ul>\n<li>Use an email client (Outlook, Thunderbird, etc.) configured with your new mail server credentials.<\/li>\n<li>Send mail to an external account (e.g., Gmail) and check delivery and spam folder placement.<\/li>\n<\/ul>\n<h3>Check DNS Records Propagation<\/h3>\n<ul>\n<li>Use online tools like MX Toolbox or DNSChecker to verify MX, SPF, DKIM, and DMARC records are active.<\/li>\n<\/ul>\n<h3>Verify Mail Server on VPS<\/h3>\n<p>Test Postfix and Dovecot functionality directly on the server:<\/p>\n<pre><code>telnet localhost 25\ntelnet localhost 143<\/code><\/pre>\n<h2>Step 7: Optional Enhancements and Maintenance<\/h2>\n<p>Your mail server is live, but further optimizations improve security, efficiency, and user experience.<\/p>\n<h3>Spam Filtering with SpamAssassin<\/h3>\n<p>Reduce junk mail with SpamAssassin:<\/p>\n<pre><code>sudo apt install spamassassin -y\nsudo systemctl enable spamassassin\nsudo systemctl start spamassassin\n<\/code><\/pre>\n<h3>Webmail Access<\/h3>\n<p>Consider installing webmail clients like Roundcube for browser-based email:<\/p>\n<pre><code>sudo apt install roundcube roundcube-core roundcube-mysql -y<\/code><\/pre>\n<h3>Regular Updates and Backups<\/h3>\n<ul>\n<li>Keep your software updated with frequent upgrades.<\/li>\n<li>Back up configuration files and mailboxes regularly.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Learning <strong>how to easily set up a mail server on your VPS<\/strong> opens doors to complete email control, enhanced privacy, and significant cost savings. By following this step-by-step guide, you\u2019ve not only built a functioning mail server but also secured and optimized it for reliability.<\/p>\n<p>Don&#8217;t settle for third-party services that limit your freedom\u2014take charge today. Start by securing your domain DNS properly, configure authentication, and continuously monitor your server\u2019s performance. The benefits of running your own mail server are worth every effort.<\/p>\n<p>Ready to elevate your business communication? Set up your mail server now, and experience email hosting on your own terms!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up your own mail server might sound like a daunting task, but it doesn\u2019t have to be. If you\u2019re looking to take control of your email hosting, maintain privacy, and avoid costly third-party services, you\u2019ve come to the right&#8230; <\/p>\n","protected":false},"author":6,"featured_media":4879,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1716,1717],"class_list":["post-4877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps-vds","tag-set-up-a-mail-server-on-vps","tag-set-up-mail-server-step-by-step"],"_links":{"self":[{"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/posts\/4877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/comments?post=4877"}],"version-history":[{"count":3,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/posts\/4877\/revisions"}],"predecessor-version":[{"id":4975,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/posts\/4877\/revisions\/4975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/media\/4879"}],"wp:attachment":[{"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/media?parent=4877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/categories?post=4877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avenacloud.com\/blog\/wp-json\/wp\/v2\/tags?post=4877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}