Deploying Mail Server on VPS

Tejendra Saradhi
5 min readSep 4, 2022

We’ll be looking at deploying iRedMail on our VPS (Digital Ocean) for 5$/Month only.

Pre Requisite

First, we need a domain name which can be brought from various domain registrars such as GoDaddy, Namecheap and so on. And then you need an account on VPS (Virtual Private Server) providers such as Microsoft Azure, Digital Ocean, and Amazon AWS. You can use my referral link for Digital Ocean and get 100$ for 60 Days for free. Note initially to activate the account you might have to buy a VPS of a minimum of 5 USD (You’ll pay that once).

Once you have all these ready follow the blog step by step. To have a seamless setup without having to do any debugging and error fixes.

Server Setup

Get a droplet for your convenience (I’m using Ubuntu 22), if you are setting it up for a small team/individual then 5$ VPS is fine. Otherwise according to your team size increase the specs of the server. I’ll be using Digital Ocean and Netlify DNS, you can follow along. Most of the steps are almost the same., maybe the location of those settings may be in different places in each Domain Registrar’s DNS management system. Name your VPS in the Digital Ocean with a Fully Qualified Domain Name (FQDN). For example, if I’m going to set up my mail in the subdomain of example.com. That is mail.example.com, then name the droplet as mail.example.com

Now by doing this your Digital Ocean will create a PTR (DNS Pointer) record, by associating an IP address with a Domain name. We are used to setting up A record to associate hostname to IPv4 address. When we ping the hostname it resolves to the IPv4 address. Whilst similarly when you query with dig or nslookup the IP address it resolves to a hostname. Gmail and Outlook mail needs us to have the configuration such that when the server queries our IPv4 address it should resolve to mail.example.com and mail.example.com should resolve to the IPv4 address.

Note: If this step is wrong, then you won’t be able to send emails to Google Mail and Outlook Mail. Use DNS CHECKER website to check the configuration for both the domain name and IPv4 address.

After following the above steps and checking it, now SSH into the VPS droplet. Now we edit the hostname and hosts file to match the requirements of the iRedMail setup script.

root@mail.example.com:~# cat /etc/hosts
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.debian.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
# /etc/cloud/cloud.cfg or cloud-config from user-data
#
127.0.1.1 mail.example.com mail.example.com
127.0.0.1 mail.example.com mail localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@mail.example.com:~# nano /etc/hostname # This command is used to edit the file named as hostname, edit it as mail.root@mail.example.com:~# hostname -b -F /etc/hostname # To update the hostnameroot@mail.example.com:~# hostname -f # It's output should be something as mail.example.comroot@mail.example.com:~# reboot

Wait for the server to reboot. After that your prompt should look like root@mail:~# and not root@mail.example.com:~# .

iRedMail Installation

Download the iRedMail compressed installation file. tar -xvf iRedMail-1.6.1.tar.gz . Move inside the uncompressed directory and change permission of the script chmod +x ./iRedMail.sh and then run it ./iRedMail.sh You’ll get see some files installing and then later you’ll get a CLI based setup wizard.

First, it will show you a welcome message hit enter to continue. Next, you’ll be asked for the installation path, hit enter or change the path according to your preference (If you know what you are doing). Third, we will be asked if we need a server. Hit enter if you are gonna use a web client else you can change the option and hit enter. Once it’s done you’ll be asked for the DB to be used, I chose MariaDB, hit enter and fill the password following the password policy. Next, it will ask for a domain name. You’ll have to fill it as example.com and not mail.example.com . Now hit enter and fill in the password for the email postmaster@example.com. Later it will ask you to choose the installations

I chose everything (This screenshot is for older version). And installation option might slightly vary. Now hit enter and wait for it to install.

Note: DO NOT HIT ENTER SEVERAL TIMES, you’ll be asked some question in between. If you enter those, it will take default values and installation might fail. I’d ask you to have some patience.

Setting Up DNS and Web Mail Client

We will go to our DNS management page and make some record entries. We now should have a A record pointing to our mail.example.com (if not add it). Now create a MX record by pointing it to mail.example.com and give priority as 10 (This value can be altered if we have multiple mail servers). Add TXT records for SPF, DKIM and DMARC. To customize you can read their instructions. What I did for SPF record is, I created TXT record as
v=spf1 include:iamtejas.tech ~all (Don’t forget to swap example.com to your domain name everywhere mentioned in the blog).

Now we have SPF, MX, A and PTR record set. For DMARC record I copied from the iRedMail setup DNS blog
v=DMARC1; p=reject; sp=none; adkim=s; aspf=s; rua=mailto:dmarc@example.tech; ruf=mailto:dmarc@example.tech

Finally when the installation is over. Reboot the droplet and then once its booted up run the command to extract key for DMARC. amavisd-new showkeys . That will give you the DMARC key in new lines with quotes. Remove those quotes and make it into a one line string. You can follow the same instruction in iRedMail’s setup DNS record article. After that you’ll have to wait for some time, to have those records propagate. Once this is done you can log in into https://mail.example.com/mail/ . That is where you have your web email client. You can login as postmaster@example.com and then you can see some mails from root@example.com save those content, which has credentials into some secure place and delete the mail. You can manage users and domain configuration in https://mail.example.com/iredadmin/ .

Setting up Mobile Client

You can use Gmail app or any other application with SMTP or POP3 protocol. Configuring it is easy. Just type in the email ID and password. And point the server to correct domain name. You might get error stating some cryptographic issue. You can ignore that. All mails will be synced in mobile and web client too.

Sign up to discover human stories that deepen your understanding of the world.

Tejendra Saradhi
Tejendra Saradhi

Written by Tejendra Saradhi

Music Producer | Cyber Security Enthusiast

Responses (1)

Write a response

Nice one!

--