Dedicated Mail Server Hosting Multiple Domains – Step 3 – Postfix and Dovecot

On a typical Linux mail server mail is stored on the disk either using one big file in /var/mai/%user either in one Maildir in users’s home folder. This Maildir usually contains one small file for each mail stored on your server and some indexing information etc.

In this setup, because users are stored on a virtual manner (in an SQL database) we need to create a user on the system, to which all the Maildirs will belong to.

adduser mails --shell=/usr/sbin/nologin

This will create a user that has “nologin” shell. There’s no need to set a password for it, as we will never use it for SSH login and by default ssh does not allow you to login remotely using empty passwords.

One of the most important sub-steps here is to get this user’s UID. For example I did not create any user on CentOS install, so my new user’s ID is 1000, it also gas GID 1000. This is important becautse your mail server will use this ID to store and access files.

We need to create a new SQL user to be used by Postfix MTA and Dovecot as the one used by Postfix Admin has write permissions and for this one there’s no need. After launching from the shell mysql -p and entering the password:

grant select on postfix.* to postfix@localhost identified by 'POSTFIX_PASSWORD';
flush privileges;

Then we start creating SQL config files for Postfix in /etc/postfix as follows:

mysql_virtual_alias_maps.cf

user = postfix
password = POSTFIX_PASSWORD
hosts = 127.0.0.1
dbname = postfix
query = SELECT goto FROM alias WHERE address='%s' AND active = 1

mysql_virtual_domains_maps.cf

user = postfix
password = POSTFIX_PASSWORD
hosts = 127.0.0.1
dbname = postfix
query = SELECT domain FROM domain WHERE domain='%s' and backupmx = '0' and active = '1'

mysql_virtual_mailbox_maps.cf

user = postfix
password = POSTFIX_PASSWORD
hosts = 127.0.0.1
dbname = postfix
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = 1

mysql_relay_domains_maps.cf

user = postfix
password = POSTFIX_PASSWORD
hosts = 127.0.0.1
dbname = postfix
query = SELECT domain FROM domain WHERE domain='%s' and backupmx = '1'

Then in Postfix’s main.cf

virtual_alias_maps = proxy:mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_gid_maps = static:1000
virtual_mailbox_base = /home/mails
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
relay_domains = $mydestination, proxy:mysql:/etc/postfix/mysql_relay_domains_maps.cf
virtual_minimum_uid = 1000
virtual_transport = dovecot
virtual_uid_maps = static:1000

and in master.cf add the lines:

dovecot   unix  -       n       n       -       -       pipe
 flags=DRhu user=mails:mails argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

The next step is Dovecot. It is important to set it up as Postfix uses Dovecot LDA to deliver mail. After a simple yum install dovecot dovecot-mysql we need to change directory to /etc/dovecot/conf.d where we edit some files as follows:

10-auth.conf

disable_plaintext_auth = no # some people just don' bother
auth_mechanisms = plain login # Otherwise Windows 8/8.1's Mail app won't authenticate
#!include auth-system.conf.ext # comment this line
!include auth-sql.conf.ext #uncomment this one

15-lda.conf

lda_mailbox_autocreate = yes
lda_mailbox_autosubscribe = yes

10-mail.conf

mail_location = maildir:/home/mails/%u:INDEX=/home/mails/%u/index

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

driver = mysql
connect = host=localhost dbname=postfix user=postfix password=$POSTFIX_PASSWORD

password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT username, 1000 AS uid, 1000 AS gid , '/home/mails/%u' as home FROM mailbox WHERE username = '%u'

Instead of 1000 you might put in the UID/GID from the user you created.

Have you added a new domain in your Postfix Admin interface? How about a new user? it is time to send him a test mail.

echo test |mail -s test USER@DOMAIN.COM

Now tail your mailllog. tail /var/log/maillog should show you something like:

Sep  6 12:31:46 mail postfix/qmgr[32482]: C44CD80BBFFE: from=<root@mail.cheriches.com>, size=438, nrcpt=1 (queue active)
Sep  6 12:31:46 mail dovecot: lda(emil@cheriches.com): msgid=<20140906085511.C44CD80BBFFE@mail.cheriches.com>: saved mail to INBOX
Sep  6 12:31:46 mail postfix/pipe[365]: C44CD80BBFFE: to=<emil@cheriches.com>, relay=dovecot, delay=5880, delays=5880/0.02/0/0.22, dsn=2.0.0, status=sent (delivered via dovecot service)

So, right now you have a functional email server, meaning that you are able to send and receive mail, but there’s still a lot of work to be done.