linux 下 搭建邮件邮件服务器(Postfix+Dovecot)(二)-基于mysql的虚拟账户登陆收发邮件
使用虚拟用户收发邮件安装
九、安装Courier authentication library
1、courier简介
courier-authlib 是Courier组件中的认证库,它是courier组件中一个独立的子项目,用于为courier的其它组件提供认证服务。其认证功能通常包括验证登录时的账号和密码、获取一个相关的家目录或邮件目录等信息、改变账号的密码。而其认证的实现方式也包括基于PAM通过/etc/passwd和/etc/shadow进行认证,基于GDBM或DB进行认证,基于LDAP/Mysql/PostgrepSQL 进行认证等。因此,courier-authlib也常常用来与courier之外的其他邮件组件(如postfix)整合为其提供认证服务。
备注:在RHEL5上要使用0.64.0及之前的版本,否则,可能会由于sqlite版本过低问题导致configure检查无法通过或编译无法进行。
2、安装
#接下来开始编译安装
在centos 6.5 x64 上 courier-authlib-0.65.0
# yum install expect
# wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz
# tar zxvf libtool-2.4.6.tar.gz
# cd libtool-2.4.6
# ./configure --prefix=/usr/local/libtool-2.4.6 --enable-ltdl-install
# make
# make install
# tar jxvf courier-authlib-0.65.0.tar.bz2
# cd courier-authlib-0.65.0
# ./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --without-authpam --without-authshadow --without-authvchkpw --without-authpgsql --with-authmysql --with-mysql-
libs=/usr/lib64/mysql --with-mysql-includes=/usr/include/mysql --with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-mailuser=postfix --with-
mailgroup=postfix --with-ltdl-lib=/usr/local/libtool-2.4.6/lib/ --with-ltdl-include=/usr/local/libtool-2.4.6/include/
#####
可以使用--with-authdaemonvar=/var/spool/authdaemon
# make
# make install
# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
# cp /etc/authdaemonrc.dist /etc/authdaemonrc
# cp /etc/authmysqlrc.dist /etc/authmysqlrc
#修改/etc/authdaemonrc 文件
# vim /etc/authdaemonrc
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemon=10
3、配置其通过mysql进行邮件账号认证
编辑/etc/authmysqlrc 为以下内容,其中2525,2525 为postfix 用户的UID和GID。
# vim /etc/authmysqlrc
MYSQL_SERVER localhost
MYSQL_PORT 3306 (指定你的mysql监听的端口,这你使用默认的3306)
MYSQL_USERNAME extmail (这时为后文的数据库的所有者的用户名)
MYSQL_PADDWORD extmail (密码)
MYSQL_SOCKET /var/lib/mysql/mysql.sock
MYSQL_DATABASE extmail
MYSQL_USER_TABLE mailbox
MYSQL_CRYPT_PWFIELD password
MYSQL_UID_FIELD 2525
MYSQL_GID_FIELD 2525
MYSQL_LOGIN_FIELD username
MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)
MYSQL_NAME_FIELD name
MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)
4、提供SysV服务脚本
# cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
# chmod 755 /etc/init.d/courier-authlib
# chkconfig --add courier-authlib
# chkconfig --level 2345 courier-authlib on
# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf
# ldconfig -v
# /etc/init.d/courier-authlib start
5、配置postfix和courier-authlib
新建虚拟用户邮箱所在目录,并将其权限赋予postfix用户:
# mkdir -pv /var/mailbox
# chown -R postfix /var/mailbox
接下来重新配置SMTP认证,编辑/usr/lib64/sasl2/smtpd.conf,确保以下内容:
# vim /usr/lib64/sasl2/smtpd.conf
pwcheck_method:authdaemond
log_level:3
mech_list:PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
# /etc/init.d/saslauthd restart
十、让postfix支持虚拟域和虚拟用户
1、编辑/etc/postfix/main.cf,添加如下内容:
####################CYRUS-SASL##############################
broken_sasl_auth_clients = yes
#smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,
# reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,
# reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
#smtpd_sasl_application_name = smtpd ##postfix2.3之后不再使用
smtpd_sasl_path = smtpd
smtpd_banner = Welcome to my mail.10fei3.top ESMTP,Warning: Version not Available!
#smtpd_banner = Welcome to my $myhostname ESMTP $mail_name ($mail_version)!
###############Virtual Mailbox Settings########################
virtual_mailbox_base = /var/mailbox
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 = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:2525
virtual_gid_maps = static:2525
virtual_transport = virtual
#maildrop_destination_recipient_limit = 1
#maildrop_destination_concurrency_limit = 1
###############QUOTA Setting#########################################
message_size_limit = 14336000
virtual_mailbox_limit = 20971520
#virtual_create_maildirsize = yes
#virtual_mailbox_extended = yes
#virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_limit_maps.cf
#virtual_mailbox_limit_override = yes
#virtual_maildir_limit_message = Sorry,the user's maildir has overdrawn his diskspace quota,please Tidy your mailbox and try again later.
2、使用extman 源码目录下docs目录中的extmail.sql和init.sql 建立数据库:
# tar zxvf extman-1.1.tar.gz
# cd extman-1.1/docs
# mysql -u root -p < extmail.sql
# mysql -u root -p < init.sql
# cp mysql* /etc/postfix/
3、授予用户extmail 访问extmail数据库的权限
mysql > GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
mysql > GRANT all privileges on extmail.* TO extmail@127.0.0.1 IDENTIFIED BY 'extmail';
说明:
1、启用虚拟域以后,需要取消中心域,即注释掉myhostname,mydestination,mydomain,myorigin几个指令;当然,你也可以把mydestination 的值修改为你自己需要的。
2、对于Mysql-5.1以后的版本,其中的服务脚本extmail.sql执行会有语法错误;可先使用如下命令修改修改extmail.sql配置文件,然后在执行。修改方法如下:
sed -i 's@TYPE=MyISA@ENGINE=InnoDB@g' extmail.sql
# cd extman-1.1/docs
# cp mysql_virtual_* /etc/postfix/
十一、配置Dovecot
# vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/var/mailbox/%d/%n/Maildir
# vim /etc/dovecot/conf.d/10-auth.conf
#!include auth-system.conf.ext
!include auth-sql.conf.ext
# vim /etc/dovecot/conf.d/auth-sql.conf.ext
passdb {
driver = sql
# Path for SQL configuration file, see example-config/dovecot-sql.conf.ext
#args = /etc/dovecot/dovecot-sql.conf.ext
args = /etc/dovecot/dovecot-mysql.conf
}
userdb {
driver = sql
#args = /etc/dovecot/dovecot-sql.conf.ext
args = /etc/dovecot/dovecot-mysql.conf
}
# vim /etc/dovecot/dovecot-mysql.conf
driver = mysql
connect = host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir,uidnumber AS uid,gidnumber AS gid FROM mailbox WHERE username = '%u'
使用dovecot 生成精简配置文件
# cd /etc/dovecot/
# doveconf -n > dovecot-new.conf
# cat dovecot-new.conf
# 2.0.9: /etc/dovecot/dovecot.conf
# OS: Linux 2.6.32-431.el6.x86_64 x86_64 CentOS release 6.5 (Final) ext4
login_greeting = Welcome to 10fe3.top Mail Pop3 Server.
login_trusted_networks = 192.168.151.0/24
mail_location = maildir:/var/mailbox/%d/%n/Maildir
mbox_write_locks = fcntl
passdb {
args = /etc/dovecot/dovecot-mysql.conf
driver = sql
}
protocols = pop3
ssl_cert = </etc/dovecot/ssl/dovecot.crt
ssl_key = </etc/dovecot/ssl/dovecot.key
userdb {
args = /etc/dovecot/dovecot-mysql.conf
driver = sql
}
# cp dovecot.conf dovecot.conf.bak
# cp dovecot-new.conf dovecot.conf
说明:如果mysql服务器是本地主机,即host=localhost时,如果mysql.sock 文件不是默认的/var/lib/mysql/mysql.sock,可以使用host=“sock文件的路径”来指定新的位置;例如,使用通用的而二进制格式安装的mysql,
相应的,connect = host=/tmp/mysql.sock dbname=extmail user=extmail password=extmail
一定要安装dovecot-mysql
# yum install dovecot-mysql -y
接下来启动dovecot服务:
# service dovecot start
# chkconfig dovecot on
十二、安装 Extmail-1.2
说明:如果extmail的放置路径做了修改,那么配置文件webmail.cf中的/var/www路径必须修改为你所需要的位置。本文使用了默认的/var/www,所以,以下实例中并没有包含路径修改的相关内容。
1、安装
# tar zxvf extmail-1.2.tar.gz
# mkdir -pv /var/www/extsuite
# mv extmail-1.2 /var/www/extsuite/extmail
# cp /var/www/extsuite/extmail/webmail.cf.default /var/www/extsuite/extmail/webmail.cf
2、修改主配置文件
# vim /var/www/extsuite/extmail/webmail.cf
部分修改选项的说明:
SYS_MESSAGE_SIZE_LIMIT 5242880
用户可以发送的最大邮件
SYS_USER_LANG = en_US
语言选项,可改作:
SYS_USER_LANG = zh_CN
SYS_MAILDIR_BASE = /home/domains
此处即为您在前文所设置的用户邮件的存放目录,可改作:
SYS_MAILDIR_BASE = /var/mailbox
SYS_MYSQL_USER = db_user
SYS_MYSQL_PASS = db_pass
以上两句用来设置连接数据库服务器所使用用户名、密码和邮件服务器用到的数据库,这里修改为:
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_HOST = localhost
指明数据库服务器主机名,这里默认即可
SYS_MYSQL_TABLE = mailbox
SYS_MYSQL_ATTR_USERNAME = username
SYS_MYSQL_ATTR_DOMAIN = domain
SYS_MYSQL_ATTR_PASSWD = password
以上用来指定验证用户登录里所用到的表,以及用户名、域名和用户密码分别对应的表中列的名称;这里默认即可
SYS_AUTHLIB_SOCKET = /var/spool/authdaemon/socket
此句用来指明authdaemon socket文件的位置,这里修改为:
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
3、apache相关配置
由于extmail要进行本地邮件的投递操作,故必须运行apache服务器用户的身份修改为您的邮件投递代理的用户;本例中打开了apache服务器的suexec 功能,故使用一下方法来实现虚拟主机运行身份的指定。此例中的MDA为postfix自带,因此,将指定为postfix用户:
# vim /etc/httpd/conf/httpd.conf
……
#DocumentRoot "/var/www/html"
……
<VirtualHost *:80>
ServerName mail.10fei3.top
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
SuexecUserGroup postfix postfix
</VirtualHost>
修改cgi执行文件属主为apache运行身份用户:
# chown -R postfix.postfix /var/www/extsuite/extmail/cgi
如果您没有打开apache服务器的suexec功能,也可以使用以下方式解决:
# vim /etc/httpd/httpd.conf
User postfix
Groupn postfix
<VirtualHost *:80>
ServerName mail.10fei3.top
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
</VirtualHost>
4、依赖关系的解决
extmail将会用到perl的Unix::syslogd功能,您可以去http://search.cpan.org搜索下载源码包进行安装。
# tar zxvf Unix-Syslog-1.1.tar.gz
# cd Unix-Syslog-1.1
# perl Makefile.PL
# make
# make install
5、启动apache服务
# service httpd start
# chkconfig httpd on
十三、安装Extman-1.1
1、安装及基本配置
# tar zxvf extman-1.1.tar.gz
# mv extman-1.1 /var/www/extsuite/extman
修改配置文件以符合本例的需要:
# cp /var/www/extsuite/extman/webman.cf.default /var/www/extsuite/extman/webman.cf
# vim /var/www/extsuite/extman/webman.cf
SYS_MAILDIR_BASE = /home/domains
此处即为您在前文所设置的用户邮件的存放目录,可改作:
SYS_MAILDIR_BASE = /var/mailbox
SYS_DEFAULT_UID = 1000
SYS_DEFAULT_GID = 1000
此两处后面设定的ID号需要更改为前面创建的postfix用户和postfix组的id号,本文使用的是2525,因此,上述两项需要修改为:
SYS_DEFAULT_UID = 2525
SYS_DEFAULT_GID = 2525
SYS_MYSQL_USER = webman
SYS_MYSQL_PASS = webman
mysql > GRANT all privileges on extmail.* TO webman@localhost IDENTIFIED BY 'webman';
mysql > GRANT all privileges on extmail.* TO webman@127.0.0.1 IDENTIFIED BY 'webman';
而后修改cgi目录的属主:
# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
在apache的主配置文件中Extmail的虚拟主机部分,添加如下两行:
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
Alias /extman /var/www/extsuite/extman/html
创建其运行时所需的临时目录,并修改其相应的权限:
#mkdir -pv /tmp/extman
#chown postfix.postfix /tmp/extman
修改 SYS_CAPTCHA_ON = 1 为 SYS_CAPTCHA_ON = 0
好了,到此为止,重新启动apache服务器后,您的Webmail和Extman已经可以使用了,可以在浏览器中输入指定的虚拟主机的名称进行访问,如下:
http://mail.10fei3.top选择管理即可登入extman进行后台管理了。默认管理账号为:root@extmail.org 密码为:extmail*123*
说明:
(1) 如果您安装后无法正常显示校验码,安装perl-GD模块会解决这个问题。如果想简单,您可以到以下地址下载适合您的平台的rpm包,安装即可:http://dires.ulyssis.org/rpm/packages/perl-GD/info.html
(2) extman-1.1自带了图形化显示日志的功能:此功能需要rrdtool的支持,您需要安装此些模块才可能正常显示图形日志。
十四、安装Extman-1.1的图形日志
配置Mailgraph_ext,使用Extman的图形日志:(下面所需的软件包需要自己下载)
接下来安装图形日志的运行所需要的软件包Time::HiRes、File::Tail和rrdtool,其中前两个包您可以去http://search.cpan.org 搜索并下载获得,后一个包您可以到http://oss.oetiker.ch/rrdtool/pub/?M=D下载获得:
注意安装顺序不能更换。
# tar zxvf rrdtool-1.4.7.tar.gz
# cd rrdtool-1.4.7
# ./configure --prefix=/usr/local/rrdtool
# make
# make install
# ln -svf /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/auto/RRDs/RRDs.so /usr/lib64/perl5/5.10.0/x86_64-linux-thread-multi
# ln -svf /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/RRDs.pm /usr/lib64/perl5/5.10.0/x86_64-linux-thread-multi
# ln -svf /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/auto/RRDs/RRDs.so /var/www/extsuite/extman/libs
# yum localinstall rrdtool-perl-1.3.8-7.el6.x86_64.rpm
测试是否ok
# perl -MRRDs -le 'print q(ok!)'
ok!
安装Time::HiRes
# tar zxvf Time-HiRes-1.9735.tar.gz
# cd Time-HiRes-1.9735
#perl Makefile.PL
# make
# make test
# make install
安装File::Tail
# tar zxvf File-Tail-1.3.tar.gz
# cd File-Tail-1.3
# perl Makefile.PL
# make
# make install
# cp -r /var/www/extsuite/extman/addon/mailgraph_ext /usr/local
# /usr/local/mailgraph_ext/mailgraph-init start
Starting mail statistics grapher: mailgraph_ext
Starting queue statistics grapher: qmonitor
# /var/www/extsuite/extman/daemon/cmdserver -v -d
loaded ok
加入开机自启动
# echo "/usr/local/mailgraph_ext/mailgraph-init start" >> /etc/rc.d/rc.local
# echo "/var/www/extsuite/extman/daemon/cmdserver -v -d" >> /etc/rc.d/rc.local
使用方法: 等待大约15分钟左右,如果邮件系统有一定的流量,即可登陆到extman里,点“图形日志”即可看到图形化的日志。具体每天,周,月,年的则点击相应的图片进入即可。
十五、问题总结
到此基于mysql的虚拟账户邮箱,可收发邮件已经搭建完成。后期准备防垃圾、防病毒等优化工作。
问题1:
# perl -MRRDs -le 'print q(ok!)'
Can't locate RRDs.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.
解决:
# yum localinstall rrdtool-perl-1.3.8-7.el6.x86_64.rpm
问题2:
Jul 1 17:47:05 localhost dovecot: auth: Fatal: Unknown database driver 'mysql'
解决:
# yum install dovecot-mysql -y