How to Setup Mail Server Using Postfix, MariaDB, Dovecot and Roundcube

Hello everybody, today we are going to setup mail server using Postfix, Devcot and MariaDB on Ubuntu 16.04 LTS. In this article we will shows you how to setup Postfix (smtp server), Dovecot (imap/pop server) and MariaDB to store information on virtual domains and users. The task of the smtp server is to accept incoming mails and relay outgoing mails from authorized users on the system. Whereas Dovecot allows authorized users to access their Inbox and read whatever mails there are. So, our primary focus in the article is to setup a fast and secure mail server using virtual users.

Setup Prerequisites

There are some number of prerequisites, that should be followed before moving towards mail server setup. This includes domain forwarding to your server and setup a static IP address to your server. Open this file '/etc/hostname' to setup your appropriate hostname.

# vim /etc/hosts
172.25.10.171 ubuntu-16.linoxide.com ubuntu-16

In order to use your mail server on a wider network, you must correctly configure the DNS and MX records for your host's domain. Then make sure that the iptables firewall or any other external firewall is not blocking any of the standard mail ports on your server that is (25, 465, 587, 110, 995, 143, and 993).

After domain and hostname setup, run below command to update your system with latest updates before installing its other required packages.

# apt-get update

Installing Packages

Now install all required packages including 'Postfix', 'Devcot' and 'MySQL' with some other necessary packages to setup our mail server by flowing the command below using root user credentials.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Then add email addresses to the virtual_users table by replace the email address values with the addresses that you wish to configure on the mailserver and update the password values with strong passwords you have.

Setup Postfix Configurations

After MySQL database setup, now we will setup Postfix so the server can accept incoming messages for the domains. Create a copy of the default Postfix configuration file in case you need to revert to the default configuration.

# cp /etc/postfix/main.cf /etc/postfix/main.cf.org

Then open the file using vi or vim editor to match the following configuration but do not forget to update your domain and hostname.

# vi /etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

This will return the email address to which the alias forwards as shown in the image below.

Next we will configure the '/etc/postfix/master.cf' file. Before making changes in it save it copy first.

# cp /etc/postfix/master.cf /etc/postfix/master.cf.org

Then open the configuration file for editing and uncomment the two lines starting with 'submission' and 'smtps' and the block of lines starting with '-o' after each which should be like the below.

# vim /etc/postfix/master.cf

Save the configuration and then run command below to restart postfix services, and you have done your postfix configurations successfully.

# service postfix restart

Dovecot Configuration Setup

Dovecot allows users to log in and check their email using POP3 and IMAP. Here we will configure Dovecot to force users to use SSL when they connect so that their passwords are never sent to the server in plain text. Let's start by copying all original configurations before making any changes.

After saving this, open the '/etc/dovecot/conf.d/10-mail.conf' file to modify the following variables within the configuration file which controls how Dovecot interacts with the server’s file system to store and retrieve messages.

service auth {
# auth_socket_path points to this userdb socket by default. It's typically
# used by dovecot-lda, doveadm, possibly imap process, etc. Its default
# permissions make it readable only by root, but you may need to relax these
# permissions. Users that have access to this socket are able to get a list
# of all usernames and get results of everyone's userdb lookups.
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}

unix_listener auth-userdb {
mode = 0600
user = vmail
#group =
}

# Postfix smtp-auth
user = dovecot
}

Uncomment the user line and set it to vmail under service auth-section.

service auth-worker {
# Auth worker process is run as root by default, so that it can access
# /etc/shadow. If this isn't necessary, the user should be changed to
# $default_internal_user.
#user = root
user = vmail
}

That's it , now save the configuration file and run below command to restart Dovecot service.

# service dovecot restart

Now setup any test account in an email client to ensure that everything is working. Provide the full email address, including your domain name and the password that you added to the MariaDB table for you email address. Then try sending an email to this account from an outside email account and then reply to it. After that check the mail log file in /var/log/mail.log for an incoming message, and the second block for an outgoing message.

Setup Roundcube for Webmail Interface

You can use email client which supports 'smtp' and 'pop/imap', but webmail part is completely optional.

Run the below command to install Roundcube along with its plugins on your Ubuntu 16.04 server and press 'Y' key to start installation process.

# apt-get install roundcube roundcube-plugins roundcube-plugins-extra

During the installation, you will be asked to for a database installed and configured before it can be used. This is optional and we will be selecting 'No' here as we have already setup the database.

After installation, open its configuration file using any editor and put the following parameters in it as shown.

Save and close the file after making your changes. Then create a virtual host in your web server and launch your favorite web browser to begin configuring Roundcube. The first step of Roundcube’s graphical configuration is an environment check. Click on the NEXT button at the bottom of the page to continue. You can see our detailed article on roundcube installation on Ubuntu 16.04.

Conclusion:

Congratulation, we have successfully setup mail server using Postfix, MariaDB, Dovecot and Roundcube on Ubuntu 16.04. If DNS records have not been created for the mail server yet then do now. Once the DNS records have propagated, email will be delivered via the new mail server. Now you can add new domains, email addresses, and aliases for the users by simply adding a new line to the appropriate MySQL table and enjoy communication over your newly build Ubuntu mail server.

About Kashif Siddique

Linux Systems and Security Engineer in Information and Communication Technology. Results-driven ICT Professional and Open Source Geek with technical specialties in the area of Open Source Operating systems and Applications.

Related Posts

In today's world, cloud usage has penetrated our lives so much that we need it for everything. To name a few, cloud based nodes, data storage, operating systems etc. Even if one decides to go for cloud computing, do they [...]

GitLab is a web-based Git repository manager similar to Github. It's an open source git repository management tool powered by Ruby and Rails. It provides a .deb package which contains GitLab Community Edition and all its dependencies including Ruby, PostgreSQL, [...]

ZenCart is a free and open source online store management system based on PHP and MySQL. It is user friendly, easy to manage e-Commerce site management tool which is freely available under GNU GPL license version 2. It is platform [...]

__________________________________________________________
This statement doesn't say which .conf file
"Disable unencrypted IMAP and POP3 by setting the protocols’ ports to 0, as shown below. Ensure that the entries for port and ssl below the IMAPS and pop3s entries are uncommented."