↓ Skip to main content
Self Hosting Part 2

Mail Server Setup

The first main service on the server: a mail server with iRedMail. This setup also installs NGINX and MariaDB.

Series map 3/23
Self Hosting
  1. 0 Planning and Architecture
  2. 1 Post-Install Configuration
  3. 2 Mail Server Setup
    1. 2.1 Setting Up SSL for the Mail Server
    2. 2.2 Adding a New Domain to the Mail Server
    3. 2.3 Using a Mail Client
    4. 2.4 Moving the Mail Server
    5. 2.5 Extra Settings
  4. 3 NGINX Configuration
  5. 4 Docker Setup
    1. 4.1 A Ready Template for Docker Containers
  6. 5 Firewalld Configuration
  7. 6 Fail2Ban Configuration
  8. 7 WireGuard Setup
    1. 7.1 Adding WireGuard Clients
    2. 7.2 Putting Services Behind the VPN
    3. 7.3 Possible Problems
  9. 8 Vault Setup
  10. 9 Vaultwarden Setup
  11. 10 WordPress Setup
    1. 10.1 Moving an Old WordPress Site into This Setup
  12. 11 Usememos Setup
  13. 12 Syncthing Setup
On this page
Requirements
  • 1 Post-Install Configuration must be done.
  • iRedMail must be installed on a freshly installed server; this part installs the first service on the server.

Assumptions

  • The name of the mail server (hostname) is set to mail.xenonly.com and the first mail domain is xenonly.com.
  • A and AAAA records exist for xenonly.com and mail.xenonly.com. The other records mail needs (MX, SPF, DKIM, DMARC, PTR) are added in this part.
  • Commands are run on the server with root rights (sudo). The DNS checks with dig are done from a personal computer.

For mail we’ll mainly use Postfix and Dovecot. For the installation and the first configuration we’ll use the iRedMail install script.

Important

iRedMail must be installed on a freshly installed server. That’s why iRedMail is the first thing we install after setting up the server. We’ll add the other services later and adjust the configuration of the services that clash with iRedMail to fit our needs.

Preparation
#

Changing the Hostname
#

I’ll use mail.xenonly.com as the name of the mail server. The other domains added later will also send mail using this server name. Let’s change the hostname with the command below:

bash
sudo hostnamectl set-hostname mail.xenonly.com

During the installation, iRedMail expects the hostname to resolve as a fully qualified domain name (FQDN). Let’s check it:

bash
hostname -f   # mail.xenonly.com

If the output is not mail.xenonly.com, let’s add the server name to the start of the 127.0.0.1 line in /etc/hosts:

/etc/hosts
127.0.0.1   mail.xenonly.com mail localhost localhost.localdomain

Turning Off SELinux
#

iRedMail says it doesn’t work with SELinux and recommends turning it off. This is the recommended way to turn it off on Rocky Linux 10:

bash
sudo grubby --update-kernel ALL --args selinux=0

After turning SELinux off with this command, let’s reboot the system:

bash
sudo reboot

Downloading and Installing iRedMail
#

We get the download link of the latest release from https://www.iredmail.org/download.html. At the time of writing (November 2026), the latest release is 1.8.8.

bash
cd /opt
sudo wget https://github.com/iredmail/iRedMail/archive/refs/tags/1.8.8.tar.gz
sudo tar zxf 1.8.8.tar.gz
cd iRedMail-1.8.8
sudo bash iRedMail.sh
Note

cd is a command of the shell itself, so it is run directly, not with sudo. The commands that write files run with sudo, so they can write under /opt.

With these commands we download and run the iRedMail installer. After the needed packages are installed, the setup wizard opens:

  • We continue with Yes and leave the mail storage directory at the default, /var/vmail.
  • Web server → Nginx
  • Database for the mail accounts → MariaDB
  • We set a password for the MariaDB root user. This password is written to /root/.my.cnf and used by root’s database commands and by the backup script; it should be random and different from the postmaster password.
  • First mail domain → xenonly.com. The important point here: this value must not be the server’s hostname (mail.xenonly.com).
  • We set a password for the address postmaster@xenonly.com. This address and password will be used as the admin account.
Warning

Strong random passwords are recommended for both; the wizard shows a random sample password on both screens. After the installation, the passwords entered during the setup are written in plain text to a few files on the server. Where they are kept and how to clean them up is in 2.5 Extra Settings.

Next we choose the optional components:

These are already selected by default. I also remove the system monitoring service Netdata.

When the wizard is done, it shows a summary like this:

********************** Review your settings *****************************
*************************************************************************

* Storage base directory:               /var/vmail
* Mailboxes:
* Daily backup of SQL/LDAP databases:
* Store mail accounts in:               MariaDB
* Web server:                           Nginx
* First mail domain name:               xenonly.com
* Mail domain admin:                    postmaster@xenonly.com
* Additional components:                Roundcubemail iRedAdmin Fail2ban

We continue with y and the installation starts. During the installation there will be some questions about the configuration.

< Question > Would you like to use firewall rules provided by iRedMail?
< Question > File: /etc/firewalld/zones/iredmail.xml, with SSHD ports: 22. [Y|n]

I answer this question with n. I’ll do the firewall settings myself in 5 Firewalld Configuration.

< Question > Would you like to use MySQL configuration file shipped within iRedMail now?
< Question > File: /etc/my.cnf. [Y|n]

We confirm this question with Y.

The installation finishes with an output like this:

********************************************************************
* URLs of installed web applications:
*
* - Roundcube webmail: https://mail.xenonly.com/mail/
*
* - Web admin panel (iRedAdmin): https://mail.xenonly.com/iredadmin/
*
* You can login to above links with below credential:
*
* - Username: postmaster@xenonly.com
* - Password: ***
*
********************************************************************
* Congratulations, mail server setup completed successfully. Please
* read below file for more information:
*
*   - /opt/iRedMail-1.8.8/iRedMail.tips
*
* And it's sent to your mail account postmaster@xenonly.com.
*
********************* WARNING **************************************
*
* Please reboot your system to enable all mail services.
*
********************************************************************

Then we reboot the system:

bash
sudo reboot

After the Installation
#

DNS Settings
#

To add the DNS records, we go to the panel of the provider that manages the domain’s DNS.

At this point we’ll add the DNS records xenonly.com needs for the mail server. The records will look roughly like the ones below.
We’ll get the <DKIM_KEY> value in the DKIM Record section below.

NameTypeContentWhat It Means
xenonly.comA<SERVER_IPV4>IPv4 record for the domain
xenonly.comAAAA<SERVER_IPV6>IPv6 record for the domain
mail.xenonly.comA<SERVER_IPV4>IPv4 record for the mail server
mail.xenonly.comAAAA<SERVER_IPV6>IPv6 record for the mail server
xenonly.comMXmail.xenonly.comSays which mail server incoming mail should go to
dkim._domainkeyTXT<DKIM_KEY>Lets receivers verify the mail signed by Amavis
_dmarcTXT"v=DMARC1; p=quarantine; sp=quarantine; np=reject; adkim=s; aspf=s; rua=mailto:dmarc@xenonly.com"Says what to do with mail that fails the SPF or DKIM check
xenonly.comTXT"v=spf1 mx a ip4:<SERVER_IPV4> ip6:<SERVER_IPV6> ~all"Says which servers may send mail for this domain
mail.xenonly.comTXT"v=spf1 a -all"SPF for the server’s HELO name; bounces with an empty sender address are checked against this name
Note

The dkim._domainkey and _dmarc values in the Name column are relative to the domain; the full names are dkim._domainkey.xenonly.com and _dmarc.xenonly.com.

DMARC Record
#

Example record:

v=DMARC1; p=quarantine; sp=quarantine; np=reject; adkim=s; aspf=s; rua=mailto:dmarc@xenonly.com
ParameterMeaning
v=DMARC1Version. It must always come first.
p=quarantineMail that fails the check goes to the spam folder. The other options: none (only a report is sent, the mail is not affected) and reject (the mail is rejected completely).
sp=quarantineThe same rule for subdomains (for example x.xenonly.com). If it is not written, the p value applies. If no mail is sent from any subdomain, reject can be used too.
np=rejectMail sent in the name of subdomains that don’t exist in DNS at all (for example random.xenonly.com) is rejected. No real mail can come from these names, so the strictest setting is safe here. It is a fairly new parameter, so some servers may not know it yet, but adding it does no harm.
adkim=sStrict matching for DKIM: the domain in the signature must be exactly the same as the domain in the sender address. With r (relaxed), subdomains are accepted too.
aspf=sStrict matching for SPF. Same idea as above.
rua=mailto:…The address the daily summary reports are sent to.
Note

We need a mailbox for dmarc@xenonly.com, the address the DMARC reports are sent to. We’ll create it below, in the Account for DMARC Reports section.

PTR Record
#

The PTR (reverse DNS) record shows which name an IP address belongs to, and it is set in the VPS provider’s control panel. In the rDNS/PTR section of the panel, we enter mail.xenonly.com for the server’s IPv4 address and, if there is one, mail.xenonly.com again for the IPv6 address.

Warning

The PTR value must be the server’s hostname, mail.xenonly.com, not xenonly.com.

Warning

If the server has IPv6, mail will most likely be sent over IPv6. If the PTR for IPv6 doesn’t match, providers like Gmail reject the mail right away.

We can check the records from our personal computer with the commands below:

bash
dig +short A mail.xenonly.com      # <SERVER_IPV4>
dig +short AAAA mail.xenonly.com   # <SERVER_IPV6>
dig +short -x <SERVER_IPV4>        # mail.xenonly.com.
dig +short -x <SERVER_IPV6>        # mail.xenonly.com.

DKIM Record
#

What it is: A digital signature standard that proves the mail really comes from the authorized domain and was not changed on the way.

How It Works
#

  • There is a private key for each domain in the /var/lib/dkim/ directory.
  • The domain and its key are defined for the Amavis service in /etc/amavisd/amavisd.conf.
  • The public key is in the dkim._domainkey DNS record.
Note

We gave xenonly.com as the first domain during the installation, so its keys were created automatically.

To see the existing keys:

bash
sudo amavisd showkeys

To get a single-line output that can go straight into the DNS record:

bash
sudo amavisd showkeys xenonly.com | awk -F'"' '/^[[:space:]]*"/ {printf "%s", $2} END {print ""}'
Note

The value added to DNS must be a single line. amavisd showkeys splits the key over several lines; the command above joins them.

We add this output to DNS like this:

NameTypeContentWhat It Means
dkim._domainkeyTXT<DKIM_KEY>Lets receivers verify the mail signed by Amavis

At this point our server can send and receive mail for xenonly.com.

Creating Users in iRedAdmin
#

Note

In the NGINX configuration that comes with iRedMail, iRedAdmin is at the /iredadmin path and Roundcube is at /mail. In 3 NGINX Configuration these addresses are moved to subdomains (iredadmin.xenonly.com and mail.xenonly.com).

Note

The server is still using the self-signed certificate iRedMail created, so the browser will show a certificate warning. We’ll get a Let’s Encrypt certificate in 2.1 Setting Up SSL for the Mail Server.

We go to https://xenonly.com/iredadmin and log in with the admin account (postmaster@xenonly.com).

Note

The xenonly.com domain was created as the first domain during the installation, so we don’t need to add it.

Following Add → User, we create a user named info for xenonly.com.

Tip

You can create as many users as you want for the existing domains. To add a new domain, see 2.2 Adding a New Domain to the Mail Server.

Account for DMARC Reports
#

So that the reports can reach rua=mailto:dmarc@xenonly.com from the DMARC record, we again follow Add → User and create a user named dmarc for xenonly.com.

Note

For every domain, there must be a mailbox for the report address given in that domain’s DMARC record.

Test and Check
#

Let’s go to Roundcube at https://xenonly.com/mail and log in as info@xenonly.com.

Let’s send a test mail to the address shown on https://www.mail-tester.com/. The expected result:

Your lovely total: 10/10