Virtual Users And Domains With Postfix, Courier And MySQL (CentOS 5.1)

Want to support HowtoForge? Become a subscriber!
Submitted by falko (Contact Author) (Forums) on Sun, 2007-12-09 17:56. :: Anti-Spam/Virus | CentOS | Postfix

Virtual Users And Domains With Postfix, Courier And MySQL (CentOS 5.1)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 12/05/2007

This tutorial is Copyright (c) 2007 by Falko Timme. It is derived from a tutorial from Christoph Haas which you can find at You are free to use this tutorial under the Creative Commons license 2.5 or any later version.

This document describes how to install a Postfix mail server that is based on virtual users and domains, i.e. users and domains that are in a MySQL database. I'll also demonstrate the installation and configuration of Courier (Courier-POP3, Courier-IMAP), so that Courier can authenticate against the same MySQL database Postfix uses.

The resulting Postfix server is capable of SMTP-AUTH and TLS and quota (quota is not built into Postfix by default, I'll show how to patch your Postfix appropriately). Passwords are stored in encrypted form in the database (most documents I found were dealing with plain text passwords which is a security risk). In addition to that, this tutorial covers the installation of Amavisd, SpamAssassin and ClamAV so that emails will be scanned for spam and viruses.

The advantage of such a "virtual" setup (virtual users and domains in a MySQL database) is that it is far more performant than a setup that is based on "real" system users. With this virtual setup your mail server can handle thousands of domains and users. Besides, it is easier to administrate because you only have to deal with the MySQL database when you add new users/domains or edit existing ones. No more postmap commands to create db files, no more reloading of Postfix, etc. For the administration of the MySQL database you can use web based tools like phpMyAdmin which will also be installed in this howto. The third advantage is that users have an email address as user name (instead of a user name + an email address) which is easier to understand and keep in mind.

This tutorial is based on CentOS 5.1 (i386). You should already have set up a basic CentOS system, as described here: and Plus, you should make sure that the firewall is off (at least for now) and that SELinux is disabled (this is important!), as shown in the chapter six on

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!


1 Edit /etc/hosts

Our hostname in this example is, and it has the IP address, so we change /etc/hosts as follows:

vi /etc/hosts

# Do not remove the following line, or various programs
# that require network functionality will fail.               localhost.localdomain localhost  server1
::1             localhost6.localdomain6 localhost6


2 Install Some Software

First we import the GPG keys for software packages:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*

Then we update our existing packages on the system:

yum update

Now we install some software that we need later on:

yum groupinstall 'Development Tools'

yum groupinstall 'Development Libraries'


3 Install Apache, MySQL, phpMyAdmin

First we enable the RPMforge repository on our CentOS system as lots of the packages that we are going to install in the course of this tutorial are not available in the official CentOS 5.1 repositories:

rpm -Uhv

(If the above link doesn't work anymore, you can find the current version of rpmforge-release here:

Afterwards we can install the needed packages with one single command (including the packages we need to build Courier-IMAP):

yum install ntp httpd mysql-server php php-mysql php-mbstring php-mcrypt phpmyadmin rpm-build gcc mysql-devel openssl-devel cyrus-sasl-devel pkgconfig zlib-devel pcre-devel openldap-devel postgresql-devel expect libtool-ltdl-devel openldap-servers libtool gdbm-devel pam-devel gamin-devel


4 Install Courier-IMAP, Courier-Authlib, And Maildrop

Unfortunately there are no rpm packages for Courier-IMAP, Courier-Authlib, and Maildrop, therefore we have to install them as described in this tutorial: How To Install courier-imap, courier-authlib, And maildrop On Fedora, RedHat, CentOS

In case you have problems creating the rpm packages, you can download mine:


5 Apply Quota Patch To Postfix

We have to get the Postfix source rpm, patch it with the quota patch, build a new Postfix rpm package and install it.

cd /usr/src
rpm -ivh postfix-2.3.3-2.src.rpm

The last command will show some warnings that you can ignore:

warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root

cd /usr/src/redhat/SOURCES
gunzip postfix-2.3.3-vda.patch.gz
cd /usr/src/redhat/SPECS/

Now we must edit the file postfix.spec:

vi postfix.spec

Change %define MYSQL 0 to %define MYSQL 1, add Patch0: postfix-2.3.3-vda.patch to the # Patches stanza, and finally add %patch0 -p1 -b .vda to the %setup -q stanza:

%define MYSQL 1
# Patches

Patch0: postfix-2.3.3-vda.patch
Patch1: postfix-2.1.1-config.patch
Patch3: postfix-alternatives.patch
Patch6: postfix-2.1.1-obsolete.patch
Patch7: postfix-2.1.5-aliases.patch
Patch8: postfix-large-fs.patch
Patch9: postfix-2.2.5-cyrus.patch
%setup -q
# Apply obligatory patches
%patch0 -p1 -b .vda
%patch1 -p1 -b .config
%patch3 -p1 -b .alternatives
%patch6 -p1 -b .obsolete
%patch7 -p1 -b .aliases
%patch8 -p1 -b .large-fs
%patch9 -p1 -b .cyrus

Then we build our new Postfix rpm package with quota and MySQL support:

rpmbuild -ba postfix.spec

You will see lots of warnings like these that you can ignore:

msg.h:12:1: warning: "/*" within comment
msg.h:14:1: warning: "/*" within comment
msg.h:33:1: warning: "/*" within comment
msg.h:34:1: warning: "/*" within comment
msg.h:35:1: warning: "/*" within comment
msg.h:36:1: warning: "/*" within comment

Our Postfix rpm package is created in /usr/src/redhat/RPMS/i386, so we go there:

cd /usr/src/redhat/RPMS/i386

The command

ls -l

shows you the available packages:

[root@server1 i386]# ls -l
total 11280
-rw-r--r-- 1 root root 3819299 Dec  5 15:25 postfix-2.3.3-2.i386.rpm
-rw-r--r-- 1 root root 7655069 Dec  5 15:25 postfix-debuginfo-2.3.3-2.i386.rpm
-rw-r--r-- 1 root root   50346 Dec  5 15:25 postfix-pflogsumm-2.3.3-2.i386.rpm
[root@server1 i386]#

Pick the Postfix package and install it like this:

rpm -ivh postfix-2.3.3-2.i386.rpm

(In case you have problems creating the Postfix rpm package, you can download mine from here: postfix-2.3.3-2.i386.rpm.)


6 Set MySQL Passwords And Configure phpMyAdmin

Start MySQL:

chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start

Then set passwords for the MySQL root account:

mysqladmin -u root password yourrootsqlpassword
mysqladmin -h -u root password yourrootsqlpassword

Now we configure phpMyAdmin. We change the Apache configuration so that phpMyAdmin allows connections not just from localhost (by commenting out the <Directory "/usr/share/phpmyadmin"> stanza):

vi /etc/httpd/conf.d/phpmyadmin.conf

#  Web application to manage MySQL

#<Directory "/usr/share/phpmyadmin">
#  Order Deny,Allow
#  Deny from all
#  Allow from

Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpMyAdmin /usr/share/phpmyadmin
Alias /mysqladmin /usr/share/phpmyadmin

Next we change the authentication in phpMyAdmin from cookie to http:

vi /usr/share/phpmyadmin/

/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'http';

Then we create the system startup links for Apache and start it:

chkconfig --levels 235 httpd on
/etc/init.d/httpd start

Now you can direct your browser to or and log in with the user name root and your new root MySQL password.

Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Tue, 2009-09-08 09:47.



i have this setup running for 1 year now, lately it started marking mails as spam that are not.

I tried putting  "whitelist_from" into /etc/mail/spammassin/,

but the mails are still marked as spam.

So I have to put the whitelist somewhere else in this setup?

Submitted by vargadanis (registered user) on Sun, 2008-04-20 15:14.

I have created new RPMs for courier IMAP. They are avaliable here
version 4.3.1

Submitted by doiu (registered user) on Thu, 2008-05-29 19:31.
I use this tutorial.
It is excellent.
But I need to let users to change they password so I make this simple php script.
I hope will be useful for someone.

Best regards,
Doru Barit

Submitted by Arfie (not registered) on Fri, 2009-10-23 11:00.

Hai doiu,

I have a problem with my squirrelmail.

When i want to change my password, i get error like this:

  ERROR:Connection refused (111)

And when i go to :

to get module chpasswd, this site is down.

would you mind if you give me that module to fix my problem?

Thank you very much 


Submitted by Anonymous (not registered) on Fri, 2008-11-14 21:09.

How do you create user mailboxes? with maildirmake? or just sending an email to you new mailbox? I followed this guide too but i dunno how to create mailboxes in /home/vmail.

Any suggestion?

Submitted by Anonymous (not registered) on Tue, 2008-10-14 19:21.
Is it possible to use proftpd instaed of vsftpd?
Submitted by Daniel (not registered) on Thu, 2010-11-25 11:43.

Look at this!

Submitted by Elfofdark (not registered) on Tue, 2009-09-08 09:02.

I can't access http://ipd/phpmyadmin, it gives to me 404 not found. Maybe because i don't have anything in /var/www/html ?
 I did everything you said up every step with no error but i can't understand what did i miss. Please help me!