在Linux上使用Postfix+dovecot+MySQL+PostfixAdmin实现基本的邮件服务
※转载请注明出处

1. 安装MySQL
可以用rpm安装,也可以编译安装,这里就不详细介绍了。
 
添加Postfix数据库
mysql> CREATE DATABASE postfix;
mysql> CREATE USER
'postfix'@'localhost' IDENTIFIED BY 'postfix';
mysql> GRANT ALL PRIVILEGES ON `postfix` . * TO
'postfix'@'localhost';
 
2. 安装配置PostfixAdmin

http://sourceforge.net/projects/postfixadmin/下载最新的软件包。
# tar zxvf postfixadmin-2.2.1.1.tar.gz
# mv postfixadmin-2.2.1.1 /var/www/postfixadmin
# cd /var/www/postfixadmin
# vim config.inc.php
修改下面相应的参数
$CONF['configured'] = true;
$CONF['default_language'] = 'cn';
$CONF['postfix_admin_url'] = '/postfixadmin';
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = ‘postfix';
$CONF['database_name'] = 'postfix';
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['encrypt'] = 'md5crypt';
$CONF['emailcheck_resolve_domain] = 'NO';
 
访问PostfixAdmin的设置页http://IP or domain/postfixadmin/setup.php
创建Setup password(如:123456)
点击“Generate password hash”获取一段加密代码
将该代码拷贝到配置文件config.inc.php中的以下配置中
$CONF['setup_password'] = 'change';(用加密代码替换change)
然后再次访问PostfixAdmin的设置页,创建管理员的账号和密码
(需要正确输入之前创建的Setup password才能创建管理员账号)
访问PostfixAdmin的登录页
http://IP or domain>/postfixadmin/
使用管理员账号登录后,就可以进行相应的管理工作。
 
3. 安装配置Postfix
Postfix的大部分RPM包都没有mysql的支持,还好我找到了带有mysql和sasl支持的RPM包。
软件包下载地址:
http://www.linuxmail.info/postfix-rpm-packages/
如果你无法下载的话,就只能自己编译安装了。
 
停止系统中默认的sendmail服务
# service sendmail stop
# chkconfig sendmail off
# mv /usr/bin/newaliases /usr/bin/newaliases.orig
# mv /usr/bin/mailq /usr/bin/mailq.orig
# mv /usr/sbin/sendmail /usr/sbin/sendmail.orig
 
安装Postfix
# rpm -ivh postfix-2.5.1-1.mysql.sasl2.vda.rhel5.i386.rpm
 
配置Postfix
# vim /etc/postfix/main.cf
#=====================BASE=========================
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 127.0.0.0/8
inet_interfaces = all
#=====================Vritual Mailbox settings=========================
virtual_minimum_uid = 201
virtual_mailbox_base = /var/spool/mail
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_alias_domains = $virtual_alias_maps
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:201    #postfix账号的uid
virtual_gid_maps = static:202    #postfix组的gid
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
#====================QUOTA========================
message_size_limit = 52428800
mailbox_size_limit = 209715200
virtual_mailbox_limit = 209715200
virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = mysql:/etc/postfix/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
#====================SASL========================
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions =  permit_mynetworks,
    permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = /var/spool/postfix/private/auth      #这里要和dovecot配置文件中的配置相对应
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP "Version not Available"
#=================================================
readme_directory = no
sample_directory = /etc/postfix
sendmail_path = /usr/sbin/sendmail
html_directory = no
setgid_group = postdrop
command_directory = /usr/sbin
manpage_directory = /usr/local/man
daemon_directory = /usr/libexec/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
queue_directory = /var/spool/postfix
mail_owner = postfix
 
Postfix的mysql配置文件
# vim /etc/postfix/mysql_virtual_alias_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address
 
# vim /etc/postfix/mysql_virtual_domains_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = domain
select_field = description
where_field = domain
 
# vim /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username
 
# vim /etc/postfix/mysql_virtual_mailbox_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
 
启动Postfix
# service postfix start
 
系统启动时自动启动Postfix服务
# chkconfig postfix on
 
刷新Postfix的配置文件
# postfix reload
 
查看Postfix的当前配置信息
# postconf -n
 
查看Postfix的默认配置信息
# postconf -d

4. 安装配置dovecot
如果系统中默认安装了dovecot,那么要先把它卸载或停用。
# service dovecot stop
# chkconfig dovecot off
 
编译安装
下载dovecot的源码包(
http://www.dovecot.org/download.html
# tar zxvf dovecot-1.2.8.tar.gz
# cd dovecot-1.0.rc14
# ./configure --prefix=/usr/local/dovecot --sysconfdir=/usr/local/etc --with-mysql --without-ssl --disable-ipv6
# make
# make install
# cp /usr/local/etc/dovecot-example.conf /usr/local/etc/dovecot.conf
 
建立启动进程所依赖的用户
# useradd -d /dev/null dovecot -s /bin/false
 
修改配置文件
# vim /usr/local/etc/dovecot.conf
base_dir = /var/run/dovecot
protocols = imap imaps pop3 pop3s
listen = *
disable_plaintext_auth = no
shutdown_clients = yes
log_path = /var/log/dovecot.log
ssl = no
mail_location = maildir:/var/spool/mail/%d/%n
auth default {
  mechanisms = plain login cram-md5
  passdb sql {
    args = /usr/local/etc/dovecot-mysql.conf
  }
 
  userdb sql {
    args = /usr/local/etc/dovecot-mysql.conf
  }
 
  socket listen {
    client {
        path = /var/spool/postfix/private/auth    #这里要和postfix配置文件的配置相对应
        mode = 0660
        user = postfix
        group = postfix
    }
  }
}
first_valid_uid = 201

dovecot的mysql配置文件
# vim dovecot-mysql.conf
driver = mysql
connect = host=/var/lib/mysql/mysql.sock dbname=postfix user=postfix password=postfix
default_pass_scheme = MD5
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, 201 AS uid, 202 AS gid FROM mailbox WHERE username = '%u'

启动dovecot
# /usr/local/dovecot/sbin/dovecot
 
可以把启动命令写进/etc/rc.d/rc.local文件,以使系统启动时能够自动启动dovecot服务。
# echo "/usr/local/dovecot/sbin/dovecot" >> /etc/rc.d/rc.local
 
停止dovecot
只能用kill命令来停止dovecot服务
# ps -aux | grep dovecot
# kill <"/usr/local/dovecot/sbin/dovecot"的pid>
 
验证
POP3
# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
+OK Dovecot ready.
 
IMAP
# telnet localhost 143
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
* OK Dovecot ready.
 
通过以上的配置,就可以实现基本的邮件收发功能。
 
※转载请注明出处