Build Your Own Mail Server with iRedMail on Ubuntu 22.04: No More Workspace Fees

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

Building Your Own Mail Server: Nightmare or Freedom?

After six months of managing a mail system for a 50-person team, I realized that running your own Mail Server isn’t as “daunting” as people say. Previously, I spent over $300 a month on Google Workspace. As the team grew, this cost became a financial burden. I tested Ubuntu Server 22.04 on over 20 different VPS instances to find the most stable setup.

Here is the iRedMail installation process to ensure your outgoing emails land in the Inbox instead of the spam folders of Gmail or Outlook.

Three Paths to Your Own Mail Server

In reality, you have three popular choices when you want to take control of your email system:

  • Manual Configuration (Postfix + Dovecot): This is for those who want to understand the inner workings of Linux architecture. However, it takes at least 2-3 days to fine-tune. Just one wrong configuration line, and your server will be immediately blacklisted as a spam source.
  • Mail-in-a-Box: An extremely fast, nearly 100% automated solution. The weakness is its rigidity, forcing users to use its own DNS, making it difficult to run alongside other websites on the same VPS.
  • iRedMail (The Optimal Choice): The perfect middle ground. The automated installation script saves time while maintaining a standard configuration file structure. You can still perform deep customization after installation.

Why Ubuntu 22.04 and iRedMail are a Perfect Match?

The power of iRedMail lies in its integration. With just one command, the system equips itself with:

  • Postfix & Dovecot: Handles sending and receiving mail.
  • Roundcube: A modern, smooth Webmail interface.
  • Amavisd & SpamAssassin: An effective “shield” to block spam.
  • iRedAdmin: Manage users directly in the browser without typing SQL commands.

Quick note: iRedMail is quite resource-intensive. You should choose a VPS with at least 2GB of RAM so that the ClamAV virus filter doesn’t freeze the system.

Step 1: DNS Setup – The Key to Avoiding Spam

Don’t rush into software installation before finishing the DNS configuration. A Mail Server lives or dies based on this step.

1.1. Set a Standard FQDN Hostname

The hostname must be a Fully Qualified Domain Name (FQDN), for example: mail.itfromzero.com. Use the following command to set it:

hostnamectl set-hostname mail.itfromzero.com

Open the /etc/hosts file and map your IP:

127.0.0.1   mail.itfromzero.com mail localhost

1.2. Configure Mandatory DNS Records

Access your domain management page and add these three critical records:

  • A Record: Point mail.domain.com to the VPS IP.
  • MX Record: Point domain.com to mail.domain.com (Priority: 10).
  • SPF Record (TXT): v=spf1 mx ip4:YOUR_IP -all.

Pro tip: Providers like DigitalOcean or Vultr often block Port 25 by default. You should submit a ticket asking them to open this port; otherwise, you won’t be able to send outgoing mail.

Step 2: Install iRedMail in 15 Minutes

Updating the system is the first thing to do to avoid software conflicts:

sudo apt update && sudo apt upgrade -y

Download the latest iRedMail version from GitHub. Currently, it is version 1.6.8:

wget https://github.com/iredmail/iRedMail/archive/refs/tags/1.6.8.tar.gz
tar -xf 1.6.8.tar.gz
cd iRedMail-1.6.8
bash iRedMail.sh

The installation screen will appear. Just take note of the following options:

  1. Storage: Keep the default /var/vmail.
  2. Web server: Choose Nginx for optimal speed.
  3. Backend: MariaDB is the most stable choice.
  4. Domain: Enter your main domain (e.g., itfromzero.com).
  5. Components: Select Roundcube and Netdata to monitor server health.

At the end of the process, the system will ask if you want to enable the firewall. Choose Y to secure the server.

Step 3: Boost Credibility with DKIM and DMARC

If you skip this step, emails sent to Gmail will almost certainly end up in spam. DKIM helps authenticate the sender using a digital signature.

Retrieve the DKIM Key from the System

Use the following command to display the public key:

amavisd-new showkeys

Copy the resulting TXT code and create a DNS record named dkim._domainkey.

Set Up DMARC Policy

Add a TXT record named _dmarc with the value:

v=DMARC1; p=quarantine; adkim=s; aspf=s

This line means: “If the mail fails SPF/DKIM checks, send it to Spam instead of rejecting it outright.”

Step 4: Install Let’s Encrypt SSL (Remove Security Warnings)

By default, iRedMail uses self-signed certificates, which cause browsers to show a red error. You should use Certbot to get a free SSL certificate.

Once you have the certificate, change the paths in the Nginx, Postfix, and Dovecot configuration files. You need to point them to the /etc/letsencrypt/live/mail.domain.com/ directory instead of the default iRedMail paths.

Finally, restart the services to apply the changes:

systemctl restart nginx postfix dovecot

Real-world Operating Experience

Never leave your Mail Server unattended after installation. Here are three lessons I’ve learned:

  1. Monitor Storage: System logs and the Roundcube database can take up dozens of GBs after a few months. Use logrotate for periodic cleanup.
  2. Backup Strategy: Always dump the MariaDB database and compress the /var/vmail directory every night. I usually push these backups to S3 for safety.
  3. Blacklist Monitoring: Use MXToolbox weekly. If your IP is blacklisted, check if any account has a compromised password and is sending out spam.

Owning your own Mail Server not only saves costs but also gives you absolute control over your data. I hope this guide helps you successfully deploy your system on Ubuntu 22.04.

Share: