Set up your email server with Mailcow

0 Shares
0
0
0
0

Introduction

In this tutorial you will set up your own mail server running on an Ubuntu server in Hetzner Cloud. With Mailcow you can host your own mail server with your own custom domain. Mailcow also offers a way to sync your contacts and calendar.

Prerequisites
  • You need a domain name.
  • A little knowledge about how Docker works
  • Hetzner Cloud servers block ports 25 and 465 by default to protect against spam. You can request to have the ports unblocked after you pay your first invoice. This essentially acts as a simple verification that you are not a spammer.

Step 1 – Create a new Cloud Server

  • Log in to the Cloud Console
  • Create a new project and name it whatever you want.
  • Choose a server location and type depending on your needs. If you are not sure, see the tips below.
  • Click Add Server and select your server image. This tutorial is based on Ubuntu 22.04, but it can easily be adapted to Debian 11, which is more stable and lightweight.
  • Select the resources you need from Type.
  • Click on the Cloud config field and enter this: #include https://get.docker.com (this will install docker)
  • Choose your SSH key
  • Type your server hostname in the Name input (mail.example.com)
  • Click Create & Buy Now

How to choose the right server location and type

Choose a server location that is geographically closest to you or your user base. As for the right server type, keep in mind that Mailcow requires a minimum of 7GB of RAM to function properly. Mailcow is a full-featured groupware solution. With that said, the lowest server type available would be the CX31* or CPX31.

Step 2 – Setting up DNS

Basically, you want to create a new DNS record called mail.example.com and add your IPv4 (A record) and IPv6 (AAAA record) server to it. Then, you can set up your domains MX record to point to your newly created subdomain mail.example.com. You should also set up a subdomain autodiscover.example.com and autoconfig.example.com. Both should be CNAMEs and point to mail.example.com.

Your DNS configuration should look like this:

# Name Type Value
mail IN A 10.0.0.1
mail IN AAAA 2001:db8:1234::1
autodiscover IN CNAME mail
autoconfig IN CNAME mail
@ IN MX 10 mail

For a more advanced setup, there are more DNS records to watch out for in the Mailcow documentation.

Step 3 – Install updates and Docker Compose on the server

Wait a few seconds for the server to start up and then connect to it using SSH and your private key. You should now be able to log in by running:

apt update && apt upgrade -y

This is a good time to reboot the server after all upgrades are complete, especially if there were any kernel updates.

Step 4 – Clone the Mailcow repository

Now we want to clone the Mailcow GitHub repository, so first you need to log in to /opt with:

cd /opt

Once you are in /opt, you can run:

git clone https://github.com/mailcow/mailcow-dockerized

Step 5 – Create the configuration, pull the Docker containers, and launch Mailcow

To create the configuration, change your working directory to /opt/mailcow-dockerized with cd /opt/mailcow-dockerized and run ./generate_config.sh to generate the configuration. Now you need to enter your domain name (something like mail.example.com). We are almost done. Now you need to run Docker compose pull to pull the Docker images. To start Mailcow, run docker compose up -d .

Step 6 – Set up reverse DNS entries

To set up reverse DNS entries, follow these steps:

  • Go to the Hetzner Cloud console and click on your project.
  • Select the server you created and go to the NETWORKING tab.
  • Click the three dots near your IPv4 address, select Edit Reverse DNS, enter your domain (mail.example.com), and confirm the change.
  • Click the three dots near your IPv6 subnet, select Edit Reverse DNS, and type ::1 in the field where the IP is under your domain name (mail.example.com).

Step 7 – First, log in to your Mailcow instance

Visit your Mailcow instance at https://mail.example.org and log in with the default credentials:

  • Username: admin
  • Password: moohoo

Important: Change your password as soon as possible, as leaving it as it is will open your server to intrusion.

Step 8 – Add domain(s) to Mailcow

Now you can add your domain to Mailcow. Simply go to Configuration -> Mail setup. Add your domain under the “Domain” tab.

Step 9 – Set up DKIM

Now, when you go back to Configuration -> Configuration & Details, you can set up DKIM. In the Configuration tab, select DKIM in the sidebar. It should be at the top. Scroll down to see a mask where you can enter your domain. Just click the Select domains with missing keys shortcut to populate it with your domain name. Now select a 2048 key and click Add. Once the key is added, you can copy the public key and create a DNS TXT entry called dkim._domainkey with the content you just copied.

Step 10 – Create a mailbox

When to Configuration -> Mail setup Once you're back, you can create a mailbox and log in to SOGo by accessing it at https://mail.example.com/SOGo.

Step 11 – Create a backup

Backups are essential for most server setups. Your email server is no different. Mailcow offers a very simple way to create backups of your email data.

Create a manual backup
  • Connect to your server using the SSH key.
  • Go to the Mailcow directory that contains the helper script /opt/mailcow-dockerized/helper-scripts.
  • Run the backup script./backup_and_restore.sh backup all --delete-days 7

Depending on the size of the emails and the type of server, this process can take anywhere from a few minutes to a few hours.

Step 12 – Update the Mailcow instance

Mailcow updates are usually released by the authors once a month. More updates are released if there are security patches or urgent bugs. It is wise to keep an eye on the Mailcow releases page for information on changes.

Before performing the update, it is recommended to check if you have a current backup of your data. In the Hetzner cloud console, you can also create a snapshot of your server to quickly get back to working order in case of an issue.

If you don't want to use Hetzner snapshots, Mailcow also has a built-in way to roll back updates.

  • Connect to your server using the SSH key.
  • Go to the Mailcow folder /opt/mailcow-dockerized
  • .Run ./update.sh
  • Finally, Mailcow will ask you if you want to remove unused components. Choose No and manually remove them later so you're prepared if something goes wrong.
  • After performing the update, Mailcow will start all services and perform internal upgrades. To avoid data corruption, do not shut down your server or docker containers.
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like