1.环境介绍
MTA: Postfix 3.1.4
SASL: Cyrus-sasl 2.1.26 ; Courier-authlib 0.66.1(Cyrus-sasl使用Courier-authlib与MySQL建立关联)
MDA: Dovecot 2.2.10
DataBase: 5.5.52-MariaDB MariaDB Server
WebMail: ExtMail 1.2(普通用户界面); ExtMan 1.1(管理员界面)
Web服务器(用于发布WebMail程序): Apache 2.4.6
服务器操作系统版本:Centos7
2.安装前的一些准备工作
Courier-authlib : http://www.courier-mta.org/download.html , 我这里下载的是最新的 courier-0.76.4.tar.bz2 。
postfix : ftp://ftp.cuhk.edu.hk/pub/packages/mail-server/postfix/index.html , 我这里下载的是最新版本的
2.1 安装开发环境
yum groups install "Compatibility Libraries" "Development Tools" "Development and Creative Workstation" -y # 安装必要的开发环境
2.2 安装安装所需的rpm包
yum install httpd, mariadb-server,mariadb, mariadb-devel, openssl-devel, dovecot, perl-DBD-MySQL, tcl, tcl-devel, libart_lgpl, libart_lgpl-devel, libtool-ltdl, libtool-ltdl-devel, expect -y
2.3卸载系统默认的postfix
yum remove postfix #卸载postfix
3配置dns服务器
3.1 安装bind
yum install bind bind-libs bind-devel -y
3.2 编辑/etc/named.conf
注释一下几行 // listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; }; // allow-query { localhost; };
3.3 编辑 /etc/named.rfc1912.zones
最后添加如下几行 zone "linuxpanda.com" IN { type master; file "linuxpanda.com.zone"; }; zone "137.168.192.in-addr.arpa" IN { type master; file "192.168.137.zone"; };
3.4 新增区域文件
vim /var/named/192.168.137.zone 内容为 $TTL 1D @ IN SOA ns.linuxpanda.com. admin.linuxpanda.com. ( 20170201 ; serial 2H ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS ns.linuxpanda.com. 200 IN PTR ns.linuxpanda.com. 200 IN PTR mail.linuxpanda.com. 200 IN PTR www.linuxpanda.com. 200 IN PTR win.linuxpanda.com.
vim /var/named/linuxpanda.com.zone 内容为 $TTL 1D @ IN SOA ns.linuxpanda.com. admin.linuxpanda.com. ( 20170201 ; serial 2H ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS ns IN MX 10 mail ns IN A 192.168.137.101 mail IN A 192.168.137.101 www IN A 192.168.137.101
3.5 修改文件权限
#chown root.named 192.196.137.zone #修改文件属主和属组 #chown root.named linuxpanda.com.zone #修改文件属主和属组 #chmod 640 192.196.137.zone #修改文件权限 #chmod 640 linuxpanda.com.zone #修改文件权限
3.6启动named服务并测试
# netstat -tunlp #查看端口信息 # systemctl restart named #重新启动named服务 # systemctl enable named #加入chkconfg中去
[root@mail named]# dig -t A mail.linuxpanda.com @192.168.137.101 #测试正向 ; <<>> DiG 9.9.4-RedHat-9.9.4-37.el7 <<>> -t A mail.linuxpanda.com @192.168.137.101 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65454 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;mail.linuxpanda.com. IN A ;; ANSWER SECTION: mail.linuxpanda.com. 86400 IN A 192.168.137.101 ;; AUTHORITY SECTION: linuxpanda.com. 86400 IN NS ns.linuxpanda.com. ;; ADDITIONAL SECTION: ns.linuxpanda.com. 86400 IN A 192.168.137.101 ;; Query time: 1 msec ;; SERVER: 192.168.137.101#53(192.168.137.101) ;; WHEN: Fri Feb 10 07:50:27 CST 2017 ;; MSG SIZE rcvd: 97
[root@mail named]# dig -t A mail.linuxpanda.com #测试反向 ; <<>> DiG 9.9.4-RedHat-9.9.4-37.el7 <<>> -t A mail.linuxpanda.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40123 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;mail.linuxpanda.com. IN A ;; ANSWER SECTION: mail.linuxpanda.com. 86400 IN A 192.168.137.101 ;; AUTHORITY SECTION: linuxpanda.com. 86400 IN NS ns.linuxpanda.com. ;; ADDITIONAL SECTION: ns.linuxpanda.com. 86400 IN A 192.168.137.101 ;; Query time: 0 msec ;; SERVER: 192.168.137.101#53(192.168.137.101) ;; WHEN: Fri Feb 10 07:50:58 CST 2017 ;; MSG SIZE rcvd: 97
4.编译安装postfix
4.1 删除postfix的文件和用户
# find / -user postfix #找到的文件都删除了
# userdel postfix
4.2创建postfix、postdrop用户组和用户
# groupadd -g 2525 postfix # useradd -g postfix -u 2525 -s /sbin/nologin -M postfix # groupadd -g 2526 postdrop # useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop
4.2开始编译安装
# tar zxvf postfix-3.1.4.tar.gz # cd postfix-3.1.4 # make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto' # make # make install
4.3 postfix的一些基础配置设置。一路回车就可以了。
install_root: [/] tempdir: [/root/Downloads/postfix-3.1.4] config_directory: [/etc/postfix] command_directory: [/usr/sbin] daemon_directory: [/usr/libexec/postfix] data_directory: [/var/lib/postfix] html_directory: [no] mail_owner: [postfix] mailq_path: [/usr/bin/mailq] manpage_directory: [/usr/local/man] newaliases_path: [/usr/bin/newaliases] queue_directory: [/var/spool/postfix] readme_directory: [no] sendmail_path: [/usr/sbin/sendmail] setgid_group: [postdrop] shlib_directory: [no] meta_directory: [/etc/postfix]
注意: 这些路径都是要确认本地存在的。可以使用rpm -ql 命令获取rpm包的信息。
4.4 生成别名二进制文件
# newalias
4.5 修改post主配置文件
# vim /etc/postfix/main.cf 修改以下几项为您需要的配置 myhostname = mail.linuxpanda.com myorigin = linuxpanda.com mydomain = linuxpanda.com mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mynetworks = 192.168.137.0/24, 127.0.0.0/8
4.6 为postfix提供sysv服务脚本/etc/rc.d/init.d/postfix
# vim /etc/rc.d/init.d/postfix #!/bin/bash # # postfix Postfix Mail Transfer Agent # # chkconfig: 2345 80 30 # description: Postfix is a Mail Transport Agent, which is the program \ # that moves mail from one machine to another. # processname: master # pidfile: /var/spool/postfix/pid/master.pid # config: /etc/postfix/main.cf # config: /etc/postfix/master.cf # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ $NETWORKING = "no" ] && exit 3 [ -x /usr/sbin/postfix ] || exit 4 [ -d /etc/postfix ] || exit 5 [ -d /var/spool/postfix ] || exit 6 RETVAL=0 prog="postfix" start() { # Start daemons. echo -n $"Starting postfix: " /usr/bin/newaliases >/dev/null 2>&1 /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start" RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix echo return $RETVAL } stop() { # Stop daemons. echo -n $"Shutting down postfix: " /usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop" RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix echo return $RETVAL } reload() { echo -n $"Reloading postfix: " /usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload" RETVAL=$? echo return $RETVAL } abort() { /usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort" return $? } flush() { /usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush" return $? } check() { /usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check" return $? } restart() { stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; reload) reload ;; abort) abort ;; flush) flush ;; check) check ;; status) status master ;; condrestart) [ -f /var/lock/subsys/postfix ] && restart || : ;; *) echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}" exit 1 esac exit $? # END
4.7 给脚本授权并加入到服务列表中去
# chmod +x /etc/rc.d/init.d/postfix # chkconfig --add postfix # chkconfig postfix on # service postfix restart
注意: 如果服务无法启动,请使用postconf检查postfix的配置错误信息。
5.测试postfix
5.1 使用telnet 命令发邮件
[root@mail Downloads]# telnet mail.linuxpanda.com 25 Trying 192.168.137.101... Connected to mail.linuxpanda.com. Escape character is '^]'. 220 mail.linuxpanda.com ESMTP Postfix HELO mail.linuxpanda.com 250 mail.linuxpanda.com mail from:root 250 2.1.0 Ok rcpt to:oracle 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> hello oracle ,i'm root . . 250 2.0.0 Ok: queued as 1C5D3399AAF quit 221 2.0.0 Bye Connection closed by foreign host.
5.2查看日志
# tail /var/log/maillog Feb 10 21:57:31 mail postfix/trivial-rewrite[35445]: warning: smtputf8_enable is true, but EAI support is not compiled in Feb 10 21:59:08 mail postfix/cleanup[35574]: warning: smtputf8_enable is true, but EAI support is not compiled in Feb 10 21:59:08 mail postfix/smtpd[35443]: 1C5D3399AAF: client=mail.linuxpanda.com[192.168.137.101] Feb 10 21:59:40 mail postfix/cleanup[35574]: 1C5D3399AAF: message-id=<20170210135908.1C5D3399AAF@mail.linuxpanda.com> Feb 10 21:59:40 mail postfix/qmgr[34499]: 1C5D3399AAF: from=<root@linuxpanda.com>, size=352, nrcpt=1 (queue active) Feb 10 21:59:40 mail postfix/local[35583]: warning: smtputf8_enable is true, but EAI support is not compiled in Feb 10 21:59:40 mail postfix/local[35583]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled Feb 10 21:59:40 mail postfix/local[35583]: 1C5D3399AAF: to=<oracle@linuxpanda.com>, orig_to=<oracle>, relay=local, delay=129, delays=129/0.05/0/0, dsn=2.0.0, status=sent (delivered to mailbox) Feb 10 21:59:40 mail postfix/qmgr[34499]: 1C5D3399AAF: removed Feb 10 21:59:43 mail postfix/smtpd[35443]: disconnect from mail.linuxpanda.com[192.168.137.101] helo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
5.3登录到oracle用户可以查收邮件的。
su - oracle [oracle@mail ~]$ mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/oracle": 1 message 1 new >N 1 root@linuxpanda.com Fri Feb 10 21:59 13/489 & 1 Message 1: From root@linuxpanda.com Fri Feb 10 21:59:40 2017 Return-Path: <root@linuxpanda.com> X-Original-To: oracle Delivered-To: oracle@linuxpanda.com Date: Fri, 10 Feb 2017 21:57:31 +0800 (CST) From: root@linuxpanda.com Status: R hello oracle ,i'm root .
6.为postfix服务开启用户别名支持
6.1在配置文件开启基于hash的别名文件支持
在main.cf中,找到如下指令,而后启用它(即移除前面的#号):
#alias_maps = hash:/etc/aliases
6.2 添加别名
编辑/etc/alias添加zhaojiedi: zhaojiedi1992行。格式说明,前一个字段为初始目标邮件地址,后一个字段为实际发往的地址。
6.3、将/etc/aliases转换为hash格式:
# postalias /etc/aliases
6.4 重新启动postfix
# service postfix restart
6.5 测试发送给zhaojiedi 是否转发到zhaojiedi1992邮箱中去。(这里测试跳过)。
7.为postfix开启基于cyrus-sasl的认证功能
7.1检查postfix是否支持了sasl功能
#postconf -a cyrus dovecot
7.2 编辑 /etc/postfix/main.cf 加入smtp认证设置信息
#vim /etc/postfix/main.cf 添加以下内容: ############################CYRUS-SASL############################ broken_sasl_auth_clients = yes 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_path = smtpd smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!
7.3 编辑 /usr/lib/sasl2/smtpd.conf
vim /usr/lib64/sasl2/smtpd.conf 添加如下内容: pwcheck_method: saslauthd mech_list: PLAIN LOGIN
7.4 重新加载postfix
#/usr/sbin/postfix reload
7.5 测试sasl配置正确性
!tel telnet mail.linuxpanda.com 25 Trying 192.168.137.101... Connected to mail.linuxpanda.com. Escape character is '^]'. 220 Welcome to our mail.linuxpanda.com ESMTP,Warning: Version not Available! helo mail.linuxpanda.com 250 mail.linuxpanda.com ehlo mail.linuxpanda.com 250-mail.linuxpanda.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
注意: 确保你的信息有250-AUTH=PLAIN LOGIN和250-ENHANCEDSTATUSCODES 这两行
8 安装Courier authentication library
8.1 安装libtool
# wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz # tar zxvf libtool-2.4.6.tar.gz # ./configure --prefix=/usr/local/libtool --enable-ltdl-install # make # make install
8.2 安装courier-unicode
下载 courier-unicode #wget https://sourceforge.net/projects/courier/files/latest/download?source=files # tar zxvf libtool-2.4.6.tar.gz # ./configure # make # make install
8.3开始安装courier-authlib
# tar jxvf ourier-authlib-0.67.0.tar.bz2 # cd courier-authlib-0.64.0 #./configure \ --prefix=/usr/local/courier-authlib \ --sysconfdir=/etc \ --without-authpam \ --without-authshadow \ --without-authvchkpw \ --without-authpgsql \ --with-authmysql \ --with-mysql-libs=/usr/lib/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/lib \ --with-ltdl-include=/usr/local/libtool/include \ --with-authdaemonvar=/var/spool/authdaemon # make # make install
8.4 创建和复制和修改文件
# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon # cp /etc/authdaemonrc.dist /etc/authdaemonrc # cp /etc/authmysqlrc.dist /etc/authmysqlrc 修改/etc/authdaemonrc 文件 authmodulelist="authmysql" authmodulelistorig="authmysql" daemons=12
8.5 配置其通过mysql进行邮件帐号认证
vim /etc/authmysqlrc 为以下内容,其中2525,2525 为postfix 用户的UID和GID。 MYSQL_SERVER localhost MYSQL_PORT 3306 (指定你的mysql监听的端口,这里使用默认的3306) MYSQL_USERNAME extmail (这时为后文要用的数据库的所有者的用户名) MYSQL_PASSWORD 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)
8.6提供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 # service courier-authlib start #启动服务
8.7创建/var/mailbox目录
#mkdir –pv /var/mailbox #chown –R postfix /var/mailbox
8.8编辑 /usr/lib/sasl2/smtpd.conf
内容有如下几项 pwcheck_method: authdaemond log_level: 3 mech_list:PLAIN LOGIN authdaemond_path:/var/spool/authdaemon/socket
9 让postfix支持虚拟域和虚拟用户
9.1编辑/etc/postfix/main.cf
添加如下内容 ########################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
注意,启用了虚拟域,需要将需要取消中心域,即注释掉myhostname, mydestination, mydomain, myorigin几行。
9.2使用extman源码目录下docs目录中的extmail.sql和init.sql建立数据库:
# tar zxvf extman-1.1.tar.gz # cd extman-1.1/docs # sed -i 's@TYPE=MyISAM@ENGINE=InnoDB@g' extmail.sql # mysql -u root -p < extmail.sql # mysql -u root -p <init.sql # cp mysql* /etc/postfix/
9.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';
10.配置dovecot
10.1编辑配置文件
# vi /etc/dovecot/conf.d/10-mail.conf #设置email位置 mail_location = maildir:/var/mailbox/%d/%n/Maildir # vim /etc/dovecot/conf.d/10-auth.conf #选择认证方式(mysql认证) 注释掉#!include auth-system.conf.ext行,去掉#!include auth-sql.conf.ext行的注释
#vim /etc/dovecot/dovecot-sql.conf.ext #配置认证sql语句
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'
10.2 重启dovecot
# service dovecot start # chkconfig dovecot on
11.安装配置extmail
11.1安装
# tar zxvf extmail-1.1.1.tar.gz # mkdir -pv /var/www/extsuite # mv extmail-1.1.1 /var/www/extsuite/extmail # cp /var/www/extsuite/extmail/webmail.cf.default /var/www/extsuite/extmail/webmail.cf
11.2配置extmail
#vi /var/www/extsuite/extmail/webmail.cf SYS_MESSAGE_SIZE_LIMIT = 5242880 SYS_MAILDIR_BASE = /var/mailbox SYS_MYSQL_USER = extmail SYS_MYSQL_PASS = extmail 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 SYS_AUTHLIB_SOCKET =/var/spool/authdaemon/socket
12.apach 配置
12.1编辑/etc/httpd/config/httpd.conf文件
添加如下内容
<VirtualHost *:80> ServerName mail.magedu.com DocumentRoot /var/www/extsuite/extmail/html/ ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi Alias /extmail /var/www/extsuite/extmail/html </VirtualHost>
注释DocumentRoot 行
修改
User postfix
Group postfix
12.2依赖关系的解决
# tar zxvf Unix-Syslog-0.100.tar.gz # cd Unix-Syslog-0.100 # perl Makefile.PL # makelinli # make install
12.3启动apach服务
# service httpd start # chkconfig httpd on
13安装Extman-1.1
13.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 # vi /var/www/extsuite/extman/webman.cf 修改内容为如下 SYS_MAILDIR_BASE = /var/mailbox SYS_DEFAULT_UID = 2525 SYS_DEFAULT_GID = 2525 SYS_MYSQL_USER = extmail SYS_MYSQL_PASS = extmail
SYS_CAPTCHA_ON = 0 而后修改cgi目录的属主: # chown -R postfix.postfix /var/www/extsuite/extman/cgi/
13.2在apache的主配置文件中Extmail的虚拟主机部分,添加如下两行:
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi Alias /extman /var/www/extsuite/extman/html
修改后虚拟主机部分的内容为
<VirtualHost *:80>
ServerName mail.linuxpanda.com
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
#SuexecUserGroup postfix postfix
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
Alias /extman /var/www/extsuite/extman/html
</VirtualHost>
13.3 创建运行目录
创建其运行时所需的临时目录,并修改其相应的权限: #mkdir -pv /var/www/tmp/extman #chown postfix.postfix /var/www/tmp/extman
13.4测试webmail配置如何
打开浏览器输入192.168.137.101/extman 就可以了。
extman默认管理帐号为:root@extmail.org 密码为:extmail*123*
注:如果在后台创建的用户无法调整,编辑/var/www/extsuite/extmail/libs/Ext/Logger/File.pm 注释第45行:printf $fh "$time $host extmail[$$]: $msg\n", @_;
在浏览器192.168.137.101/extmail 页面注册2个用户, 测试邮件发送。