Vous êtes sur la page 1sur 11

16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

Guides & Tutorials (/docs/)


Email Server Guides (/docs/email/)
Postx Guides (/docs/email/postx/)
Email with Postx, Dovecot and MySQL on CentOS 6

Email with Post x, Dovecot and MySQL on CentOS 6


Updated Thursday, March 26th, 2015 by Elle Krout

Use promo code DOCS10 for $10 credit on a new account. Try this Guide

Contribute on GitHub
View Project (https://github.com/linode/docs) | View File (https://github.com/linode/docs/blob/master/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6.md) | Edit File
(https://github.com/linode/docs/edit/master/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6.md)

The Postx Mail Transfer Agent (MTA) is a high performance open source e-mail server system. This guide will help you get Postx running on
your CentOS 6 Linode, using Dovecot for IMAP/POP3 service, and MySQL to store information on virtual domains and users.

Prior to using this guide, be sure you have followed the getting started guide (/docs/getting-started/) and set your hostname.

Note
The steps in this guide require root privileges. Be sure to run the steps below as root , or use su - root to log in as root. Certain commands below
cannot be run as sudo and must be run as root.

Install Required Packages


1. Install any outstanding package updates:

1 yum update

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

File excerpt: /etc/yum.repos.d/CentOS-Base.repo


1 [base]
2 name=CentOS-$releasever - Base
3 exclude=postfix
4
5 #released updates
6 [updates]
7 name=CentOS-$releasever - Updates
8 exclude=postfix

3. Install the required packages:

1 yum --enablerepo=centosplus install postfix


2 yum install dovecot mysql-server dovecot-mysql

This installs the Postx mail server, the MySQL database server, the Dovecot IMAP and POP daemons, and several supporting packages
that provide services related to authentication.

Next, set up a MySQL database to handle virtual domains and users.

Set up MySQL for Virtual Domains and Users


https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 1/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

Set up MySQL for Virtual Domains and Users


1. Congure MySQL to start on boot, then start MySQL:

1 chkconfig mysqld on
2 service mysqld start

2. Run mysql_secure_installation . You will be presented with the opportunity to change the MySQL root password, remove anonymous
user accounts, disable root logins outside of localhost, remove test databases, and reload privilege tables. It is recommended that you
answer yes to these options:

1 mysql_secure_installation

3. Start the MySQL shell:

1 mysql -u root -p

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

1 CREATE DATABASE mail;


2 USE mail;

5. Create a mail administration user called mail_admin and grant it permissions on the mail database. Please be sure to replace
mail_admin_password with a strong password:

1 GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost' IDENTIFIED BY 'mail_admin_password';


2 GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost.localdomain' IDENTIFIED BY 'mail_admin_password';
3 FLUSH PRIVILEGES;

6. Create the virtual domains table:

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

7. Create a table to handle mail forwarding:

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

8. Create the users table:

1 CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );

9. Create a transports table:

1 CREATE TABLE transport ( domain varchar(128) NOT NULL default '', transport varchar(128) NOT NULL default '', UNIQUE KEY domain

10. Exit the MySQL shell:

1 quit

11. Bind MySQL to localhost (127.0.0.1) by editing /etc/my.cnf , and adding the following to the [mysqld] section of the le:

File excerpt: /etc/my.cnf

1 bind-address=127.0.0.1

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 2/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

This is required for Postx to be able to communicate with the database server. If you have MySQL set up to listen on another IP address
(such as an internal IP), you will need to substitute this IP address in place of 127.0.0.1 during the Postx conguration steps. It is not
advisable to run MySQL on a publicly-accessible IP address.

12. Restart the database server:

1 service mysqld restart

Next, perform additional Postx conguration to set up communication with the database.

Con gure Post x to work with MySQL


Note
For the next four steps, replace mail_admin_password with the mail_admin password input earlier.

1. Create a virtual domain conguration le for Postx called /etc/postfix/mysql-virtual_domains.cf :

File: /etc/postfix/mysql-virtual_domains.cf
1 user = mail_admin
2 password = mail_admin_password
3 dbname = mail
4 query = SELECT domain AS virtual FROM domains WHERE domain='%s'
5 hosts = 127.0.0.1

2. Create a virtual forwarding le for Postx called /etc/postfix/mysql-virtual_forwardings.cf :

File: /etc/postfix/mysql-virtual_forwardings.cf

1 user = mail_admin
2 password = mail_admin_password
3 dbname = mail
4 query = SELECT destination FROM forwardings WHERE source='%s'
5 hosts = 127.0.0.1

3. Create a virtual mailbox conguration le for Postx called /etc/postfix/mysql-virtual_mailboxes.cf :

File: /etc/postfix/mysql-virtual_mailboxes.cf
1 user = mail_admin
2 password = mail_admin_password
3 dbname = mail
4 query = SELECT CONCAT(SUBSTRING_INDEX(email,<'@'>,-1),'/',SUBSTRING_INDEX(email,<'@'>,1),'/') FROM users WHERE email='%s'
5 hosts = 127.0.0.1

4. Create a virtual email mapping le for Postx called /etc/postfix/mysql-virtual_email2email.cf :

File: /etc/postfix/mysql-virtual_email2email.cf
1 user = mail_admin
2 password = mail_admin_password
3 dbname = mail
4 query = SELECT email FROM users WHERE email='%s'
5 hosts = 127.0.0.1

5. Set proper permissions and ownership for these conguration les:

1 chmod o= /etc/postfix/mysql-virtual_*.cf
2 chgrp postfix /etc/postfix/mysql-virtual_*.cf

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

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 3/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

1 groupadd -g 5000 vmail


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

7. Complete the remaining steps required for Postx conguration. Please be sure to replace server.example.com with the Linodes fully
qualied domain name. If you are planning on using your own SSL certicate and key, replace /etc/pki/dovecot/private/dovecot.pem
with the appropriate path:

1 postconf -e 'myhostname = server.example.com'


2 postconf -e 'mydestination = $myhostname, localhost, localhost.localdomain'
3 postconf -e 'mynetworks = 127.0.0.0/8'
4 postconf -e 'inet_interfaces = all'
5 postconf -e 'message_size_limit = 30720000'
6 postconf -e 'virtual_alias_domains ='
7 postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_emai
8 postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf'
9 postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf'
10 postconf -e 'virtual_mailbox_base = /home/vmail'
11 postconf -e 'virtual_uid_maps = static:5000'
12 postconf -e 'virtual_gid_maps = static:5000'
13 postconf -e 'smtpd_sasl_type = dovecot'
14 postconf -e 'smtpd_sasl_path = private/auth'
15 postconf -e 'smtpd_sasl_auth_enable = yes'
16 postconf -e 'broken_sasl_auth_clients = yes'
17 postconf -e 'smtpd_sasl_authenticated_header = yes'
18 postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'
19 postconf -e 'smtpd_use_tls = yes'
20 postconf -e 'smtpd_tls_cert_file = </etc/pki/dovecot/certs/dovecot.pem'
21 postconf -e 'smtpd_tls_key_file = </etc/pki/dovecot/private/dovecot.pem'
22 postconf -e 'virtual_create_maildirsize = yes'
23 postconf -e 'virtual_maildir_extended = yes'
24 postconf -e 'proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbo
25 postconf -e 'virtual_transport = dovecot'
26 postconf -e 'dovecot_destination_recipient_limit = 1'

8. Edit the le /etc/postfix/master.cf and add the Dovecot service to the bottom of the le:

File excerpt: /etc/postfix/master.cf

1 dovecot unix - n n - - pipe


2 flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}

9. Congure Postx to start on boot and start the service for the rst time:

1 chkconfig postfix on
2 service postfix start

This completes the conguration for Postx.

Con gure Dovecot


1. Move /etc/dovecot/dovecot.conf to a backup le:

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

2. Copy the following into the now-empty dovecot.conf le, substituting your systems domain name for example.com in line 37:

File: /etc/dovecot/dovecot.conf

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 4/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

1 protocols = imap pop3


2 log_timestamp = "%Y-%m-%d %H:%M:%S "
3 mail_location = maildir:/home/vmail/%d/%n/Maildir
4
5 ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
6 ssl_key = </etc/pki/dovecot/private/dovecot.pem
7
8 namespace {
9 type = private
10 separator = .
11 prefix = INBOX.
12 inbox = yes
13 }
14
15 service auth {
16 unix_listener auth-master {
17 mode = 0600
18 user = vmail
19 }
20
21 unix_listener /var/spool/postfix/private/auth {
22 mode = 0666
23 user = postfix
24 group = postfix
25 }
26
27 user = root
28 }
29
30 service auth-worker {
31 user = root
32 }
33
34 protocol lda {
35 log_path = /home/vmail/dovecot-deliver.log
36 auth_socket_path = /var/run/dovecot/auth-master
37 postmaster_address = postmaster@example.com
38 }
39
40 protocol pop3 {
41 pop3_uidl_format = %08Xu%08Xv
42 }
43
44 passdb {
45 driver = sql
46 args = /etc/dovecot/dovecot-sql.conf.ext
47 }
48
49 userdb {
50 driver = static
51 args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
52 }

3. MySQL will be used to store password information, so /etc/dovecot/dovecot-sql.conf.ext must be created. Insert the following contents
into the le, making sure to replace mail_admin_password with your mail password:

File: /etc/dovecot/dovecot-sql.conf.ext
1 driver = mysql
2 connect = host=127.0.0.1 dbname=mail user=mail_admin password=mail_admin_password
3 default_pass_scheme = CRYPT
4 password_query = SELECT email as user, password FROM users WHERE email='%u';

4. Restrict access to the le by changing the permissions to allow users in the dovecot group to access it, while denying access to others:

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


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

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 5/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

5. Congure Dovecot to start on boot, and start it for the rst time:

1 chkconfig dovecot on
2 service dovecot start

6. Check /var/log/maillog to make sure Dovecot started without errors. Your log should have lines similar to the following:

File excerpt: /var/log/maillog

1 Mar 18 15:21:59 sothoryos postfix/postfix-script[3069]: starting the Postfix mail system


2 Mar 18 15:22:00 sothoryos postfix/master[3070]: daemon started -- version 2.6.6, configuration /etc/postfix
3 Mar 18 15:32:03 sothoryos dovecot: master: Dovecot v2.0.9 starting up (core dumps disabled)

7. Test your POP3 server to make sure its running properly:

1 yum install telnet


2 telnet localhost pop3

8. The terminal should output results similar to the following:

1 Trying 127.0.0.1...
2 Connected to localhost.localdomain.
3 Escape character is '^]'.
4 +OK Dovecot ready.

9. Enter the command quit to return to your shell. This completes the Dovecot conguration. Next, youll make sure aliases are congured
properly.

Con gure Mail Aliases


1. Edit the le /etc/aliases , making sure the postmaster and root directives are set properly for your organization:

File: /etc/aliases

1 postmaster: root
2 root: postmaster@example.com

2. Update aliases and restart Postx:

1 newaliases
2 service postfix restart

This completes alias conguration. Next, test Postx to make sure its operating properly.

Testing Post x
1. Test Postx for SMTP-AUTH and TLS:

1 telnet localhost 25

2. While still connected, issue the following command:

1 ehlo localhost

3. You should see output similar to the following:

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 6/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

1 250-hostname.example.com
2 250-PIPELINING
3 250-SIZE 30720000
4 250-VRFY
5 250-ETRN
6 250-STARTTLS
7 250-AUTH PLAIN
8 250-AUTH=PLAIN
9 250-ENHANCEDSTATUSCODES
10 250-8BITMIME
11 250 DSN

4. Issue the command quit to terminate the telnet connection.

Next, populate the MySQL database with domains and email users.

Set Up and Test Domains and Users


Note
Before continuing, modify the DNS records for any domains that you wish to handle email by adding an MX record that points to your mail servers fully
qualied domain name. If MX records already exist for a domain you would like to handle the email for, either delete them or set them to a higher
priority number than your mail server. Smaller priority numbers indicate higher priority for mail delivery, with 0 being the highest priority.

In the following example, the MySQL shell is used to add support for the domain example.com, which will have an email account called sales.

1. Log into the MySQL shell:

1 mysql -u root -p

2. Switch to the mail database, add support for your domain, and create an email account. Be sure to replace example.com with your
domain name, sales@example.com with your chosen email, and password with a strong password:

1 USE mail;
2 INSERT INTO domains (domain) VALUES ('example.com');
3 INSERT INTO users (email, password) VALUES ('sales@example.com', ENCRYPT('password'));
4 quit

3. Prior to accessing any newly-created email account, a test message needs to be sent to create that users mailbox:

1 yum install mailx


2 mailx sales@example.com

Press Ctrl+D to complete the message. You can safely leave the eld for Cc: blank. This completes the conguration for a new domain
and email user.

Note
Given the possibility for virtual hosting a large number of domains on a single mail system, the username portion of an email address (i.e. before the @
sign) is not sufcient for authentication. When email users authenticate to the server, they must supply the entire email address created above as their
username.

Check Your Logs


After the test mail is sent, check the mail logs to make sure the mail was delivered.

1. Check the maillog located in /var/log/maillog . You should see something similar to the following:

File excerpt: /var/log/maillog

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 7/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

1 Mar 18 15:39:07 server postfix/cleanup[3252]: 444E34055: message-id=<20150318153907.444E34055@server.example.com>


2 Mar 18 15:39:07 server postfix/qmgr[3218]: 444E34055: from=<root@server.example.com>, size=489, nrcpt=1 (queue active)
3 Mar 18 15:39:07 server postfix/pipe[3258]: 444E34055: to=<sales@example.com>, relay=dovecot, delay=0.09, delays=0.04/0.01/0/0.0
4 Mar 18 15:39:07 server postfix/qmgr[3218]: 444E34055: removed

2. Check the Dovecot delivery log located in /home/vmail/dovecot-deliver.log . The contents should look similar to the following:

File excerpt: /home/vmail/dovecot-deliver.log


1 deliver(<sales@example.com>): 2011-01-21 20:03:19 Info: msgid=\<<20110121200319.E1D148908@hostname.example.com>>: saved mail to

Now you can test to see what the users of your email server would see with their email clients.

Test the Mailbox


1. To test the sales@example.com mailbox, navigate to the mailbox directory /home/vmail/example.com/sales/Maildir and issue the
following command:

1 cd /home/vmail/example.com/sales/Maildir
2 find

2. You should see output similar to the following:

1 .
2 ./dovecot-uidlist
3 ./cur
4 ./new
5 ./new/1285609582.P6115Q0M368794.li172-137
6 ./dovecot.index
7 ./dovecot.index.log
8 ./tmp

3. Test the maillbox by using a mail client. For this test, using mutt is recommended. If it is not installed by default, install it with
yum install mutt , then run:

1 mutt -f .

You may be prompted to create the root mailbox. This is not required.

4. If there is an email in the inbox, Postx, Dovecot, and MySQL have been successfully congured! To quit mutt press q .

(/docs/assets/postxcentos-mutt.png)

More Information

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 8/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be
useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

ISP-style Email Server with Debian-Lenny and Postx 2.5 guide (http://workaround.org/ispmail/lenny)
Groupware Server With Group-Ofce, Postx, Dovecot And SpamAssassin On Debian Lenny (5.0) (http://www.howtoforge.com/groupware-
server-with-group-ofce-postx-dovecot-spamassassin-on-debian-lenny)
Postx MySQL Howto (http://www.postx.org/MYSQL_README.html)
Postx SASL Howto (http://www.postx.org/SASL_README.html)
Dovecot Documentation Wiki (http://wiki.dovecot.org/)
MySQL Documentation (http://dev.mysql.com/doc/)

This guide is published under a CC BY-ND 4.0 (https://creativecommons.org/licenses/by-nd/4.0) license.

Get paid to write for Linode.


We're always expanding our docs. If you like to help people, can write, and want to earn some cash, learn how you can earn
$250 for every guide you write (/docs/contribute) and we publish.

Get started in the Linode Cloud today.

Create an Account (https://manager.linode.com/session/signup)

Overview (https://www.linode.com/linodes)
Plans & Pricing (https://www.linode.com/pricing)

Features (https://www.linode.com/linodes)

Add-Ons (https://www.linode.com/addons)

Managed (https://www.linode.com/managed)

Professional Services (https://www.linode.com/professional-services)

Resources (https://www.linode.com/docs)
Guides & Tutorials (https://www.linode.com/docs)

Speed Test (https://www.linode.com/speedtest)

Forum (https://forum.linode.com/)

Chat (https://www.linode.com/chat)

System Status (http://status.linode.com/)

Company (https://www.linode.com/about)
About Us (https://www.linode.com/about)

Blog (https://blog.linode.com)

Press (https://www.linode.com/press)

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 9/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

Referral System (https://www.linode.com/referrals)

Careers (https://www.linode.com/careers)

Contact Us (https://www.linode.com/contact)
855-4-LINODE (tel:+18554546633)

(855-454-6633) (tel:+18554546633)

Intl.: +1 609-380-7100 (tel:+16093807100)

Email us (mailto:support@linode.com)

(https://facebook.com/linode) (https://twitter.com/linode) (https://plus.google.com/+linode/) (https://linkedin.com/company/linode)

2017 Linode, LLC

Terms of Service (https://www.linode.com/tos)

Privacy Policy (https://www.linode.com/privacy)

Security (https://www.linode.com/security)

Standards & Compliance (https://www.linode.com/compliance)

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 10/11
16/3/2017 Email with Postx, Dovecot and MySQL on CentOS 6

https://www.linode.com/docs/email/postx/email-with-postx-dovecot-and-mysql-on-centos-6 11/11