在这篇博客中小编要实现一个基于虚拟账号的邮件系统,读者日常生活中所用到的邮件账号都是虚拟账号,并不是登陆系统的账户,这些虚拟的账号存储在数据库中集中管理,今天小编要实现如下工作流程的邮件系统
各部分的功能:
MUA:可以使用客户端软件或者浏览器进行收发邮件但是都要结合认证,要实现浏览器访问,服务器必须搭建相应的站点,这里就使用了extmail,extman作为管理工具(域和用户的管理)
MTA:发送信件,这里小编使用源码postfix实现(老版的不支持mysql扩展),当然这里还要结合简单认证安全层(sasl,使用cyrus-sasl实现)和courier-authlib才能够最终和mysql联系起来
MRA:接收信件,小编这里使用dovecot来实现
软件需求
所需的源码包:courier-authlib-0.63.0.tar、extmail-1.2.tar、extman-1.1.tar、postfix-2.10.tar、Unix-Syslog-1.1.tar(源码包均来自互联网,如有需要可自行下载)
还需要系统自带的rpm包,这里可以使用yum安装
yum install -y mysql openssl-devel perl-DBD-MySQL tcl tcl-devel libart_lgpl libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect
Step 1.网卡以及主机名参数设置
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=192.168.111.10
# vim /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=mail.zzu.com
Step 2.搭建web服务器
Apr安装
# tar -jxf apr-1.4.6.tar.bz2 -C /usr/local/src/
# cd /usr/local/src/apr-1.4.6/
# ./configure
# make && make install
Apr-util安装
# tar -jxf apr-util-1.5.2.tar.bz2 -C /usr/local/src/
# cd /usr/local/src/apr-util-1.5.2/
# ./configure --with-apr=/usr/local/src/apr-1.4.6/apr-1-config(加上apr的路径)
# make && make install
Pcre安装
# unzip pcre-8.32.zip
# cd pcre-8.32
# ./configure
# make && make install
安装apache
# tar -jxf httpd-2.4.4.tar.bz2 -C /usr/local/src/
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr/bin/apr-1-config --with-pcre=/usr/local/pcre/
# make && make install
编写启动脚本
# vim httpd
#!/bin/bash
# chkconfig: 35 80 30
# description: Apache server
HTTPD='/usr/local/apache/bin/httpd'
LOCKFILE='/var/lock/subsys/httpd'
. /etc/init.d/functions
function start(){
[ -e $LOCKFILE ] && echo "httpd is running ......." && exit
echo -n "httpd is starting ......"
sleep 1
`$HTTPD -k start`
}
function stop(){
echo -n "httpd is stoping......"
sleep 1
killproc $HTTPD &>/dev/null
}
function status(){
[ -e $LOCKFILE ]&& echo "httpd is running ......" || echo "httpd is stoped"
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "httpd Usage: {start|stop|restart}"
;;
esac
# cp httpd /etc/init.d/
# chmod a+x /etc/init.d/httpd
# chkconfig --add httpd
# chkconfig httpd on
# service httpd start
Step 3.Mysql的安装
# groupadd mysql
# useradd -r -g mysql mysql
# cd /usr/local
# tar zxvf /path/to/mysql-VERSION-OS.tar.gz
# ln -s mysql-advanced-5.6.10-linux-glibc2.5-i686/ mysql
# cd mysql
# chown -R mysql .
# chgrp -R mysql .
# scripts/mysql_install_db --user=mysql
# chown -R root .
# chown -R mysql data
# cp support-files/my-default.cnf /etc/my.cnf
# bin/mysqld_safe --user=mysql &
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# ln –s /usr/local/mysql/lib /usr/lib/mysql
# ln -s /usr/local/mysql/include/ /usr/include/mysql
# vim /etc/bashrc //在文件最后加入下面的内容
export PATH=/usr/local/mysql/bin:$PATH
# . /etc/bashrc
启动mysql测试
# service mysqld start
# mysqladmin -u root password 123456 //修改初始密码
# mysql -u root –p
mysql> grant all on *.* to 'root'@'%' idenfied by '123456'; //赋予root用户最高权限
mysql> flush privileges; //刷新权限
Step 4.搭建DNS 服务器
# yum install bind bind-chroot caching-nameserver –y
# cd /var/named/chroot/etc/
创建配置文件
# cp -p named.caching-nameserver.conf named.conf
# vim named.conf
# vim named.rfc1912.zones
# cd ../var/named/
添加域文件
# cp -p localhost.zone zzu.com.zone
# vim zzu.com.zone
# echo “nameserver 192.168.111.10” > /etc/resolv.conf //修改DNS指向
# chkconfig named on
启动DNS服务器
# service named start
Step 5.安装配置postfix
之所以要使用源码安装是因为5.4自带的postfix不支持mysql的扩展啦
添加postfix用户
# groupadd -g 2525 postfix
# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
# groupadd -g 2526 postdrop
# useradd -g postdrop -u 2526 -s /bin/false -M postdrop
解压缩源码
# tar -zxvf postfix-2.10.0.tar.gz -C /usr/local/src/
# cd /usr/local/src/postfix-2.10.0/
# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2 -lssl -lcrypto'
这里安装前需要提示一下,可能缺少libpcre.so.1,这样解决
ln -s /usr/local/lib/libpcre.so.1 /usr/lib
# make install
install_root: [/]
tempdir: [/usr/local/src/postfix-2.10.0] /tmp
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]
newaliases_path: [/usr/bin/newaliases]
queue_directory: [/var/spool/postfix]
readme_directory: [no]
sendmail_path: [/usr/sbin/sendmail]
setgid_group: [postdrop]
# postfixconf -m
# newaliases 生成别名二进制文件,这个步骤如果忽略,会造成postfix效率极低
安装是完成了,可是没有启动脚本啊,这个好解决,可以将源码rpm包中的脚本提取出来就是了
# mkdir tmp
# cd tmp/
# cp /mnt/cdrom/Server/postfix-2.3.3-2.1.el5_2.i386.rpm .
# rpm2cpio postfix-2.3.3-2.1.el5_2.i386.rpm |cpio –id
# cd etc/rc.d/init.d/
# cp postfix /etc/init.d/
# chkconfig --add postfix
# chkconfig postfix on
好啦,启动脚本算是做完了
# vim /etc/postfix/main.cf
说明:
myorigin参数用来指明发件人所在的域名;
mydestination参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到哪个域名的邮件;
myhostname 参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名;
mydomain参数指定您的域名,默认情况下,postfix将myhostname的第一部分删除而作为mydomain的值;
mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问
启动postfix试试
# service postfix start
# netstat -tupln |grep master
测试一下是否能发信
先添加两个测试账户
# useradd user1
# echo '123456' | passwd --stdin user1
# useradd user2
# echo '123456' | passwd --stdin user2
集中测试
Step 6.安装cyrus-sasl,开启postfix的账户认证功能
# yum install cyrus-sasl
# chkconfig saslauthd on
修改postfix的main.cf文件,添加一下内容
############################CYRUS-SASL############################
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!# vim /usr/lib/sasl2/smtpd.conf
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
重新启动认证服务
# service saslauthd start
# service postfix restart
Step 7.安装配置courier-authlib
# tar -jxvf courier-authlib-0.63.0.tar.bz2 -C /usr/local/src/
#./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --with-authmysql --with-mysql-libs=/user/lib/mysql --with-mysql-includes=/usr/include/mysql --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-ltdl-lib=/usr/lib --with-ltdl-include=/usr/include --with-sqlite-libs=/usr/lib --with-sqlite-includes=/usr/include
# make && make install
# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
# cp courier-authlib.sysvinit /etc/init.d/courier-authlib //创建启动脚本# chmod 755 /etc/init.d/courier-authlib
# chkconfig --add courier-authlib
# chkconfig courier-authlib on
# cp /etc/authdaemonrc.dist /etc/authdaemonrc
# cp /etc/authmysqlrc.dist /etc/authmysqlrc
安装是算完成了
修改文件/etc/authdaemonrc
# vim /etc/authdaemonrc
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10
# vim /etc/authmysqlrc
MYSQL_SERVER 192.168.111.10
MYSQL_PORT 3306
MYSQL_USERNAME extmail
MYSQL_PASSWORD extmail
MYSQL_SOCKET /tmp/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)
# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf
# ldconfig –v
# service courier-authlib start
新建虚拟用户邮箱所在的目录,并将其权限赋予postfix用户:
# mkdir -pv /var/mailbox
# chown -R postfix /var/mailbox
重新配置SMTP 认证,编辑 /usr /lib/sasl2/smtpd.conf ,修改为一下内容
# vim /usr/lib/sasl2/smtpd.conf
pwcheck_method: authdaemond
log_level: 3
mech_list:PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
好了courier-authlib算是配置完成了
Step 8.配置postfix,让其支持虚拟域和虚拟用户
# vim /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
##########################QUOTA Settings########################
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_mailbox_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.
virtual_overquota_bounce = yes
使用extman源码目录下docs目录中的extmail.sql和init.sql建立数据库:
# tar -zxvf extman-1.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/extman-1.1/docs
这里有两个sql脚本需要导入 extmail.sql和init.sql
但是要注意直接导入extmail.sql是会出现错误,小编这里稍作修改就是了
修改之后
并且还要将每个创建表语句后面的TYPE=MyISAM 和后面的内容删掉
# mysql -u root -p <extmail.sql
# mysql -u root -p <init.sql
# cp mysql* /etc/postfix/
这里做好授权工作
重启postfix
# service postfix restart
Step 9.配置dovecot
# vim /etc/dovecot.conf
增加一行:
mail_location = maildir:/var/mailbox/%d/%n/Maildir
注释掉796、829、25257、25264行
打开870、872、874行,修改872行:
打开931、933、934行,修改933行:
# vim /etc/postfix/main.cf
取消home_mailbox = Maildir/前的注释
编辑新文件dovecot-mysql.conf
# vim /etc/dovecot-mysql.conf
添加一下内容
driver = mysqlconnect = host=192.168.111.10 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
# service dovecot start
# chkconfig dovecot on
Step 10.安装extmail
# tar -zxvf extmail-1.2.tar.gz -C /usr/local/src/
# cd /usr/local/src/
# mkdir -pv /var/www/extsuite //建立用于存放extmail的物理目录
# mv extmail-1.2 /var/www/extsuite/extmail
创建配置文件
# cp /var/www/extsuite/extmail/webmail.cf.default /var/www/extsuite/extmail/webmail.cf
修改配置文件
# vim /var/www/extsuite/extmail/webmail.cf
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_AUTHLIB_SOCKET = /var/spool/authdaemon/socket
此句用来指明authdaemo socket文件的位置,这里修改为:
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/
这里还不算完,这里还要给日志系统打个补丁
# tar -zxvf Unix-Syslog-1.1.tar.gz -C /usr/src/
[root@mail ~]# cd /usr/local/src/Unix-Syslog-1.1/
[root@mail Unix-Syslog-1.1]# perl Makefile.PL
[root@mail Unix-Syslog-1.1]# make && make install
配置extman
# cd /usr/local/src/
# 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_CAPTCHA_ON = 1
为
SYS_CAPTCHA_ON = 0
SYS_MYSQL_SOCKET = /tmp/mysql.sock
修改cgi目录的属主:
# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
好了两个站点的物理目录算是搭建好了,接下来要和apache配合起来
# vim /usr/local/apache/conf/httpd.conf //因为是源码安装的
User postfix
Group postfix
在最后添加如下内容:
<Directory /var/www/extsuite/extmail>
Order deny,allow
Allow from all
Require all granted
</Directory>
<Directory /var/www/extsuite/extman>
Order deny,allow
Allow from all
Require all granted
</Directory>
<VirtualHost 192.168.101.10:80>
ServerName mail.zzu.com
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
Alias /extman /var/www/extsuite/extman/htm
</VirtualHost>
最重要的一点要打开cgi的支持
LoadModule cgid_module modules/mod_cgid.so
# mkdir -pv /tmp/extman
# chown postfix.postfix /tmp/extman
重新启动apache
# service httpd restart
# yum install perl-DBD-mysql
最后测试的时候小编发现还要在mysql中执行一条授权语句
sql >grant all on *.* to 'webman'@'mail.zzu.com' identified by 'webman';
sql > flush privileges;
Step 11.集中测试
在客户机浏览器输入http://mail.zzu.com (客户机的DNS指向192.168.111.10)
默认的用户名为:root@extmail.org密码为:extmail*123*
这里还要注意小编使用的mysql版本比较高,注册的时候会出错,还记得当初导入表的时候已经做过修改么,这里还要进行一项修改
mysql > use extmail
mysql > alter table domain
> modify can_signup varchar(1);
但是貌似新建域的过程中的允许自由注册选项没法生效,读者可以再修改一下
退出到主页,注册用户
注册两个用户user1和user2
登陆user1发信测试
退出登陆,切换到user2收信试试
打开信件看消息
再回复试试
User1的收信情况
到此整个邮件系统就算搭建完成了
如果在搭建过程中出现问题,请及时联系小编哈
本文转自 chenming421 51CTO博客,原文链接:http://blog.51cto.com/wnqcmq/1192457