Learn how to set up a email server on a Cloud Server running CentOS 7. This tutorial features Postfix as an SMTP server, Dovecot for POP/IMAP func­tion­al­i­ty, and RoundCube as a webmail program for users to check and receive email from a web browser.

The tutorial will also walk you through the process of creating and using a self-signed SSL cer­tifi­cate for use in securing incoming and outgoing email con­nec­tions.

Tip

Set up your own pro­fes­sion­al email server in the blink of an eye. You benefit from large and ex­pand­able storage space, your own domain, automatic en­cryp­tion as well as calendar and office functions or email archiving. IONOS also offers the highest security standards in ISO-certified data centers.

Re­quire­ments

  • A Cloud Server running CentOS 7.
  • Postfix installed and running.

To check the status of Postfix on the server, use the command:

sudo postfix status
bash

If Postfix is installed and running, you will see the output:

[user@localhost ~]# sudo postfix status
postfix/postfix-script: the Postfix mail system is running: PID: 1681
text

Firewall access

If you have a firewall, you will need to allow access to the following ports:

  • SMTP: 25
  • POP3: 110
  • IMAP: 143
  • SMTP Secure: 465
  • MSA: 587
  • IMAP Secure: 993
  • POP3 Secure: 995

Create an SSL cer­tifi­cate

We will create a self-signed SSL cer­tifi­cate to secure incoming and outgoing email con­nec­tions:

sudo openssl req -x509 -nodes -newkey rsa:2048 -keyout mailserver.key -out mailserver.crt -nodes -days 365
bash

Answer the questions at the prompts, or just hit [Enter] to leave an answer blank. This command will create two files: mailserv­er.key and mailserv­er.crt.

Create a folder for the SSL cer­tifi­cate files:

sudo mkdir /etc/postfix/ssl
bash

Then move the files into this folder:

sudo mv mailserver.key /etc/postfix/ssl
sudo mv mailserver.crt /etc/postfix/ssl
bash
Be secure. Buy an SSL cer­tifi­cate.
  • Secures data transfers
  • Avoids browser warnings
  • Improves your Google ranking

Configure Postfix

The following steps will enable a basic Postfix set-up, allowing you to send and receive mail. For more in­for­ma­tion on these con­fig­u­ra­tions, consult the official doc­u­men­ta­tion.

Before you begin the con­fig­u­ra­tion changes, be sure that you have the most recent version of Postfix by running the command:

sudo yum update postfix
bash

Edit master.cf

First we will edit the /etc/postfix/master.cf file to enable the sub­mis­sion daemon to send and accept mail securely:

sudo nano /etc/postfix/master.cf
bash

Delete the hash mark (#) to un-comment the following lines:

#submission inet n       -       n       -       -       smtpd
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
text

Save and exit the file.

Edit main.cf

The /etc/postfix/main.cf file contains the con­fig­u­ra­tion options for your Postfix in­stal­la­tion. Edit this file:

sudo nano /etc/postfix/main.cf
bash

Make the following changes:

Set Hostname

Edit the line which reads:

#myhostname = host.domain.tld
text

Delete the hash mark (#) to un-comment the line, and replace host.domain.tld with the hostname of the server:

myhostname = example.com
text

Set Domain

Edit the line which reads:

#mydomain = domain.tld
text

Delete the hash mark (#) to un-comment the line, and replace domain.tld with the domain you want to receive mail for:

mydomain = example.com
text

Enable Other Settings

Delete the hash mark (#) to un-comment the following lines, and comment out the already active line inet_interfaces = localhost with a hash mark (#):

#myorigin = $myhostname
#inet_interfaces = all
#inet_protocols = all
#mydestination = $myhostname, localhost.$mydomain, localhost
#home_mailbox = Maildir/
text

Add Con­fig­u­ra­tions

Finally, add the following lines to the end of the file:

smtpd_tls_key_file = /etc/postfix/ssl/mailserver.key
smtpd_tls_cert_file = /etc/postfix/ssl/mailserver.crt
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_security_level=may
text

Save and exit the file, then restart Postfix to load the new con­fig­u­ra­tions:

sudo systemctl restart postfix
bash

Test Postfix with Telnet

To test Postfix we will telnet to the server and perform a basic “handshake protocol,” just as an email program would.

First, install Telnet:

sudo yum install telnet
bash

Once Telnet is installed, use it to connect to the server’s SMTP port:

telnet localhost 25
bash

The server will respond with:

[user@localhost ~]$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 example.com ESMTP Postfix
text

Start by telling the server who the mail is being sent from:

mail from: some-person@some-other-server.com
text

Then tell the server who you are sending mail to, replacing user@example.com with your own username and domain name:

rcpt to: user@example.com
text

Now add a simple message. Tell the server your message body starts here:

data
text

Type the message, then follow it with [Enter], a period ., and [Enter]:

hello world 
.
text

Close the session by typing quit and hitting Enter.

If you are suc­cess­ful, your test message will appear in /home/username/Maildir/new. You can view this message with the command:

ll /home/username/Maildir/new
bash

To read the message, copy and paste the name of the file (it will be a long name like 1481229255.Vfd01I402b66aM546595.example.com) and read it with more:

more /home/username/Maildir/new/1481229255.Vfd01I402b66aM546595.example.com
bash

You will see the email message, along with all of the header in­for­ma­tion:

From some-person@some-other-server.com  Thu Dec  8 19:43:10 2016
Return-Path: <some-person@some-other-server.com>
X-Original-To: user@example.com
Delivered-To: user@example.com
Received: from localhost (localhost [127.0.0.1])
        by mail.example.com (Postfix) with SMTP id 6CFD589184
        for <user@example.com>; Thu,  8 Dec 2016 19:42:33 +0000 (UTC)
Message-Id: <20161208194238.6CFD589184@mail.oxnardindustries.com>
Date: Thu,  8 Dec 2016 19:42:33 +0000 (UTC)
From: some-person@some-other-server.com

hello world
text

Install and configure Dovecot

We will be using Dovecot as a POP3/IMAP server, so that users can receive and check their email. You can find more in­for­ma­tion about Dovecot on the Dovecot website.

Install Dovecot with the command:

sudo yum install dovecot
bash

Start Dovecot and check its status with the commands:

sudo systemctl start dovecot
sudo systemctl status dovecot
bash

If Dovecot is running, you will see output similar to:

[user@mail dovecot]$ sudo systemctl status dovecot -l
● dovecot.service - Dovecot IMAP/POP3 email server
   Loaded: loaded (/usr/lib/systemd/system/dovecot.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2016-12-08 21:04:48 UTC; 3s ago
  Process: 8985 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS)
 Main PID: 8989 (dovecot)
   CGroup: /system.slice/dovecot.service
           ├─8989 /usr/sbin/dovecot -F
           ├─8992 dovecot/anvil
           ├─8993 dovecot/log
           └─8995 dovecot/config

Dec 08 21:04:48 example.com systemd[1]: Starting Dovecot IMAP/POP3 email server...
Dec 08 21:04:48 example.com systemd[1]: Started Dovecot IMAP/POP3 email server.
Dec 08 21:04:48 example.com dovecot[8989]: master: Dovecot v2.2.10 starting up for imap (core dumps disabled)
text
Active: active (running) since Thu 2016-12-08 21:04:48 UTC; 3s ago
text

This means that Dovecot is installed and running.

Email Archiving Solutions
Never lose a business email again
  • Automatic, pro­fes­sion­al email archiving
  • Powerful en­ter­prise-wide search and eDis­cov­ery
  • One-click restore, download, and migration

Install Roundcube

Roundcube is a free, open source webmail program with robust built-in mul­ti­lin­gual ca­pa­bil­i­ties, and many con­ve­nient features for your users. You can read more about Roundcube on the Roundcube website.

Roundcube requires several PHP and MariaDB packages as a pre­req­ui­site. Install and/or update these first and start MariaDB with the commands:

sudo yum install mysql mariadb-server php php-common php-json php-xml php-mbstring php-mysql
sudo systemctl start mariadb
bash

Next, create a MariaDB username and database for the Roundcube in­stal­la­tion. Log into the MariaDB client with the command:

mysql -u root -p
bash

Log in with the root password, then create a Roundcube database:

create database roundcube_db;
sql

Create a user for this database. Note: Be sure to give the user a strong password.

grant all on roundcube_db.* to roundcube_user@localhost identified by 'roundcube_db_password';
sql

Replace:

  • roundcube_user with the username you want to create for your database.
  • roundcube_db_password with a strong password for this user.

Then exit MariaDB:

quit;
sql

Go to your website’s document root. Download the current release from the Roundcube website by using the command:

sudo wget [download URL]
bash

To find the download URL, visit the current release page on the Roundcube website. Copy the URL for the green “Download” link. You can paste it into the command line by clicking Shift + Insert.

For example, to download the current stable release as of this article’s pub­li­ca­tion the command is:

sudo wget https://github.com/roundcube/roundcubemail/releases/download/1.2.3/roundcubemail-1.2.3-complete.tar.gz
bash

Un­com­press this file using the command:

sudo tar -xvf [filename]
bash
Tip

You can use tab com­ple­tion to make this process easier. Type sudo tar -xvf roundcube then hit Tab. The shell will au­to­mat­i­cal­ly fill out the rest of the file name on the command line.

To unzip the file down­loaded above, the command is:

sudo tar -xvf roundcubemail-1.2.3-complete.tar.gz
bash

Rename the extracted directory roundcube:

sudo mv roundcubemail-1.2.3 roundcube
bash

Set the ownership of the files to the Apache user and start Apache:

sudo chown -R apache:apache roundcube
sudo systemctl start httpd
bash

To complete the in­stal­la­tion, switch to a browser and go to the URL http://example.com/roundcube/installer/.

Scroll down to the bottom of the page and click NEXT.

In the next section, you will need to fill out the database in­for­ma­tion.

Fill out the MariaDB database name, username, and password which you created above. Then scroll down and click CREATE CONFIG.

Note: There are many potential con­fig­u­ra­tions. You can change or set them at any time in the future.

After the config file is created, click CONTINUE to continue.

Click the Ini­tial­ize database button to set up the database.

Test the mail server con­fig­u­ra­tion by sending a test SMTP email.

  • Sender: Your new email address on this server. (For example, jdoe@example.com)
  • Recipient: An external email address. (For example, jdoe@gmail.com)

Then click Send test mail. After a few moments, check the external email account to see if your email was sent.

Test the IMAP con­fig­u­ra­tions by logging in with your username and password.

Note that your username is NOT your full email address. For example, log in with the username jdoe, not jdoe@example.com.

Now that the con­fig­u­ra­tion is complete, you can log in at http://example.com/roundcube to check and send email.

Business email
Stand out with your own branded email
  • Build trust with email matching your domain (you@yourbrand.com)
  • Free domain included to get your brand online instantly
  • Work smarter with AI writing support

Reviewer

Go to Main Menu