How to Easily Set Up a Personal Email Server in 2022

A fully validated email server that comes with a DNS manager and full Let's Encrypt SSL activation.
Table of Contents

    Introduction

    If you run server(s), VPS’s or Server Apps (Cloudpanel, WHM/Cpanel, Vesta, Ajenti, Cyberpanel etc) and/or have a few domains (and you are on a small budget like me) you will notice that the one thing that can ruin a project is either 1) Having to pay extra for an email inbox for said domain or, 2) trying to set up your own email server to find out that the VPS host has port 25 blocked.

    Even then, let’s say you have those problems solved but now certain inboxes will not accept your mail because of “invalid DKIM” or something similar. The whole process can be an absolute headache.

    This guide aims to guide you through setting up an email server for all your domains for only $5 a month

    You will need a server with port 25 open for this guide. I will go through how to contact your server provider and request to open port 25.

    Port 25

    Scaleway.com

    EDIT: Since writing, I have noticed that Scaleway.com actually allow SMTP from their VPS or what they call “Instances”. Their great servers and if you can manage to grab one on the stardust level you can get a whole AMD EPYC CPU core allocated for only about £1.50 a month. To enable SMTP you need to verify your id which is easily done automatically. Further information on how to enable SMTP. Currently, this is the easiest and “hassle-free” method of enabling SMTP on your VPS’s.

    Be aware that Scaleway do not currently have an option to set the reverse address PTR for IPv6 addresses. This can cause issues when sending to gmail addresses.

    Scaleway Stardust Instance Specs

    Atlantic.net

    You can complete this guide with a free VPS from Atlantic.net but you will need to email their support to open port 25. You can also use Hetzner and email their support to open port 25.

    DigitalOcean.com

    DigitalOcean.com will also offer the same service but from my experience can be stricter.

    Vultr.com

    Vultr.com are the same as DigitalOcean you have to fill out forms and send them back but usually, it will go through fine. You can get $100 free on Vultr.com by clicking here.

    What to do?

    Simply email the support staff requesting to open port 25 because you need it for development purposes. They will probably ask why you need it exactly and how many emails you intend to send per month. Depending on your usage explain what you need and they will usually oblige.

    Hetzner

    I used Hetzner and they were quick to respond and opened port 25 for ALL my VPS’s.

    In my example, I said I needed it for development purposes and I estimated to send around 100-150 emails per month.

    Requirements

    So now we have our VPS. It’s super small depending on what route you took. Most providers have different plans. Atlantic.net’s offer is particularly good with a server with 2GB RAM.

    Regardless of which provider you choose you will need a minimum of only 512MB RAM on the VPS. Most VPS of this size cost $5 or less.

    You will need to be able to set your domain’s Glue Records

    Server Specs

    • Server with 512MB RAM
    • At least 1 core.
    • Ubuntu 22.04 Only
    • A domain you control at your domain provider. (Namecheap, Godaddy, Porkbun etc)
    • The VPS hostname must be set as box.yourdomain.com (obviously, replace your domain.com with your actual domain).
    • You will need FULL control over your domain nameservers. If you have subdomains you can set them up later.
    • When you have created your VPS set the Reverse Address or PTR to your hostname that you will be using.

    Pre-install

    So we have our VM, Ubuntu 22.04 and it’s booted up. Let’s go through a few commands to update the system and set our hostname.

    Update and upgrade the system.

    apt update && apt upgrade -y

    Now reboot

    reboot

    Once rebooted, let’s set our hostname. Ubuntu 22.04 uses systemctl to set the hostname.

    systemctl set-hostname box.yourdomain.com --static

    Reboot again

    reboot

    Now we need to set our reverse DNS on the VPS. It varies for all providers but it’s the same output. Change the reverse to the domain you have set up eg, box.yourdomain.com

    This is how it looks in Hetzner.

    IPv4

    IPv6

    IPv6 Setup

    You might need to add your IPv6 address to your Ubuntu VM if it is not done automatically. Most hosts are different in how they deal with provisioning IPv6. Sometimes they will add it automatically sometimes they don’t.

    To edit the VM’s IPv6 address we do this with netplan.

    Find out your IPv6 address, Its normally in the format 2a01:5f8:c0c:cca4::1 the ::1 on the end basically says your using the first available IPv6 address in that block or number 1. If you had other IPv6 addresses on the same network they would have ::2 or ::3 etc.

    Open a ssh session to your VM

    ssh [email protected]

    Edit the cloud-init file

    nano /etc/netplan/50-cloud-init.yaml

    We need to add our IPv6 address at the bottom replacing the dummy IP 2a01:4ff:ff00::add:1.

    # This file is generated from information provided by the datasource.  Changes
    # to it will not persist across an instance reboot.  To disable cloud-init's
    # network configuration capabilities, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    network:
       version: 2
       ethernets:
          eth0:
                addresses:
                - 2001:db8:1234::1/64
                dhcp4: true
                gateway6: fe80::1
                match:
                   macaddress: your-original-macaddress
                nameservers:
                   addresses:
                   - 2a01:4ff:ff00::add:2
                   - 2a01:4ff:ff00::add:1    <--- Replace this one
                set-name: eth0

    Save the file, then exit and reboot

    CTRL +x then y then ENTER

    reboot

    Nameserver Setup

    At this point, we are (almost) ready to install.

    Go to your domain registrar and look for Glue Records

    Glue Records

    This is what it looks like to set Glue Records at Porkbun

    Editing Glue Records at Porkbun

    Setting the server will look something like this:

    After adding both ns1.box and ns2.box it will look something like the following:

    So we have our Glue Records all set and they are named ns1.box and ns2.box and they point to your VPS IP.

    Stick with me, this can be confusing.

    At this point, we are ready to start the installation.

    Installation

    Installing Mail-in-a-Box is pretty easy at this point and it’s quite amazing how it sets everything up for you or gives you direct instructions on what to fix and how to fix it.

    Log back into our VPS

    ssh [email protected]  

    To start the installation use the following oneliner

    curl -s https://mailinabox.email/setup.sh | sudo -E bash

    Let the script run. It will ask you some basic questions but everything is self-explanatory.

    The script will finish and at this point, you can log in to your server at https://box.yourdomain.com/admin

    Once logged in click Status Checks

    This dashboard will show you whether your domain has resolved and propagated. Be patient with this sometimes it can take hours. Try refreshing your cache by clicking ctrl+shift + refresh.

    Issuing Certificates

    When a name resolves to the machine it will try to auto-issue a certificate for the hostname and nameservers. Once all names have resolves you will be given the option to add all certificates for all the required domains. It’s super simple and just works.

    The DNS Server

    While it may not be fast, your server is a full-fledged and fully certified email server but also a DNS server and you can set DNS addresses from the panel.

    To add a domain to the server first add an email to be associated with the domain then point the domain’s DNS at your ns1.box and ns2.box nameserver addresses.

    Example

    • Add email to box from GUI (avoid, admin, postmaster etc)
    • Point your domain at your ns1.box.yourdomain.com and ns2.box.yourdomain.com.
    • Add a subdomain or a main domain record from the DNS panel

    Complete!

    And there we have it. Your server will automatically generate all the records needed and you can always check the status panel it will give you all the information you need to know as far as DKIM, DMARC & SPF records.

    If you need more information head over to Mail-in-a-Box.

    Thanks for reading.

    Troubleshooting

    If you add an email and change the nameservers of the domain to point to your email box when you go to the status page if things have not updated in a reasonable time you can use the following command to restart the nameserver. Check the status page afterwards and you should have your domains resolve as usual.

    service nsd restart
    Share This Guide!