List of content you will read in this article:
There are plenty of e-mail services out there, however, how trustworthy are they? For normal day-to-day e-mail use, typical email services are just secure enough. Nevertheless, if you are dealing with sensitive data, it is best to have your own mail server as in this case, you get full control over where your emails go through. In today’s article, we will discuss about the SMTP (i.e. Simple Mail Transfer Protocol) protocol and show you a step-by-step tutorial on how to run your own mail server on Linux.
What is an SMTP Server?
SMTP is a part of the application layer of the TCP/IP protocol. With the use of a process named “store and forward”, it transfers emails across networks. An SMTP server is an application that’s primary purpose is to send, receive, and/or relay outgoing mail between email senders and receivers.
Without an SMTP server, your email wouldn’t make it to its destination. Once you hit “send,” your email transforms into a string of code that is then sent to the SMTP server. The SMTP server is able to process that code and pass on the message. If the SMTP server wasn’t there to process the message, it would be lost in translation.
How to Run a Mail Server on Linux
Please note that this tutorial is made for Ubuntu/Debian.
In this tutorial, we will use the Postfix mail server and the Roundcube webmail application.
- First, set a valid FQDN (i.e. Fully Qualified Domain Name) for your server with the following command. Do not forget to replace “monovm.com” with your domain name.
sudo hostnamectl set-hostname mail.monovm.com
- Now, add a MX and A records for your domain in your DNS control panel.
MX record @ mail.monovm.com
mail.monovm.com
- Once that is done, you will need to install Apache2, MariaDB and PHP :
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt install apache2 apache2-utils mariadb-server mariadb-client php7.4 libapache2-mod-php7.4 php7.4-mysql php-net-ldap2 php-net-ldap3 php-imagick php7.4-common php7.4-gd php7.4-imap php7.4-json php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-bz2 php7.4-intl php7.4-gmp php-net-smtp php-mail-mime php-net-idna2 mailutils
- Now you should install Postfix using the following command
sudo apt-get install postfix
- Once that is installed, you need to install an MDA (i.e. Mail Delivery Agent), we chose Dovecot
sudo apt-get install dovecot-imapd dovecot-pop3d
- Next, restart the Dovecot service with the following command:
sudo systemctl restart dovecot
- Now you need to install a webmail service, we picked Roundcube
wget https://github.com/roundcube/roundcubemail/releases/download/1.4.8/roundcubemail-1.4.8.tar.gz
tar -xvf roundcubemail-1.4.8.tar.gz
sudo mv roundcubemail-1.4.8 /var/www/html/roundcubemail
sudo chown -R www-data:www-data /var/www/html/roundcubemail/
sudo chmod 755 -R /var/www/html/roundcubemail/
- Once the above steps are completed, you need to create a new database and a user for Roundcube as well as grant all the permissions. Note: do not include “MariaDB [(none)]>” in the commands you are writing.
mysql -u root
MariaDB [(none)]> CREATE DATABASE roundcube DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
MariaDB [(none)]> CREATE USER roundcubeuser@localhost IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON roundcube.* TO roundcubeuser@localhost;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit;
- Now, import the initial tables to the Roundcube database
sudo mysql roundcube < /var/www/html/roundcubemail/SQL/mysql.initial.sql
- Create an apache virtual host for Roundcube webmail.
sudo nano /etc/apache2/sites-available/roundcube.conf
Then, add the following configuration to it:
<VirtualHost *:80>
ServerName monovm.com
DocumentRoot /var/www/html/roundcubemail/
ErrorLog ${APACHE_LOG_DIR}/roundcube_error.log
CustomLog ${APACHE_LOG_DIR}/roundcube_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html/roundcubemail/>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
- Next, enable this virtual host and reload apache with the following commands:
sudo a2ensite roundcube.conf
sudo systemctl reload apache2
- The webmail is now accessible through http://yourdomain.com/roundcubemail/installer/. It should look something like this:
Now, go to database settings and add the database details.
- Once the changes are made, create a config.inc.php file.
- Now the installation is finished. Delete the installer folder and make sure that the enable_installer option in inc.php is disabled.
sudo rm /var/www/html/roundcubemail/installer/ -r
- To add users, enter the following commands:
sudo useradd username
Where “username” is replaced with the desired name.
sudo passwd username
You should now be able to use your Linux email server with the accounts you created. Did it work for you? Leave a comment below.
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.