Servers

Mail Server With Front-end and Database Support

Before proceeding you must be having advance knowledge of DNS, postfix, dovecot, networking, and Linux command line. We can use both rhel7 as well as CentOS7, but in our case, we are using CentOS7.

Install Required Packages

1. Update your system to the latest repository data.

# yum update

2. The version of Postfix included in the main CentOS repository does not include support for MariaDB, therefore, you will need to install Postfix from the CentOS Plus repository. Before doing so, add exclusions to the “[base]” and “[updates]” repositories for the Postfix package to prevent it from being overwritten with updates that do not have MariaDB support.

Your repository should look like below:

[base]
name=CentOS-$releasever - Base
exclude=postfix
#released updates
[updates]
name=CentOS-$releasever -
Updates exclude=postfix

3. For the database support, we are using “mariadb“.

4. Install the required packages.

# yum –enablerepo=centosplus install postfix

# yum install dovecot mariadb-server dovecot-mysql

# yum install bind bind-utils

Check Information of a package using yum
Check Information of a package using yum
Check postfix and dovecot is installed on not using yum
Check postfix and dovecot is installed on not using yum

Setup DNS Server

  • Your “/etc/named.conf” file should look exactly like this.
Check local DNS configuration on Centos 7
Check local DNS configuration on Centos 7
  • Create your zone files by navigating to “/var/named/” and the group of zone files to “named”.
Check DNS Zone files on Centos 7
Check DNS Zone files on Centos 7
  • Start your local dns server and check it.
Confirm DNS working using NSLOOKUP
Confirm DNS working using NSLOOKUP

# service named restart

# chkconfig named on

# nslookup mail.example.com ( it should return your machine’s ip )

Setup the MariaDB for Virtual Domains and Users – Mail server with front-end

  1. Configure mariadb to start on boot, then start its service.

# systemctl enable mariadb

# systemctl start mariadb

2. After starting its service, run the following command:

# mysql_secure_installation

> set a root password ( in my case the password is ‘1’ )

> remove anonymous user accounts ( y )

> disable root logins outside of localhost ( y )

> remove test databases ( y )

> reload privilege tables ( y )

3. Start the mariadb shell.

# mysql -u root -p

4. Create a database for your mail server and switch to it.

> CREATE DATABASE mail;

> USE mail;

5. Create a mail administration user called “mail_admin” and grant it permissions on the “mail” database. Also, set password of your choice ( in our case password = password ).

> grant all on mail.* to ‘mail_admin’@’localhost’ identified by ‘password’;

> grant all on mail.* to ‘mail_admin’@’localhost.localdomain’ identified by ‘password’;

> flush privileges; ( don’t forget to run this command )

6. Create virtual domains table to store virtual domains.

> CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );

Check the Database entry for the domain
Check the Database entry for the domain

7. Create a table to handle mail forwarding.

> CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOTNULL, PRIMARY KEY (source) );

Check forwardings entry
Check forwardings entry

8. Create users table.

varchar(20) NOT NULL, PRIMARY KEY (email) );

> CREATETABLE users (emailvarchar(80) NOTNULL, password

Users table
Users table

9. Create a transports table.

> CREATE TABLE transport ( domain varchar(128) NOT NULL default ”, transportvarchar(128) NOT NULL default ”, UNIQUE KEY domain (domain) );

Check the transport table
Check the transport table

10. Exit the mariadb shell.

> quit

11. Bind your MariaDB to localhost (127.0.0.1) by editing “/etc/my.cnf”, adding the following to the “[mysqld]” section of the file.

> bind-address=127.0.0.1

Adding bind address to mysql conf file
Adding bind address to mysql conf file

**This is required for Postfix to be able to communicate with the database server. If you have MariaDBset up to listen on another IP address (such as an internal IP), you will need to substitute this IP addressin place of 127.0.0.1 during the Postfix configuration steps. It is not advisable to run MariaDB on apublicly-accessible IP address.**

12. Restart your database server.

#systemctl restart mariadb

Next, We will be configuring the postfix so that it can communicate with our database.

Configure Postfix to Work With MariaDB – Mail server with front-end

1. Create a virtual domain configuration file for postfix called “/etc/postfix/mysql-virtual_domains.cf”.

Postfix virtual domains config file
Postfix virtual domains config file

2. Create a virtual forwarding file for Postfix called “/etc/postfix/mysql-virtual_forwardings.cf”.

postifx mysql virtual forwardings config file
postifx mysql virtual forwardings config file

3. Create a virtual mailbox configuration file for Postfix called “/etc/postfix/mysql-virtual_mailboxes.cf”.

postfix mysql virtual mailboxes
postfix mysql virtual mailboxes

4. Create a virtual email mapping file for Postfix called “/etc/postfix/mysql-virtual_email2email.cf”.

postfix mysql virtual email to email config file
postfix mysql virtual email to email config file

5. Set proper permissions and ownership for these configuration files.

# chmod o= /etc/postfix/mysql-virtual_*.cf

# chgrp postfix /etc/postfix/mysql-virtual_*.cf

All the postfix config files
All the postfix config files

6. Create a user and a group for mail handling. All virtual mailboxes will be stored under this user’s home directory.

# groupadd -g 5000 vmail

# useradd -g vmail -u 5000 vmail -d /home/vmail -m

Create a user named as vmail
Create a user named as vmail

7. Complete the remaining steps required for Postfix configuration. Please be sure to replace “server.example.com” with the Host’s fully qualified domain name. If you are planning on using your own SSL certificate and key, replace “/etc/pki/dovecot/private/dovecot.pem” with the appropriate path to your ssl certificate.

Private key of dovecot
Private key of dovecot
Final main config file
Final main config file

8. Edit the file “/etc/postfix/master.cf” and add the Dovecot service to the bottom of the file.

Postfix Master config file
Postfix Master config file
Add dovecot support in postfix config file
Add dovecot support in postfix config file

9. Configure the postfix to start on boot and start it for the first time.

# systemctl enable postfix

# systemctl start postfix

Configure Dovecot

1. create a backup of your “dovecot.conf” file.

# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup

2. Your dovecot file should look like this.

Dovecot config file
Dovecot config file
More config of dovevot conf file
More config of dovevot conf file

3. MariaDB will be used to store password information, so “/etc/dovecot/dovecot-sql.conf.ext” must be created. Insert the following contents into the file.

Mysql docecot config file
Mysql docecot config file

4. Restrict access to the file by changing the permissions to allow users in the dovecot group to accessit, while denying access to others.

# chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext

# chmod o= /etc/dovecot/dovecot-sql.conf.ext

5. Configure Dovecot to start on boot, and start it for the first time.

# systemctl enable dovecot

# systemctl start dovecot

6. Check your “/var/log/maillog” file make sure dovecot is started without any errors.

7. Test your pop3 server to make sure it’s running properly.

# yum install telnet telnet-server

# chkconfig telnet on

# telnet mail.example.com pop3

Use telnet for pop3
Use telnet for pop3

Configure Mail Aliases

1. Edit the file “/etc/aliases

System Aliases
System Aliases

2. Update aliases and restart Postfix.

# new aliases

# systemctl restart postfix

This completes alias configuration. Next, test Postfix to make sure it’s operating properly.

Testing Postfix

1. Test Postfix for SMTP-AUTH and TLS.

# telnet localhost 25

Telnet SMTP
Telnet SMTP

2. While still connected, issue the following command.

> ehlo localhost

Telnet SMTP to check the configuration
Telnet SMTP to check the configuration

Setup Web Server With PHP Support – Mail server with front-end

Note: we are doing this because we want to add users and domains to database via webserver throughconnecting php with our mail server database.

1. Install the required packages.

# yum install php httpd php-mysql

2. Navigate to “/var/www/html/”.

> your index.php

This file is going to act as “Mail server with front-end”.

index.php file
index.php file

3. If you are using centos 6 or rhel 6 then you have to configure the “httpd” by loading php module to it.

Enable support for php5 in apache web server
Enable support for php5 in apache web server

4. If you want that your web server should open your “index.php” file by default then edit your “/etc/httpd/conf/httpd.conf” file like this.

Update apache directory index
Update apache directory index

Time To Test Our Server

* In our case, we are using windows7 with em-client (email client), you can use any client you like.

Open your browser and enter your webserver’s address to the URL field and you will see the following webpage.

Go to "mail.example.com"
Go to “mail.example.com”

Now open your mail client and enter your newly created account’s credentials.

Use any mail client
Use any mail client

Do as shown below.

Hit Yes
Hit Yes

Enter your account name and your name.

Add details of the newly created user
Add details of the newly created user

Finish the account setup.

Hit finish
Hit finish

Also Read: Completely Setup and Configure PXE installation server on Redhat 6.* or Centos 6.*

Comment here