您的位置:首页 > 大数据 > 人工智能

从无到有快速搭建一个基于Web的Mail服务器,能够实现邮件的收发

2013-04-17 21:40 876 查看
一、安装前的准备工作:

1、准备好实验中所需要的各种软件包:bind97、bind97-libs、bind97-utils、mysql、mysql-devel、mysql-server、postfix-2.9.6.tar.gz、dovecot、libtool-ltdl、libtool-ltdl-devel、expect

2、关闭sendmail,并将它的随系统自动启动功能关闭:
# service sendmail stop
# chkconfig sendmail off

3、安装以下开发所用到的rpm包组:
Development Libraries
Development Tools

方法:
# yum groupinstall "packge_group_name"

二、搭建一个简易的DNS服务器

因为邮件服务依赖于DNS服务,所以这里我们简单的搭建一个DNS服务器。
1、安装配置DNS服务器
# yum install bind97 bind97-libs bind97-utils

2、配置并测试DNS服务器
# vim /etc/named.conf
options {
directory   "/var/named";
dump-file   "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursion yes;

/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
};


一个简易的DNS服务器已经搭建完毕。

三、使用rpm安装一个MySQL数据库
1、安装rpm包
# yum install mysql-server mysql-devel


我们在使用rpm安装数据库的时候需要注意到数据库的库文件及头文件的安装路径。
# service mysqld start                              #启动服务
# chkconfig mysqld on                               #添加服务级别

2、配置并测试数据库是否安装正常
# mysql                                             #测试mysql是否使用正常
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> \q
Bye

数据库至此已经安装完毕。

四、安装并调试postfix
1、安装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 /sbin/nologin -M postdrop

# tar xf postfix-2.9.6.tar.gz
# cd postfix-2.9.6


注:编译安装之前要注意系统时间是否一致;
#hwclock -s                                         #将虚拟机时间和硬件时间同步
# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS ' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2  -lssl -lcrypto'
# make
# make install

按照以下的提示输入相关的路径([]号中的是缺省值,”]”后的是输入值,省略的表示采用默认值)
install_root: [/] /
 tempdir: [/root/postfix-2.9.3] /tmp/postfix
 config_directory: [/etc/postfix] /etc/postfix
 daemon_directory: [/usr/libexec/postfix]
 command_directory: [/usr/sbin]
 queue_directory: [/var/spool/postfix]
 sendmail_path: [/usr/sbin/sendmail]
 newaliases_path: [/usr/bin/newaliases]
 mailq_path: [/usr/bin/mailq]
 mail_owner: [postfix]
 setgid_group: [postdrop]
html_directory: [no]/var/www/html/postfix
manpages: [/usr/local/man]
readme_directory: [no]

2、调试并配置postfix软件,测试使用情况
# vim /etc/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


# chmod +x /etc/init.d/postfix                      #添加执行权限
# chkconfig --add postfix                           #添加服务启动级别
# chkconfig --add postfix                           #将postfix服务添加至服务列表:
# chkconfig postfix on                              #设置其开机自动启动:
# service postfix restart                           #使用此脚本重新启动服务,以测试其能否正常执行:

# newaliases                                        #生成别名二进制文件:

3、配置postfix文件
# cd /etc/postfix/
# vim main.cf


修改以下几项为您需要的配置
myhostname = mail.wangej.com                        #指定允许postfix的主机名,默认为本机名
myorigin = wangej.com                               #指定发件人所在的域,做地址伪装
mydomain = wangej.com                               #指定自身所在的域
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, ns.$mydomain
mynetworks = 172.16.0.0/16, 127.0.0.0/8             #指定所在网络,postfix以此来判断是本地用户或远程

# postconf -n                                       #查看配置选项


注:
1、在postfix的配置文件中,参数行和注释行是不能处在同一行中的;
2、任何一个参数的值都不需要加引号,否则,引号将会被当作参数值的一部分来使用;
3、每修改参数及其值后执行 postfix reload 即可令其生效;但若修改了inet_interfaces,则需重新启动postfix;
4、如果一个参数的值有多个,可以将它们放在不同的行中,只需要在其后的每个行前多置一个空格即可;postfix会把第一个字符为空格或tab的文本行视为上一行的延续;

此时可以使用本地用户发送邮件,测试一下。

五、配置dovecot服务
# yum install dovecot -y
# vim /etc/dovecot.conf
修改#protocols = imap imaps pop3 pop3s只允许pop3即可

# service dovecot start                             #启动服务
# chkconfig dovecot on                              #添加服务运行级别


测试
# mutt -f pop://jim@mail.wangej.com




六、基于sasl认证
# vim /etc/sysconfig/saslauthd
将MECH=pam修改为MECH=shadow,使其认证方式从pam转变为读取shadow文件
# service saslauthd start                           #启动sasl服务
# chkconfig saslauthd on                            #添加服务启动级别
# testsaslauthd -utom -ptudou                       #做一下简单测试

# vim /usr/lib/sasl2/smtpd.conf                     #创建smtpd的配置文件
log_level: 3                                        #定义日志级别
pwcheck_method: saslauthd                           #定义谁来检查密码
mech_list: PLAIN LOGIN                              #定义完成认证功能

# service saslauthd restart                         #重启服务

# vim /etc/postfix/main.cf
将mynetworks = 172.16.0.0/16, 127.0.0.0/8, 192.168.0.0/24修改为只允许127.0.0.0网段访问

并在文件尾部添加以下内容:
############################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_sasl_path = smtpd
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!

# service postfix restart #重启服务
发送一封邮件使用tail查看日志:












至此基于用户的认证邮件服务就已经搭建完成。

七、安装Courier authentication library
1、首先我们要确认libtool-ltdl libtool-ltdl-devel expect是否安装上了
# yum -y install libtool-ltdl libtool-ltdl-devel
# yum install -y expect

备注:在RHEL5上安装courier时,需要安装0.64.0或之前版本。
# tar xf courier-authlib-0.64.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/lib \
--with-ltdl-include=/usr/include
# make
# make install


2、配置实用mysql连接认证
备注:可以使用--with-authdaemonvar=/var/spool/authdaemon选项来指定进程套接字目录路径
# 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=10


注:这里的数据库用户名及密码在真实生产环境中最好更改成你自己设定的。
修改/etc/authmysqlrc文件
MYSQL_SERVER            localhost                   #数据库服务器
MYSQL_USERNAME          extmail                     #数据库登录用户
MYSQL_PASSWORD          extmail                     #数据登录密码
MYSQL_DATABASE          extmail                     #数据库
MYSQL_USER_TABLE        mailbox                     #数据库表
MYSQL_CRYPT_PWFIELD     password                    #表中的哪个字段是密码
MYSQL_UID_FIELD         2525                        #postfix的uid
MYSQL_GID_FIELD         2525                        #postfix的gid
MYSQL_LOGIN_FIELD       username                    #数据库中的用户名
MYSQL_HOME_FIELD        concat('/var/mailbox/',homedir)#用户的邮件家目录
MYSQL_MAILDIR_FIELD     concat('/var/mailbox/',maildir)#用户的邮件保存地址


3、提供服务脚本
# 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 (启动服务)
4、配置postfix和courier-authlib
新建虚拟用户邮箱所在的目录,并将其权限赋予postfix用户:
#mkdir –pv /var/mailbox
#chown –R postfix /var/mailbox

接下来重新配置SMTP 认证,编辑 /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
# service saslauthd restart

八、配置让postfix支持虚拟域和虚拟用户
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
##########################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

2、这里我们使用早已经写好的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';
mysql> flush privileges;


说明:
1、启用虚拟域以后,需要取消中心域,即注释掉myhostname, mydestination, mydomain, myorigin几个指令;当然,你也可以把mydestionation的值改为你自己需要的。
2、对于MySQL-5.1以后的版本,其中extmail.sql脚本的执行会有错,因为TYPE=MyISAM在之后的版本中已经不支持了切记,可先使用如下命令进行修改之后再执行文件。
# sed -i 's@TYPE=MyISAM@ENGINE=InnoDB@g' extmail.sql

重启服务
# service postfix restart

九、安装Web服务器

安装服务器软件
# yum install httpd -y
这里我们只使用了最基本的功能,所以就不做任何配置了

十、配置dovecot
# vim /etc/dovecot.conf
mail_location = maildir:/var/mailbox/%d/%n/Maildir
……
auth default {
mechanisms = plain
passdb sql {
args = /etc/dovecot-mysql.conf
}
userdb sql {
args = /etc/dovecot-mysql.conf
}
……

# vim /etc/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


说明:如果mysql服务器是本地主机,即host=localhost时,如果mysql.sock文件不是默认的/var/lib/mysql/mysql.sock,可以使用host=“sock文件的路径”来指定新位置;

接下来启动dovecot服务:
# service dovecot start
# chkconfig dovecot on

十一、安装Extmail-1.2

PS:如果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、修改主配置文件
#vi /var/www/extsuite/extmail/webmail.cf

SYS_USER_LANG = zh_CN                       #修改默认支持的语言格式
SYS_MAILDIR_BASE = /var/mailbox             #修改用户邮件存放目录
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket

3、修改apache的相关配置

PS:由于extmail要进行本地邮件的投递操作,故必须将运行apache服务器用户的身份修改为您的邮件投递代理的用户;本例中打开了apache服务器的suexec功能,故使用以下方法来实现虚拟主机运行身份的指定。此例中的MDA为postfix自带,因此将指定为postfix用户:# vim /etc/httpd/conf/httpd.conf
修改 cgi执行文件属主为apache运行身份用户:
# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/

如果您没有打开apache服务器的suexec功能,也可以使用以下方法解决:
# vim /etc/httpd/httpd.conf
User postfix
Group postfix

<VirtualHost *:80>
ServerName mail.wangej.com
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-0.100.tar.gz
# cd Unix-Syslog-0.100
# 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

2、修改配置文件
SYS_MAILDIR_BASE = /var/mailbox                 #修改邮件存放目录
#SYS_CAPTCHA_ON = 1                             #注释验证码(这里暂不支持)
SYS_DEFAULT_UID = 2525                          #修改属主
SYS_DEFAULT_GID = 2525                          #修改属组

这里我们使用另外的用户访问extman,所以在数据库中添加了一个用户
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';
mysql> flush privileges;

而后修改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


至此,Web邮箱就已经搭建完成了,这里有一个默认的管理密码:extmail*123*

现在我们将自己的域添加进去:wangej.com至此就可以注册发送邮件了。








本文出自 “小败向前冲!” 博客,请务必保留此出处http://yhwhzhang.blog.51cto.com/3821405/1180234
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: