This tutorial exists for these OS versions
- Ubuntu 13.10 (Saucy Salamander)
- Ubuntu 14.04 LTS (Trusty Tahr)
- Ubuntu 12.10 (Quantal Quetzal)
- Ubuntu 12.04 LTS (Precise Pangolin)
- Ubuntu 11.10 (Oneiric Ocelot)
- Ubuntu 11.04 (Natty Narwhal)
On this page
Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail (Ubuntu 11.10)
Author: Falko Timme
Follow me on Twitter
This tutorial is Copyright (c) 2011 by Falko Timme. It is derived from a tutorial from Christoph Haas which you can find at http://workaround.org. 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. I will also show how to install SquirrelMail as a webmail interface so that users can read and send emails and change their passwords.
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 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 Preliminary Note
This tutorial is based on Ubuntu 11.10 Server (Oneiric Ocelot), so you should set up a basic Ubuntu 11.10 server installation before you continue with this tutorial (e.g. as shown on the pages 1 - 3 in this tutorial: The Perfect Server - Ubuntu 11.10 [ISPConfig 3]). The system should have a static IP address. I use 192.168.0.100 as my IP address in this tutorial and server1.example.com as the hostname.
Make sure that you are logged in as root (type in
to become root), because we must run all the steps from this tutorial as root user.
It is very important that you make /bin/sh a symlink to /bin/bash...
Use dash as the default system shell (/bin/sh)? <-- No
... and that you disable AppArmor:
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils
2 Install Postfix, Courier, Saslauthd, MySQL, phpMyAdmin
To install Postfix, Courier, Saslauthd, MySQL, and phpMyAdmin, we simply run
apt-get install postfix postfix-mysql postfix-doc mysql-client mysql-server courier-authdaemon courier-authlib-mysql courier-pop courier-pop-ssl courier-imap courier-imap-ssl libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql openssl phpmyadmin apache2 libapache2-mod-php5 php5 php5-mysql libpam-smbpass
You will be asked a few questions:
New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword
Create directories for web-based administration? <-- No
General type of mail configuration: <-- Internet Site
System mail name: <-- server1.example.com
SSL certificate required <-- Ok
Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common? <-- No
3 Apply The Quota Patch To Postfix
We have to get the Postfix sources, patch it with the quota patch, build new Postfix .deb packages and install those .deb packages:
apt-get build-dep postfix
apt-get source postfix
(Make sure you use the correct Postfix version in the following commands. I have Postfix 2.8.5 installed. You can find out your Postfix version by running
postconf -d | grep mail_version
The output should look like this:
patch -p1 < ../postfix-vda-v10-2.8.5.patch
Next open debian/rules and change DEB_BUILD_HARDENING from 1 to 0:
[...] export DEB_BUILD_HARDENING=0 [...]
If you don't do this, your build will fail with the following error messages:
maildir.c: In function âsql2fileâ:
maildir.c:410:21: warning: format â%dâ expects argument of type âintâ, but argument 3 has type âsize_tâ [-Wformat]
maildir.c: In function âdeliver_maildirâ:
maildir.c:973:17: error: format not a string literal and no format arguments [-Werror=format-security]
maildir.c:976:17: error: format not a string literal and no format arguments [-Werror=format-security]
maildir.c:982:17: error: format not a string literal and no format arguments [-Werror=format-security]
maildir.c:985:17: error: format not a string literal and no format arguments [-Werror=format-security]
maildir.c: In function âsql2fileâ:
maildir.c:403:25: warning: ignoring return value of âreadâ, declared with attribute warn_unused_result [-Wunused-result]
maildir.c:416:26: warning: ignoring return value of âwriteâ, declared with attribute warn_unused_result [-Wunused-result]
cc1: some warnings being treated as errors
make: *** [maildir.o] Error 1
make: Leaving directory `/usr/src/postfix-2.8.5/src/virtual'
make: *** [update] Error 1
make: Leaving directory `/usr/src/postfix-2.8.5'
make: *** [build] Error 2
dpkg-buildpackage: error: debian/rules build gave error exit status 2
Now we can build the new Postfix .deb packages:
Now we go one directory up, that's where the new .deb packages have been created:
shows you the available packages:
[email protected]:/usr/src# ls -l
drwxr-xr-x 24 root root 4096 2011-10-14 10:14 linux-headers-3.0.0-12
drwxr-xr-x 7 root root 4096 2011-10-14 10:14 linux-headers-3.0.0-12-server
drwxr-xr-x 19 root root 4096 2011-11-04 18:19 postfix-2.8.5
-rw-r--r-- 1 root root 3664 2011-11-04 18:19 postfix_2.8.5-2~build1_amd64.changes
-rw-r--r-- 1 root root 1445386 2011-11-04 18:19 postfix_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 264915 2011-11-04 18:17 postfix_2.8.5-2~build1.diff.gz
-rw-r--r-- 1 root root 1259 2011-11-04 18:17 postfix_2.8.5-2~build1.dsc
-rw-r--r-- 1 root root 3647010 2011-10-06 06:03 postfix_2.8.5.orig.tar.gz
-rw-r--r-- 1 root root 45542 2011-11-04 18:19 postfix-cdb_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 152822 2011-11-04 18:19 postfix-dev_2.8.5-2~build1_all.deb
-rw-r--r-- 1 root root 1064624 2011-11-04 18:19 postfix-doc_2.8.5-2~build1_all.deb
-rw-r--r-- 1 root root 54258 2011-11-04 18:19 postfix-ldap_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 47372 2011-11-04 18:19 postfix-mysql_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 47382 2011-11-04 18:19 postfix-pcre_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 47456 2011-11-04 18:19 postfix-pgsql_2.8.5-2~build1_amd64.deb
-rw-r--r-- 1 root root 56777 2011-09-08 08:35 postfix-vda-v10-2.8.5.patch
Pick the postfix and postfix-mysql packages and install them like this:
dpkg -i postfix_2.8.5-2~build1_amd64.deb postfix-mysql_2.8.5-2~build1_amd64.deb
4 Create The MySQL Database For Postfix/Courier
Now we create a database called mail:
mysqladmin -u root -p create mail
Next, we go to the MySQL shell:
mysql -u root -p
On the MySQL shell, we create the user mail_admin with the passwort mail_admin_password (replace it with your own password) who has SELECT,INSERT,UPDATE,DELETE privileges on the mail database. This user will be used by Postfix and Courier to connect to the mail database:
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost' IDENTIFIED BY 'mail_admin_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost.localdomain' IDENTIFIED BY 'mail_admin_password';
Still on the MySQL shell, we create the tables needed by Postfix and Courier:
CREATE TABLE domains (
domain varchar(50) NOT NULL,
PRIMARY KEY (domain) )
CREATE TABLE forwardings (
source varchar(80) NOT NULL,
destination TEXT NOT NULL,
PRIMARY KEY (source) )
CREATE TABLE users (
email varchar(80) NOT NULL,
password varchar(20) NOT NULL,
quota INT(10) DEFAULT '10485760',
PRIMARY KEY (email)
CREATE TABLE transport (
domain varchar(128) NOT NULL default '',
transport varchar(128) NOT NULL default '',
UNIQUE KEY domain (domain)
As you may have noticed, with the quit; command we have left the MySQL shell and are back on the Linux shell.
The domains table will store each virtual domain that Postfix should receive emails for (e.g. example.com).
|[email protected]||[email protected]|
The users table stores all virtual users (i.e. email addresses, because the email address and user name is the same) and passwords (in encrypted form!) and a quota value for each mail box (in this example the default value is 10485760 bytes which means 10MB).
|[email protected]||No9.E4skNvGa. ("secret" in encrypted form)||10485760|
The transport table is optional, it is for advanced users. It allows to forward mails for single users, whole domains or all mails to another server. For example,
would forward all emails for example.com via the smtp protocol to the server with the IP address 220.127.116.11 (the square brackets  mean "do not make a lookup of the MX DNS record" (which makes sense for IP addresses...). If you use a fully qualified domain name (FQDN) instead you would not use the square brackets.).
BTW, (I'm assuming that the IP address of your mail server system is 192.168.0.100) you can access phpMyAdmin over http://192.168.0.100/phpmyadmin/ in a browser and log in as mail_admin. Then you can have a look at the database. Later on you can use phpMyAdmin to administrate your mail server.