Setting up an email server can be a very daunting task when you aren’t too sure about how an email server works. Thankfully there are tools and configurations you can use that will make the job of managing an email server incredibly easy to understand and perform.
For a long time I was creating new linux OS users to create a new mailbox. The problem here is it meant that only one server could handle a domain, this is when I discovered virtual mailboxes which allows one single server to handle emails for a large number of different domains. A lot of the tutorials out there will show you how to do this using the default database system that postfix uses. This is perfectly fine, but it becomes a major chore to manage when you start to get a large number of domains and emails. After some searching I discovered a web application called postfix admin. This magic piece of software lets you manage all your domains and mailboxes from your web browser without ever needing to ssh into the server. To make things even better, this will also replace the default postfix DB with a much more familiar MySQL database. So lets get to it, this guide will explain everything that you need to know about setting up an email server that uses Postfix and Dovecot with Postfix Admin to manager your sever.
Create Mail User
First thing we want to do is create the mail user. This will be a user that has access to manage all of the files and folders that your mail server will need to manage. The following commands will create a user and configure it with the correct permission and UID. This step is very important as all of the config code below is assuming that you are going to be using the same user created below. If you know what you are doing then of course you can change this to whatever you wish.
groupadd vmail -g 2222 useradd vmail -r -g 2222 -u 2222 -d /var/vmail -m -c "mail user"
We now have a user and a group called vmail with the uid and gid of 2222. This is the ID that needs to be used when configuring Postfix and Dovecot.
Install And Configure Postfix
First and most important, install your SMTP server. There are various ways in which you can do this depending on your operating system. To install this on a Centos/ Red Hat OS run the following command.
yum install postfix
Once postfix is installed, navigate to /etc/postfix/main.cf. Open this file and edit the contents to make it look like the following.
/etc/postfix/main.cf
queue_directory = /var/spool/postfix command_directory = /usr/sbin daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix mail_owner = postfix unknown_local_recipient_reject_code = 550 alias_maps = hash:/etc/postfix/aliases alias_database = $alias_maps inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost debug_peer_level = 2 debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5 sendmail_path = /usr/sbin/sendmail.postfix newaliases_path = /usr/bin/newaliases.postfix mailq_path = /usr/bin/mailq.postfix setgid_group = postdrop html_directory = no relay_domains = * virtual_mailbox_domains = proxy:mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf virtual_alias_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf virtual_mailbox_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf virtual_create_maildirsize = yes virtual_mailbox_extended = yes virtual_mailbox_limit_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf virtual_mailbox_limit_override = yes virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later. virtual_overquota_bounce = yes virtual_mailbox_base = /var/vmail virtual_minimum_uid = 2222 virtual_transport = virtual virtual_uid_maps = static:2222 virtual_gid_maps = static:2222 smtpd_sasl_auth_enable = yes smtpd_sasl_type = dovecot smtpd_sasl_path = /var/run/dovecot/auth-client smtpd_sasl_security_options = noanonymous smtpd_sasl_tls_security_options = $smtpd_sasl_security_options smtpd_sasl_local_domain = $mydomain broken_sasl_auth_clients = yes smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtpd_milters = inet:127.0.0.1:8891 non_smtpd_milters = $smtpd_milters milter_default_action = accept milter_protocol = 2
One last change to the config files is to the master.cf file. Add or uncomment the following line. There is a good chance the line exists already and its commented (has a # at the start). Removing the hash will do.
/etc/postfix/master.cf
submission inet n - n - - smtpd
you have now setup the cf files, but we still have to setup the database. We are going to use MySQL as this allows us to link it in with a web interface and makes life so much easier. If you look at the contents added to the main.cf file you will see references to a bunch of files in an sql folder. All of these files need to be created. If you have downloaded postfix admin there will be an sh script that you can run and it will generate all of the files for you. This saves you a lot of trouble as these are annoying to write, but to make sure that this guide covers everything I will show you everything that needs to be added for these files. Before we can do this we need to setup PostfixAdmin as this will generate the database that you need to use. So lets setup PostfixAdmin.
Install PostfixAdmin
PostfixAdmin is a free, web interface that allows you to manage the database that postfix uses for virtual users. First things first, download and unzip the latest version of the code, which can be found here http://postfixadmin.sourceforge.net/. You will need to configure apache with an alias or virtualhost depending on what you want to do.
PostfixAdmin comes with its own extensive install guide that comes zipped in the folder. The setup is very straightforward. Run the setup php script through the browser and this will generate the database and add some admin information so you can begin working on it. Once you have it installed and setup, you will now have a database that can be used by postfix. Now we need to tell postfix to actually use this database. We have already instructed postfix that it will be using these files, so all you will need to do is create the following files and add the content to them.
Note:Â PostfixAdmin comes with a script to generate these files automatically for you. If you run the script (all documented in the setup notes) it will create the files in /tmp. If you decide to do this, take these files and copy them into /etc/postfix/sql and you will be done. If you dont know how to do this you can create them yourself.
Here are the database files that you need to create and add the content to.
/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
/etc/postfix/sql/mysql_virtual_alias_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT goto FROM alias WHERE address='%s' AND active = '1' #expansion_limit = 100
/etc/postfix/sql/mysql_virtual_domains_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
/etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'
/etc/postfix/sql/mysql_virtual_mailbox_maps.cf
user = postfix password = password hosts = localhost dbname = postfix query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
Finally you will need to restart postfix to apply all of the changes that you have made today. Make sure to check the log when restarting to ensure there weren’t any lines that were entered wrong etc.
service postfix restart
Install And Configure Dovecot
Setting up Dovecot is a little less tricky than Postfix and there are less things that can go wrong, so consider the worst out of the way. First things first, let’s install Dovecot.
yum install dovecot
Once installed, navigate to the install directory and modify the following files.
/etc/dovecot/dovecot.conf
listen = * ssl = no protocols = imap lmtp disable_plaintext_auth = no auth_mechanisms = plain login mail_access_groups = vmail default_login_user = vmail first_valid_uid = 2222 first_valid_gid = 2222 #mail_location = maildir:~/Maildir mail_location = maildir:/var/vmail/%d/%n userdb { driver = sql args = /etc/dovecot/dovecot-sql.conf } passdb { driver = sql args = /etc/dovecot/dovecot-sql.conf } service auth { unix_listener auth-client { group = postfix mode = 0660 user = postfix } user = root } service imap-login { process_min_avail = 1 user = vmail }
/etc/dovecot/dovecot-sql.conf
connect = host=127.0.0.1 dbname=postfix user=postfix password=password driver = mysql # Default password scheme - change to match your Postfixadmin setting. # depends on your $CONF['encrypt'] setting: # md5crypt -> MD5-CRYPT # md5 -> PLAIN-MD5 # cleartext -> PLAIN default_pass_scheme = MD5-CRYPT password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1' # Query to retrieve user information, note uid matches dovecot.conf AND Postfix virtual_uid_maps parameter. user_query = SELECT maildir, 2222 AS uid, 2222 AS gid FROM mailbox WHERE username = '%u' AND active='1' # MYSQL : user_query = SELECT CONCAT('/var/vmail/', maildir) AS home, 2222 AS uid, 2222 AS gid, CONCAT('*:bytes=', quota) AS quota_rule FROM mailbox WHERE username = '%u' AND active='1' user_query = SELECT maildir, 2222 AS uid, 2222 AS gid, CONCAT('dict:storage=',floor(quota/1000),' proxy::quota') as quota FROM mailbox WHERE username = '%u' AND active='1'
/etc/dovecot/dovecot-dict-quota.conf
driver = mysql connect = host=localhost dbname=postfix user=postfix password=password default_pass_scheme = MD5-CRYPT table = quota select_field = current where_field = path username_field = username
This is everything you need to get dovecot working. Note the UID and GID values are using 2222 which is the same value that was setup for the user and group earlier in the tutorial. Of course please change the username/password/db name for all of the SQLÂ queries for everything.
Final Notes
Everything should now be up and running. Restart everything just to make sure that everything is working fresh and clean.
service postfix restart service dovecot restart
You should be able to navigate to Postfix Admin in your web browser and work away with managing your web server. I understand that this is an incredibly frustrating part of the setup as there are so many things that can go wrong. I have gathered everything that I have encountered from the times when I have set this up. Hopefully everything went well for you, but if not these resources might help you.
Issue Fixes
Hopefully everything went well, but I know all to well that things never go smooth. Chances are you have some issues that you have encountered while setting this up. The following resources might help you solve any issues that you might be getting while setting this up.
Mysql Access Denied
This error is a damn nightmare. It can be one of many things to solve and hopefully its the obvious error that the account you are using has a bad password or does not have the correct permissions to access the database that you are trying to connect to. If you are 100% sure the user has access and can access the database via the command line then it might be an issue with MySQL. The following tutorial may help you get past this issue.
 Dovecot – unknown database driver MySQL
This is a common issue with a simple fix. It just means that you have not installed the dovecot mysql driver. The following guide will show you how to do this.
Hello,
This is a good one.
Could you pls add ssl integrations in to these..
Have tried a few and didnt help.
This helps. A lot of Thanks.