概述介绍本案例基于Centos7搭建企业级邮件服务器,并基于extman进行管理账户,同时提供一个webmail给用户进行邮件管理操作。配置邮件服务器并不是一项简单的工作,其过程不仅需要一定的技术能力
概述介绍
本案例基于Centos7搭建企业级邮件服务器,并基于extman进行管理账户,同时提供一个webmail给用户进行邮件管理操作。配置邮件服务器并不是一项简单的工作,其过程不仅需要一定的技术能力,同时也需要大家具有一定的耐心,同时也需要大家非常细心。为了能够**「看懂」**并 **「最终实践」**这篇文章,大家还需要了解或掌握以下知识:
Centos :
.如果说 Ubuntu 是现今最受桌面用户欢迎的 Linux 操作系统,那么 CentOS 就是最受公司、企业、IDC 喜爱的 Linux 发行版了。得益于极为出色的稳定性,全球范围内无数著名网站均选用它,异次元的服务器也是!CentOS 是基于 Red Hat Enterprise Linux (收费昂贵但口碑极佳) 的源代码再编译出来的免费版,因此不仅继承 RHEL 优越的稳定性,还提供免费更新,因此在服务器提供商、中小型公司中装机量几乎是最大的 Linux 发行版,现在也已正式加入红帽公司。从事互联网技术的同学,CentOS 应该是值得你深入了解学习研究的。
.Postfix :
.是一个标准的MTA「Mail Transfer Agent」服务器,它负责通过SMTP协议管理发送到本机的邮件以及由本机发向外界的邮件。在本例中,Postfix会把邮件的本地投递工作「接受到邮件之后将邮件存档到本地磁盘」交给Dovecot的LMTP服务「Local Mail Transfer Protocol service」处理。当然,当大家想通过服务器向外界发送邮件时,Postfix还将负责验证权限以确保服务器不被滥用。「很多邮件服务器根本没有对SMTP做用户验证,这将导致任何匿名用户都可以通过服务器向外界发送邮件,从而使得服务器变成垃圾中转站」
.**Dovecot ** :
.是一个非常优秀的IMAP/POP服务器用以接收外界发送到本机的邮件。通常,Dovecot的工作内容包括:验证用户身份以确保邮件不会被泄露。在本例中,Dovecot将负责所有的「身份验证」工作,我们会配置Dovecot查询本地的MySQL数据库以确认用户身份。
.**courier-authlib ** :
.courier-authlib是Courier组件中的认证库,它是courier组件中一个独立的子项目,用于为Courier的其它组件提供认证服务。其认证功能通常包括验正登录时的帐号和密码、获取一个帐号相关的家目录或邮件目录等信息、改变帐号的密码等。而其认证的实现方式也包括基于PAM通过/etc/passwd和/etc/shadow进行认证,基于GDBM或DB进行认证,基于LDAP/MySQL/PostgreSQL进行认证等。因此,courier-authlib也常用来与courier之外的其它邮件组件(如postfix)整合为其提供认证服务。
.**extmail,extman ** :
.ExtMail套件用于提供从浏览器中登录、使用邮件系统的Web操作界面,而Extman套件用于提供从浏览器中管理邮件系统的Web操作界面。Extmail 套件可以提供给普通邮件用户使用,而Extman套件可以提供给邮件系统的管理员使用。整个项目的目标是开发出高效、 易用、富有生命力的邮件系统(方案),经过粗略的统计,截止2009年10月份,已有超过8000个服务器在运行ExtMail软件,其中有超过4000 个运行在互联网上。Extmail的主要特点集中如下:支持Maildir,索引技术,速度超快 支持多国语言同屏显示,真正国际邮 支持巨型邮箱(超过1GB)海量文件 支持无限尺寸附件,I/O能力强 模板化,多语言,修改非常容易.
.** nginx ** :
.Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
.安装前准备
- 比较熟悉iptables,所以先关闭防火墙,采用iptables服务,如果没有按照实际情况yum安装
# systemctl stop firewalld# systemctl disable firewalld# iptables-service# touch /etc/sysconfig/iptables# systemctl start iptables# systemctl enable iptables
- 如果系统有sendmail,需要停止服务,并禁止开机启动
# service sendmail stop# chkconfig sendmail off
- 因为rpm的postfix不支持mysql认证,我们采用源码安装postfix,所以需要删除原来的postfix及账户体系
# yum remove postfix# userdel postfix# groupdel 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
注:这里不要为了偷懒而不去删除postfix用户,postfix的rpm包安装好之后,postfix用户的UID和GID是89,如果这里以这样一个ID去运行服务,后面会出一些错误!所以最好自己创建一个用户,ID号大于500的。我这里使用的是2525,也不建议大家改,因为这里一改,后面很多很多地方,都需要改。postfix搞起来本身就非常的麻烦!
- 安装依赖
# yum install nginx vim gcc gcc-c++ openssl openssl-devel db4-devel ntpdate mysql mysql-devel bzip2 php-mysql cyrus-sasl-md5 perl-GD perl-DBD-MySQL perl-GD perl-CPAN perl-CGI perl-CGI-Session cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel libtool-ltdl-devel telnet mail libicu-devel -y
** 注:未直接采用本机作为mysql服务器,所以没有安装,如需安装:追加mysql-server **
- 安装postfix
wget http://mirrors.go-parts.com/postfix/source/official/postfix-3.0.1.tar.gztar xf postfix-3.0.1.tar.gzcd postfix-3.0.1make 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 -lrt -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto'make && make install
配置postfix
chown -R postfix:postdrop /var/spool/postfixchown -R postfix:postdrop /var/lib/postfix/chown root /var/spool/postfixchown -R root /var/spool/postfix/pid
[root@localhost ~]# vim /etc/postfix/main.cfmyhostname = mail.sample.com //设置主机名mydomain = sample.com //指定域名myorigin = $mydomain //指明发件人所在的域名inet_interfaces = //all指定postfix系统监听的网络接口mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain //指定postfix接收邮件时收件人的域名 [使用虚拟域需要禁用]mynetworks_style = host //指定信任网段类型mynetworks = 192.168.1.0/24, 127.0.0.0/8 //指定信任的客户端relay_domains = $mydestination //指定允许中转邮件的域名alias_maps = hash:/etc/aliases //设置邮件的别名
- 安装配置dovecot
[root@localhost ~]# yum install -y dovecot dovecot-mysql
配置dovecot
[root@localhost ~]# cd /etc/dovecot/[root@localhost dovecot]# vim dovecot.conf //直接在配置文件最后添加即可protocols = imap pop3!include conf.d/*.conflisten = *base_dir = /var/run/dovecot/[root@localhost dovecot]# cd conf.d/[root@localhost conf.d]# vim 10-auth.confdisable_plaintext_auth = no[root@localhost conf.d]# vim 10-mail.confmail_location = maildir:~/Maildirmail_location = maildir:/var/mailbox/%d/%n/Maildirmail_privileged_group = mail[root@localhost conf.d]# vim 10-ssl.confssl = no[root@localhost conf.d]# vim 10-logging.conf og_path = /var/log/dovecot.loginfo_log_path = /var/log/dovecot.infolog_timestamp = "%Y-%m-%d %H:%M:%S "[root@localhost conf.d]# cp auth-sql.conf.ext auth-sql.conf[root@localhost conf.d]# vim auth-sql.confpassdb { driver = sql # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext args = /etc/dovecot/dovecot-sql.conf.ext}userdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext}
编辑dovecot通过mysql认证的配置文件
[root@localhost conf.d]# vim /etc/dovecot-mysql.confdriver = mysqlconnect = host=localhost dbname=extmail user=extmail password=extmaildefault_pass_scheme = CRYPTpassword_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'
- 安装courier-unicode-1.2编译安装
[root@localhost ~]# wget https://sourceforge.net/projects/courier/files/courier-unicode/1.2/courier-unicode-1.2.tar.bz2[root@localhost ~]# tar xf courier-unicode-1.2.tar.bz2 [root@localhost ~]# cd courier-unicode-1.2[root@localhost courier-unicode-1.2]# ./configure[root@localhost courier-unicode-1.2]# make && make install
安装courier-authlib
[root@localhost ~]# wget https://sourceforge.net/projects/courier/files/authlib/0.66.2/courier-authlib-0.66.2.tar.bz2[root@localhost ~]# tar xf courier-authlib-0.66.2.tar.bz2[root@localhost ~]# cd courier-authlib-0.66.2[root@localhost ~]# ./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[root@localhost ~]#make && makeinstall
配置 courier-authlib
[root@localhost courier-authlib-0.66.2]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon[root@localhost courier-authlib-0.66.2]# cp /etc/authdaemonrc.dist /etc/authdaemonrc[root@localhost courier-authlib-0.66.2]# cp /etc/authmysqlrc.dist /etc/authmysqlrc[root@localhost courier-authlib-0.66.2]# vim /etc/authdaemonrc //配置文件里的验证方法比较多,我们这里只使用authmysqlauthmodulelist="authmysql"authmodulelistorig="authmysql"[root@localhost courier-authlib-0.66.2]# vim /etc/authmysqlrc //直接添加到配置文件尾部,然后去上面将响应系统默认的注视掉,或者删除即可MYSQL_SERVER localhostMYSQL_USERNAME extmailMYSQL_PASSWORD extmailMYSQL_SOCKET /var/lib/mysql/mysql.sockMYSQL_PORT 3306MYSQL_DATABASE extmailMYSQL_USER_TABLE mailboxMYSQL_CRYPT_PWFIELD passwordDEFAULT_DOMAIN test.comMYSQL_UID_FIELD '2525'MYSQL_GID_FIELD '2525'MYSQL_LOGIN_FIELD usernameMYSQL_HOME_FIELD concat('/var/mailbox/',homedir)MYSQL_NAME_FIELD nameMYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)
courier-authlib 添加服务启动脚本及其他
[root@localhost courier-authlib-0.66.2]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib[root@localhost courier-authlib-0.66.2]# chmod +x /etc/init.d/courier-authlib[root@localhost courier-authlib-0.66.2]# chkconfig --add courier-authlib[root@localhost courier-authlib-0.66.2]# chkconfig courier-authlib on[root@localhost courier-authlib-0.66.2]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf[root@localhost courier-authlib-0.66.1]# ldconfig[root@localhost courier-authlib-0.66.1]# service courier-authlib startStarting Courier authentication services: authdaemond
smtp以及虚拟用户相关的设置
[root@localhost ~]# vim /usr/lib64/sasl2/smtpd.conf //文件不存在,要自己创建pwcheck_method: authdaemondlog_level: 3mech_list: PLAIN LOGINauthdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket[root@localhost ~]# vim /etc/postfix/main.cf##postfix支持SMTP##smtpd_sasl_auth_enable = yessmtpd_sasl_local_domain = ''smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destinationbroken_sasl_auth_clients=yessmtpd_client_restrictions = permit_sasl_authenticatedsmtpd_sasl_security_options = noanonymous##postfix支持虚拟用户##virtual_mailbox_base = /var/mailboxvirtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf //这里的配置文件需在后面extman里复制过来virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cfvirtual_alias_domains =virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cfvirtual_uid_maps = static:2525virtual_gid_maps = static:2525virtual_transport = virtual
- 安装extmail,extman
[root@localhost ~]# wget http://7xivyw.com1.z0.glb.clouddn.com/extmail-1.2.tar.gz[root@localhost ~]# http://7xivyw.com1.z0.glb.clouddn.com/extman-1.1.tar.gz[root@localhost ~]# mkdir -p /var/www/extsuite[root@localhost ~]# tar xf extmail-1.2.tar.gz -C /var/www/extsuite/[root@localhost ~]# mv /var/www/extsuite/extmail-1.2/ /var/www/extsuite/extmail
更改extmail的配置文件
[root@localhost ~]# cd /var/www/extsuite/extmail[root@localhost extmail]# cp webmail.cf.default webmail.cf[root@localhost extmail]# vim webmail.cfSYS_SESS_DIR = /tmp/extmailSYS_UPLOAD_TMPDIR = /tmp/extmail/uploadSYS_USER_LANG = zh_CNSYS_MIN_PASS_LEN = 8SYS_MAILDIR_BASE = /var/mailboxSYS_MYSQL_USER = extmailSYS_MYSQL_PASS = extmailSYS_MYSQL_DB = extmailSYS_MYSQL_HOST = localhostSYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sockSYS_MYSQL_TABLE = mailboxSYS_MYSQL_ATTR_USERNAME = usernameSYS_MYSQL_ATTR_DOMAIN = domainSYS_MYSQL_ATTR_PASSWD = passwordSYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
建立临时文件目录与session目录
[root@localhost extmail]# mkdir -p /tmp/extmail/upload[root@localhost extmail]# chown -R postfix.postfix /tmp/extmail/
安装extman
[root@localhost ~]# tar xf extman-1.1.tar.gz -C /var/www/extsuite/[root@localhost ~]# cd /var/www/extsuite/[root@localhost extsuite]# mv extman-1.1/ extman
更改extman配置文件
[root@localhost extsuite]# cd extman/[root@localhost extman]# cp webman.cf.default webman.cf
更改cgi目录属主属组
[root@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/[root@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/
导入数据库由于数据库不能识别TYPE= MyISAM ,所以这里直接导入会出错,先编辑extmail.sql数据库文件,将TYPE=MyISAM更改为ENGINE=MyISAM此处需要修改my.cnf配置文件
[root@localhost extman]# vim /etc/my.cnf# Recommended in standard MySQL setup#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES //将这行注视掉,重启mysql,这里需要注意的是,等数据库导入成功后,这项是不可以去掉注释的,不然mysql就启动不起来了。再次导入数据库[root@localhost extman]# mysql -uroot < docs/extmail.sql [root@localhost extman]# mysql -uroot < docs/init.sql
复制extman提到的配置文件到postfix
[root@localhost ~]# cd /var/www/extsuite/extman/docs/[root@localhost docs]# cp mysql_virtual_* /etc/postfix/
为extman创建临时目录
[root@localhost extman]# mkdir /tmp/extman[root@localhost extman]# chown -R postfix.postfix /tmp/extman/
启动postfix、dovecot、saslauthd
[root@localhost extman]# ss -tnluo | grep :25tcp LISTEN 0 100 *:25 *:* [root@localhost extman]# ps aux | grep dovecotroot 2220 0.1 0.3 15616 1508 ? Ss 03:02 0:00 /usr/sbin/dovecot -Fdovecot 2223 0.0 0.1 9312 1004 ? S 03:02 0:00 dovecot/anvilroot 2224 0.0 0.2 9440 1172 ? S 03:02 0:00 dovecot/logroot 2226 0.0 0.4 12428 2184 ? S 03:02 0:00 dovecot/configroot 2240 0.0 0.1 112640 972 pts/0 R+ 03:02 0:00 grep --color=auto dovecot[root@localhost extman]# ps aux | grep saslauthdroot 2230 0.0 0.1 71944 916 ? Ss 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2231 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2232 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2233 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2234 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2242 0.0 0.1 112640 668 pts/0 R+ 03:03 0:00 grep --color=auto saslauthd
测试虚拟用户
[root@localhost courier-authlib-0.66.2]# telnet 127.0.0.1 25 #回车输入创建的邮件账号如:liudehua回车密码,会提示认证通过的信息[root@localhost courier-authlib-0.66.2]# mkdir /var/mailbox[root@localhost courier-authlib-0.66.2]# chown -R postfix.postfix /var/mailbox/
常见问题
这里是列表文本Undefined subroutine &Ext::Utils::sort2name called at /var/www/extsuite/extmail/libs/Ext/App/Folders.pm line 387. extmail展示错误
解决中.....
extman中创建用户后,/var/mailbox中无法创建对应的目录解决办法:写了一个python脚本,cron每5分钟运行一次,自动根据数据库新建的邮件账户,间接解决。
1)python 如下
# -*- coding: UTF-8 -*-#安装 MYSQL DB for pythonimport MySQLdb as mdbimport osimport logging,pwduid = pwd.getpwnam('postfix').pw_uid; gid = pwd.getpwnam('postfix').pw_gid;logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='maildir-create.log', filemode='a')logging.debug("create mailbox cron touch start.")mailhome = "/home/mailbox/"con = Nonetry: #连接 mysql 的方法: connect('ip','user','password','dbname') con = mdb.connect('192.168.11.37', 'infmanage','376347', 'extmail'); #所有的查询,都在连接 con 的一个模块 cursor 上面运行的 cur = con.cursor() #执行一个查询 cur.execute("select maildir,uid from mailbox where mailhost = ''") #取得上个查询的结果,是单个结果 rows = cur.fetchall() for row in rows: if not os.path.exists(mailhome + row[0]): print "maildir %s to create" % (row[0]) os.makedirs(mailhome + row[0]) logging.debug("create mail box " + row[0]) dstat = os.stat(mailhome + row[0]); if dstat.st_uid != uid: try: os.chown(mailhome + row[0], uid, gid); except: logging.debug("error chown postfix fail") else: print "maildir %s is exist" % (row[0]) params = ['mail.inf-technology.com',row[1]] cur.execute("update mailbox set mailhost=%s where uid=%s",params) logging.debug("create mailbox cron touch end.")finally: if con: #无论如何,连接记得关闭 con.close()
2)crontab -e 追加*/5 * * * * python /home/shell/extman-create.py3)重启crontab
- 这里是列表文本extmail首页登录默认域名是ip,如何换成默认域名百度了方法,直接修改html的源代码文件,就可以解决
参考文献
博文:CentOS7下搭建postfix邮件服务器实现extmail的web访问
博文:【POSTFIX系列之一】编译安装POSTFIX邮件服务器
博文:Postfix-2.11+Dovecot-2.0.9+MySQL+Cyrus-sasl+Extmail-1.2实现基于虚拟用户的邮件系统架构
软件下载
postfix源码
courier-authlib源码
courier-unicode源码
extmail源码
extman源码