现在的位置: 首页 > 综合 > 正文

史上最详细的centos下 postfix + extmail + dovecot + maildrop 安装笔记2014更新

2018年03月21日 ⁄ 综合 ⁄ 共 9182字 ⁄ 字号 评论关闭

本文最初是2008年发表的,最近几天照此笔记又安装了一遍系统,这里更新一下记录。
作者:wangdy

本文发表于  http://blog.csdn.net/aryang/article/details/40593283  转载请注明!

1. 概述
安装环境是CentOS6.5,拿到服务器的时候只有根目录的分区。邮件准备存储到 /var/mailbox下,后面内容会出现多次,如果要修改请注意不要有遗漏的地方。
/var/spool/postfix是postfix存储队列的地方, /var/vmail是用来存储邮件的。
有条件的建议对 /var/spool/postfix和 /var/vmail进行了单独分区。

CentOS提供了很方便的yum在线安装,我的基本原则是非重要、对版本不敏感的模块尽量用yum安装,邮件系统主要模块则用源码编译安装。
首先用yum安装的模块有:mysql mysql-server mysql-devel db4-devel openssl gcc libxml2 libxml2-develgcc-c++
几个模块都需要用到PCRE。

先说明一下几个模块的搭配。

MTA用的是postfix-2.5.17。

web模块是最先需要确定的,因为它决定了邮箱系统最核心的数据结构,这里选用国产的extmail,其他所有模块都按照extmail的结构进行配置修改。
webmail使用 extmail-1.0.5,web管理模块用extman-0.2.5。

extmail对maildrop的自动回复、转发等提供了很好的支持,所以MDA采用最流行的maildrop(2.5.4)

这里面简单说一下为什么要用maildrop。

对虚拟用户投递的支持,postfix自带了一个投递代理(MDA):virtual
virtual投递代理在 master.cf里描述为:

virtual  unix -      n       n      -       -      virtual

如果我们在main.cf里设置virtual_transport = virtual,就表示对于包含在 virtual_mailbox_domains域名里的邮件,都使用virtual进行投递。
postfix自带的virtual只提供了最基本的QUOTA功能,如果想使用更复杂的功能,如自动转发,自动回复,邮件自定义过滤等,就需要换成更高级的MDA。
当然可选择的MDA有很多种,比如procmail, maildrop等。我这里选用maildrop。

用户信息存储方式也有很多可选的,比如mysql,ldap等。这里选mysql,原因是各个模块,包括extmail等对这个支持最好最方便最简单。

maildrop在投递邮件的时候,需要确定一些数据,比如收件用户的Maildir的路径,保存邮件用的uid,gid,邮箱容量等,这些数据需要 courier-authlib来提供。

整个系统模块间的作用和关系是:

postfix作为MTA(Mail Transfer Agent), 负责创建smtp服务(smtpd)接收本域用户或其他域名服务器投递来的邮件,负责向其他服务器投递(转发)邮件,管理邮件队列;
maildrop作为MDA(Mail Delivery Agent)负责把从postfix接收到邮件存入邮件夹,同时还支持自动转发、自动回复,邮件过滤等功能;
courier-authlib 为 maildrop 提供与用户相关的信息查询;
dovecot提供系统的POP3和IMAP服务,同时给postfix提供SMTP的SASL认证服务。

2. 系统基础配置

如果系统已经安装postfix请先卸载!

创建邮箱存储目录 /var/mailbox ,子目录结构为 ./[domain]/[username]
创建负责邮件存取的用户和组帐户 vmail,注意要指定uid和gid,这两个id在后面很多配置里要用到!
#groupadd -g 1001 vmail
#useradd -g vmail -u 1001 -s /sbin/nologin

#chown vmail:vmail /var/mailbox
#chmod 700 /var/mailbox

postfix安装的是需要指定几个专门的用户和组
#groupadd -g 2001 postfix
#useradd -g postfix -u 1001 -s /sbin/nologin -M postfix
#groupadd-g 2002 postdrop
#useradd-g postdrop -u 2002 -s /bin/false -M postdrop

3. courier-authlib安装

下载 courier-authlib-0.66.1

 ./configure --sysconfdir=/etc --without-authpam --without-authldap --without-authpwd --without-authshadow --without-authvchkpw --without-authpgsql --with-authmysql --with-mysql-libs=/usr/local/mysql/lib/mysql --with-mysql-includes=/usr/local/mysql/include/mysql
--with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-mailuser=1001 --with-mailgroup=1001

注意此处已经用到了vmail用户的uid和gid!

#cp /etc/authdaemonrc.dist /etc/authdaemonrc

修改配置文件,启用mysql用户认证方式:

#authmodulelist="authuserdb authmysql authcustom authpipe"
authmodulelist="authmysql"

#authmodulelistorig="authuserdb authmysql authcustom authpipe"
authmodulelistorig="authmysql"

authmysql的配置文件:
#cp /etc/authmysqlrc.dist /etc/authmysqlrc

修改相关配置,其中

MYSQL_USER_TABLE        mailbox
MYSQL_CRYPT_PWFIELD     crypt

MYSQL_SELECT_CLAUSE     SELECT username as id,password as crypt,"" as clear,\
                        '1001' as uid,'1001' as gid,   \
                        CONCAT('/var/mailbox/',homedir) as home,     \
                        CONCAT('/var/mailbox/',maildir) as maildir,  \
                        quota,                                       \
                        name                                         \
                        FROM mailbox                                 \
                        WHERE  active = 1 and username = '$(local_part)@$(domain)'

其实就是配置了数据源是什么(mysql),怎么连接,执行什么sql语句查询用户信息,数据的字段名是什么等。

启动auth服务 
# /usr/local/libexec/courier-authlib/authdaemond &

4. Dovecot安装
 
 下载dovecot-2.0.21.tar.gz
#./configure--prefix=/usr/local/dovecot --sysconfdir=/etc --with-sql --with-mysql
#make
#make install

需要ssl支持可以加上参数--with-ssl=openssl

拷贝 doc/example-config/下的 dovecot.conf 至 /etc/dovecot

配置dovecot.conf:
base_dir=/var/run/dovecot
#支持的用户协议
protocols=imap imaps pop3 pop3s
listen=*
disable_plaintext_auth = no
#是否启用ssl
ssl_disable = yes
#邮件存储位置,%d表示域名,%n表示用户名
mail_location= maildir:/var/vmail/%d/%n/Maildir
pop3_uidl_format=%08Xu%08Xv
authdefault {
...
mechanisms= plain login digest-md5 cram-md5
        
#验证密码的配置文件
passdb sql { 
         args = /etc/dovecot/dovecot-sql.conf
}
...
#查询用户信息的配置文件
userdbsql {
         args = /etc/dovecot/dovecot-sql.conf
}
...
socketlisten {
   ...
   client {
     path = /var/spool/postfix/private/auth
     mode = 0660
     user = postfix
     group = postfix
   }
}
}
 
编辑/etc/dovecot/dovecot-sql.conf:

driver =mysql
connect =host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme=MD5-CRYPT
password_query= SELECT password, maildir as userdb_home FROM mailbox WHERE username = '%u'
user_query= select 1001 as uid ,1001 as gid from mailbox where username='%u' And active='1'

注意文件里的用户名口令。
password_query是pop3 or imap登录的时候执行的,传用户名查密码,邮箱目录
user_query是查询用户存取邮件用的uid和gid的,这里写死了。。

创建符号链接
#ln -s/usr/local/dovecot/sbin/dovecot /usr/bin/dovecot

启动dovecot服务
# dovecot

如果出错,看看 /var/log/dovecot

5. 安装 maildrop
下载 maildrop-2.5.4.tar.gz

./configure--enable-sendmail=/usr/sbin/sendmail --enable-trusted-users='root vmail'--enable-syslog=1 --enable-maildirquota --enable-maildrop-uid=1001--enable-maildrop-uid=1001 --with-trashquota --with-dirsync
 
make& make install
 
/etc/postfix/master.cf里增加
 
maildrop  unix -       n       n      -       -       pipe
  flags=DRhu user=vmailargv=/usr/local/bin/maildrop -d ${recipient}

6. 安装 postfix

下载postfix-2.5.17.tar.gz
 
编译
# make CCARGS='-DUSE_SASL_AUTH -DDEF_SERVER_SASL_TYPE=\"dovecot\"\
-DHAS_PCRE-DHAS_MYSQL \
-I/usr/include/-I/usr/local/include -I/usr/include/mysql/' \
AUXLIBS='-L/usr/local/lib-L/usr/lib/mysql -lmysqlclient -lpcre'
# make
# make install

-DUSE_SASL_AUTH -DDEF_SERVER_SASL_TYPE=\"dovecot\" 
表示启用SASL身份认证,并默认使用dovecot。
mysql的几个路径可以根据实际情况做修改,我这里的目录都是yum默认安装的。
 
注意make参数里加上正确的mysql -I  (头文件所在目录) -L(动态库所在目录)
make install的提问全部回车默认。
(install的时候如果默认参数里没有值,请根据错误提示认真检查)
 
导入extmail的mysql表结构和数据。
下载extman-0.2.5.tar.gz
解开后进入 docs目录

建库建表:
# mysql-u root -p < extmail.sql
注意extmail.sql里配置的数据库,用户名,口令都是extmail,可以编辑脚本修改。
extmail.sql里创建的数据库用户 webman 是用在web后台管理的,extmail用在所有其他地方,包括postfix邮件收发,dovecot帐户验证、邮件收发,webmail里等。
 
导入初始数据:
#mysql-u root -p < init.sql
(可以先吧 init.sql文件里的extmail.org改成你自己的域名,或登录web管理后再添加域名)
把目录下 mysql_*.cf全部拷贝至/etc/postfix

# cpmysql_*.cf /etc/postfix/
修改cf文件里的用户名口令

继续配置 postfix
# mv/etc/aliases /etc/aliases.old
# ln -s/etc/postfix/aliases /etc/aliases
# /usr/bin/newaliases

编辑  /etc/postfix/main.cf
 
#邮件服务器域名
myhostname= mail.abc.com
#邮箱域名
mydomain= abc.com
myorigin= localhost
mydestination= $myhostname localhost localhost.$mydomain
#可以信赖的网络,后面的permit_mynetworks 说的就是这
mynetworks= 127.0.0.0/8
inet_interfaces= all
home_mailbox=  mbox
smtpd_banner= Welcome to ESMTP Server
#设置可以任意从本机转发邮件的域名,黑莓什么的可能用到
#relay_domains=
alias_maps=

sendmail_path= /usr/sbin/sendmail
newaliases_path= /usr/bin/newaliases
mailq_path= /usr/bin/mailq
setgid_group= postdrop
html_directory= no
manpage_directory= /usr/local/man
sample_directory= /etc/postfix

unknown_local_recipient_reject_code= 450
non_fqdn_reject_code= 450
unknown_virtual_alias_reject_code= 450
unknown_virtual_mailbox_reject_code= 450
maps_rbl_reject_code= 450

maximal_queue_lifetime= 1
bounce_queue_lifetime= 0
 
#=====================VritualMailbox settings=========================
#查询域名的配置
virtual_mailbox_domains= mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_alias_domains=
virtual_alias_maps= mysql:/etc/postfix/mysql_virtual_alias_maps.cf
#用maildrop投递邮件!后面会安装
virtual_transport= maildrop

maildrop_destination_recipient_limit=1

#====================QUOTA========================
#设置邮件大小限制和邮箱最大容量
message_size_limit= 500001000 
mailbox_size_limit= 500001000
virtual_mailbox_limit= 209715200
 
#====================SASLESMTP Authenticat=================
#注: smtpd说的是接收邮件客户端发送邮件的服务, smtp说的是给别的邮件服务器投递用的服务
#这个得加yes,不然你就为互联网贡献一个垃圾邮件服务器
smtpd_sasl_auth_enable= yes
#这里用到了dovecot ^_^
smtpd_sasl_type= dovecot
smtpd_sasl_path= private/auth
broken_sasl_auth_clients= yes
smtpd_sasl_local_domain=
smtpd_sasl_security_options= noanonymous
 
#注意第二行起有空格
#这个说的是邮件客户端(包括本域用户客户端和别的服务器投递进来的)提交收件人的时候做的检查
# permit_sasl_authenticated 表示 smtp 鉴权通过的permit,其他几种情况全拒!
smtpd_recipient_restrictions=
 permit_sasl_authenticated,
 permit_mynetworks,
 reject_invalid_hostname,
 reject_non_fqdn_hostname,
 reject_non_fqdn_sender,
 reject_non_fqdn_recipient,
 reject_unknown_sender_domain,
 reject_unknown_recipient_domain,
 reject_unauth_pipelining,
 reject_unauth_destination
#check_recipient_access pcre:/etc/postfix/spamd_filter_access

#reject_rbl_client sbl.spamhaus.org,
#reject_rbl_client cblless.anti-spam.org.cn,

#检查发信人
smtpd_sender_restrictions=
 permit_sasl_authenticated,
 permit_mynetworks,
 reject_non_fqdn_sender,
 reject_unknown_sender_domain,
 reject_sender_login_mismatch,
 reject_authenticated_sender_login_mismatch,
 reject_unauthenticated_sender_login_mismatch

smtpd_helo_required= yes
smtpd_helo_restrictions=
 permit_sasl_authenticated,
 permit_mynetworks,
 reject_invalid_hostname

smtpd_sender_login_maps=
 mysql:/etc/postfix/mysql_virtual_sender_maps.cf,
 mysql:/etc/postfix/mysql_virtual_alias_maps.cf

disable_vrfy_command= yes

smtpd_client_restrictions=
 permit_mynetworks,
 check_client_access hash:/etc/postfix/access

#=====================TLS=========================
#smtp的tls支持,此处不详述
smtpd_use_tls= yes
smtpd_tls_security_level= may
smtpd_tls_key_file= /etc/postfix/certs/mail.key
smtpd_tls_cert_file= /etc/postfix/certs/mail.crt
smtpd_tls_loglevel= 1
smtpd_tls_session_cache_timeout= 3600s
smtpd_tls_session_cache_database= btree:/var/spool/postfix/smtpd_tls_cache
tls_random_source= dev:/dev/urandom

readme_directory= no
command_directory= /usr/sbin
daemon_directory= /usr/libexec/postfix
queue_directory= /var/spool/postfix
mail_owner= postfix
data_directory= /var/lib/postfix

启动postfix

#postfix

启动后注意观察 /etc/log/maillog 里是否有错误信息
 
7. 安装extmail和extman!

extman是邮箱的后台管理web,注意webman.cf里的配置:

邮箱的根目录:
SYS_MAILDIR_BASE= /var/mailbox 

SYS_DEFAULT_UID= 1001
SYS_DEFAULT_GID= 1001

SYS_MYSQL_USER= webman
SYS_MYSQL_PASS= webman 
 
extmail里的 webmail.cf
 
SYS_MAILDIR_BASE= /var/mailbox
 
祝您成功!

抱歉!评论已关闭.