I have spent about 3 days to install the Postfix, dovecot and mysql on my VPS server. It has been a very frustrating process. I have googled painfully for 3 days and collected the information piece by piece and eventually made this combination work.
Just want to list steps and all configuration files together, hopefully useful for who is also undergoing the painful process.
- make mysql ready, and create database postfix (or whatever the name you want), create mysql user postfix and grant all privilege to postfix database.
Create the following tables:
CREATE TABLE virtual_domains (
id int(11) NOT NULL auto_increment,
name varchar(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE virtual_aliases (
id int(11) NOT NULL auto_increment,
domain_id int(11) NOT NULL,
source varchar(100) NOT NULL,
destination varchar(100) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE virtual_users (
id int(11) NOT NULL auto_increment,
domain_id int(11) NOT NULL,
password varchar(32) NOT NULL,
email varchar(100) NOT NULL,
maildir varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Compile Postfix with mysql support, you should see the bunch of postfix configuration files:
main.cf
[root@mail postfix]#postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /var/postfix/usr/sbin
compatibility_level = 2
daemon_directory = /var/postfix/usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6 /binddd
$daemon_directory/$process_name $process_id & sleep 5
home_mailbox = Maildir/
html_directory = no
inet_interfaces = all
inet_protocols = ipv4
mail_owner = postfix
mail_spool_directory = /home
mailq_path = /var/postfix/usr/bin/mailq
manpage_directory = /usr/local/man
meta_directory = /etc/postfix
mydomain = myspeedshow.com
myhostname = mail.yourdoamin.com
mynetworks_style = host
myorigin = $mydomain
newaliases_path = /var/postfix/usr/bin/newaliases
postscreen_greet_banner = "before smtp banner"
postscreen_greet_wait = 2s
postscreen_non_smtp_command_enable = no
postscreen_pipelining_enable = no
queue_directory = /var/spool/postfix
readme_directory = no
recipient_delimiter = +
sample_directory = /etc/postfix
sendmail_path = /var/postfix/usr/sbin/sendmail
setgid_group = postdrop
shlib_directory = no
smtpd_banner = $myhostname ESMTP $mail_name
smtpd_recipient_restrictions =
reject_invalid_hostname,<br>
reject_unknown_recipient_domain,
reject_unauth_pipelining,
permit_mynetworks,
reject_unauth_destination,
reject_rbl_client zen.spamhaus.org,
reject_rbl_client bl.spamcop.net,
reject_rbl_client dnsbl.sorbs.net,
reject_rbl_client cbl.abuseat.org,
reject_rbl_client b.barracudacentral.org,
reject_rbl_client dnsbl-1.uceprotect.net,
permit<br>
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = private/auth
smtpd_sasl_type = dovecot
smtpd_tls_auth_only = yes
smtputf8_enable = no
unknown_local_recipient_reject_code = 550
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias- maps.cf,mysql:/etc/postfix/mysql-email2email.cf
virtual_gid_maps = static:5000
virtual_mailbox_base = /var/vmail
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_limit = 51200000
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_transport = virtual
virtual_uid_maps = static:5000
master.cf
relay unix - - n - - smtp
flush unix n - n 1000? 0 flush
trace unix - - n - 0 bounce
verify unix - - n - 1 verify
rewrite unix - - - - - trivial-rewrite
proxymap unix - - n - - proxymap
anvil unix - - n - 1 anvil
scache unix - - n - 1 scache
discard unix - - n - - discard
tlsmgr unix - - n 1000? 1 tlsmgr
retry unix - - n - - error
proxywrite unix - - n - 1 proxymap
smtp unix - - n - - smtp
smtp inet n - n - 1 postscreen
smtpd pass - - n - - smtpd
lmtp unix - - n - - lmtp
cleanup unix n - n - 0 cleanup
qmgr fifo n - n 300 1 qmgr
virtual unix - n n - - virtual
dovecot unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/local/libexec/dovecot /dovecot-lda -f ${sender} -d ${recipient}
mysql-virtual-mailbox-domains.cf
user=postfix
password=yourpassword
host=127.0.0.1
dbname=postfix
query=select name from virtual_domains where name='%s'
mysql-virtual-mailbox-maps.cf
user=postfix
password=yourpassword
dbname=postfix
query=select maildir from virtual_users where email='%s'
mysql-virtual-alias-maps.cf
user=postfix
password=yourpassword
host=127.0.0.1
dbname=postfix
query=select destination from virtual_aliases where source='%s'
The next step is to configure the Dovecot.
10-auth.conf
disable_plaintext_auth = yes
auth_mechanisms = plain login
!include auth-sql.conf.ext
comments out all other !include
auth-sql.conf.ext
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/vmail/%d/%n
}
10-mail.conf comments out all mail_location
Here we use Maildir format to store the email in:
/var/vmail/domain/user/Maildir/ folder, in virtual_users table, the column maildir should be in the following format 'yourdomain.com/user/Maildir/'
If you have not populated the virtual_users.maildir
column correctly, the postfix will use mailbox format, which store all mail belong to a domain to a file /var/vmail/1
.