一、安装前的准备工作

邮件依赖于DNS系统,所以安装postfix之前,要把DNS给弄好。如果系统有sendmail,需要把sendmail关掉

# service sendmail stop
# chkconfig sendmail off

由于是编译安装postfix,所以如果有rpm的postfix,要删除掉,并且把postfix用户等信息也删除掉。因为我后面要配合做mysql认证,好像rpm的postfix不支持mysql认证,我也没有验证过,为了保险起见,直接源码来安装了!

# yum remove postfix
# userdel postfix
# groupdel postdrop

创建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

注:这里不要为了偷懒而不去删除postfix用户,postfix的rpm包安装好之后,postfix用户的UID和GID是89,如果这里以这样一个ID去运行服务,后面会出一些错误!所以最好自己创建一个用户,ID号大于500的。我这里使用的是2525,也不建议大家改,因为这里一改,后面很多很多地方,都需要改。postfix搞起来本身就非常的麻烦!


安装编译环境及依赖软件包

# yum groupinstall 'Development Libraries' 'Development Tools'
# yum install httpd php php-mysql mysql mysql-server mysql-devel openssl-devel dovecot perl-DBD-MySQL tcl tcl-devel libart_lgpl libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect perl-Unix-Syslog  db4 db4-devel cyrus-sasl-* dovecot-mysql perl-CGI per-GD

有些源里可能没有perl-Unix-Syslog这个包,可以使用下面的命令来下载安装# rpm -ivh ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Unix-Syslog-1.1-1.el6.rf.x86_64.rpm


启动saslauthd服务及mysql

# service saslauthd start
# chkconfig saslauthd on
# service mysqld start
# chkconfig mysqld on

二、开始安装postfix

# wget ftp://ftp.cuhk.edu.hk/pub/packages/mail-server/postfix/official/postfix-2.10.2.tar.gz
# tar xf postfix-2.10.2.tar.gz
# cd postfix-2.10.2
# 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'

注:上面的命令要注意的是根据自己的情况去修改一些东西。我这里是64位的系统,所以mysqlclient指的是/usr/lib64/,如果是32位的系统,就是/usr/lib/ 否则会报/usr/bin/ld: cannot find -lmysqlclient

# make && make install
    BTW: Check your /etc/aliases file and be sure to set up aliases
    that send mail for root and postmaster to a real person, then run
    /usr/bin/newaliases.

安装完成后,可以看到postfix提示生成别名二进制文件

#  newaliases

我这里准备搭建的是企业级的邮件系统,需要支持虚拟用户的,本地用户测试这一些东西,现在就先不搞了!

提供脚本管理postfix启动与停止,把下面的内容放在/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 $?

为脚本添加执行权限,并加入开机启动

# chmod +x /etc/init.d/postfix
# chkconfig --add postfix
# chkconfig postfix on
# chown postfix.postfix -R /var/lib/postfix/
# chown postfix.postfix /var/spool/ -R
# service postfix start

为postfix开户基于cryus-sasl的认证功能
使用以下命令验正postfix是否支持cyrus风格的sasl认证,如果您的输出为以下结果,则是支持的:

# postconf -a
cyrus
dovecot

添加以下内容:

#vim /etc/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_sasl_application_name = smtpd
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!

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

三、安装Courier authentication library

courier-authlib是Courier组件中的认证库,它是courier组件中一个独立的子项目,用于为Courier的其它组件提供认证服务。其认证功能通常包括验正登录时的帐号和密码、获取一个帐号相关的家目录或邮件目录等信息、改变帐号的密码等。而其认证的实现方式也包括基于PAM通过/etc/passwd和/etc/shadow进行认证,基于GDBM或DB进行认证,基于LDAP/MySQL/PostgreSQL进行认证等。因此,courier-authlib也常用来与courier之外的其它邮件组件(如postfix)整合为其提供认证服务。

# wget http://jaist.dl.sourceforge.net/project/courier/authlib/0.66.1/courier-authlib-0.66.1.tar.bz2
# tar xf courier-authlib-0.66.1.tar.bz2
# cd courier-authlib-0.66.1
# ./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
# 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 文件

# vi /etc/authdaemonrc
authmodulelist="authmysql"  # 会支持很多认证方式,只保留authmysql即可
authmodulelistorig="authmysql"
daemons=10   # 开户认证的进程数,也可以不用改,根据自己的情况

配置通过MySQL认证,修改/etc/authmysqlrc,其中2525,2525 为postfix 用户的UID和GID。

# vi /etc/authmysqlrc
MYSQL_SERVER localhost
MYSQL_PORT 3306                   (指定你的mysql监听的端口,这里使用默认的3306)
MYSQL_USERNAME  extmail      (这时为后文要用的数据库的所有者的用户名)
MYSQL_PASSWORD extmail        (密码,最好不要改,不然后面安装Extmail的时候也要改,挺麻烦)
MYSQL_SOCKET  /var/lib/mysql/mysql.sock
MYSQL_DATABASE  extmail
MYSQL_USER_TABLE  mailbox
MYSQL_CRYPT_PWFIELD  password
DEFAULT_DOMAIN   gm100861.com  # 默认域
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)

提供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
# service courier-authlib start   (启动服务)
# chkconfig courier-authlib on

由于是socket的方式,所以没有端口,验证一下进程有没启

# ps -ef | grep courier
root     11347     1  0 17:02 ?        00:00:00 /usr/local/courier-authlib/sbin/courierlogger -pid=/usr/local/courier-authlib/var/spool/authdaemon/pid -start /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11348 11347  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11349 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11350 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11351 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11352 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11353 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11354 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11355 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11356 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11357 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond
root     11358 11348  0 17:02 ?        00:00:00 /usr/local/courier-authlib/libexec/courier-authlib/authdaemond

新建虚拟用户邮箱所在的目录,并将其权限赋予postfix用户:

# mkdir –p /var/mailbox
# chown -R  postfix /var/mailbox

接下来配置SMTP 认证,编辑 /usr/lib64/sasl2/smtpd.conf ,确保其为以下内容:

# vi /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

四、让postfix支持虚拟域和虚拟用户并为postfix配置dovecot

支持虚拟域和虚拟用户,编辑/etc/postfix/main.cf,添加如下内容:

# vi /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 = 10
maildrop_destination_concurrency_limit = 10
##########################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

配置dovecot

# vi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
disable_plaintext_auth = no

# vi /etc/dovecot/conf.d/10-mail.conf
mail_location =  maildir:/var/mailbox/%d/%n/Maildir

# cp /etc/dovecot/conf.d/auth-sql.conf.ext /etc/dovecot/conf.d/auth-sql.conf
# vi /etc/dovecot/conf.d/auth-sql.conf
passdb {
  driver = sql
  args = /etc/dovecot-mysql.conf
}
userdb {
  driver = sql
  args = /etc/dovecot-mysql.conf
}

编辑dovecot通过mysql认证的文件

# vi /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'

启动dovecot服务:

# service dovecot start
# chkconfig dovecot on

五、安装Extmail和Extman

Extmail是一款国人开发的开源的webmail工具,主要是使用perl写的。Extmail用于用户的登陆注册,发送接收邮件等。Extman是Extmain的管理工具,主要用于管理员来管理Extmail的信息,比如添加虚拟用,添加用户等!

下载地址:http://www.extmail.org/cgi-bin/download.cgi

安装Extmail

# 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

修改Extmail的主配置文件

# vi /var/www/extsuite/extmail/webmail.cf

部分修改选项的说明:
SYS_MESSAGE_SIZE_LIMIT = 5242880 #5M
用户可以发送的最大邮件

SYS_SESS_DIR = /tmp/extmail  用户的session保存的位置
SYS_UPLOAD_TMPDIR = /tmp/extmail/upload

SYS_USER_LANG = en_US
语言选项,可改作:
SYS_USER_LANG = zh_CN

SYS_MIN_PASS_LEN = 8  最短密码长度

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_SOCKET = /var/lib/mysql/mysql.sock

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
此句用来指明authdaemo socket文件的位置,这里修改为:
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket

建立extmail的临时文件目录及session目录

# mkdir -p /tmp/extmail/upload
# chown postfix.postfix -R /tmp/extmail/

apache相关配置

由于extmail要进行本地邮件的投递操作,故必须将运行apache服务器用户的身份修改为您的邮件投递代理的用户;如果apache打开了SUEXEC,需要配置suexec,挺麻烦的,我是直接关掉了

# vi /etc/httpd/conf/httpd.conf
User postfix
Group postfix
#LoadModule suexec_module modules/mod_suexec.so  # 注释掉,关闭suexec
#DocumentRoot "/var/www/html"   # 关闭中心主机,开户虚拟主机

建立Extmail虚拟主机配置文件

# vi /etc/httpd/conf.d/extmail.conf
<VirtualHost *:80>
ServerName mail.gm100861.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/html
</VirtualHost>
# service httpd restart
# chkconfig httpd on

安装及配置Extman

# 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 = /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
修改为:
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail

SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock

而后修改cgi目录的属主:

# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/

导入Extman的数据库

# cd /var/www/extsuite/extman/docs
# mysql -u root -p < extmail.sql
# mysql -u root -p <init.sql
# cp mysql_virtual_* /etc/postfix/

授予用户extmail访问extmail数据库的权限

# mysql -u root -p
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';

为Extman创建临时目录并给予权限

# mkdir /tmp/extman
# chown postfix.postfix -R /tmp/extman/

全部完成后,就可以通过IP地址或者域名来访问Extmail了。

extmail的首页,可以在域名那里输入你的域名,然后点下面的注册来注册一个用户,不过默认只有一个extmail的域,所以我们需要登陆Extman来管理域列表,添加一个我们自己的虚拟域

点登录邮箱那个下拉三角,选择登录邮箱管理,默认的管理员账号和密码是:

root@extmail.org 
extmail*123*

六、配置Mailgraph_ext,使用Extman的图形日志:(下面所需要的软件包要自己下载)

接下来安装图形日志的运行所需要的软件包Time::HiRes、File::Tail和rrdtool,其中前两个包您可以去http://search.cpan.org搜索并下载获得,后一个包您可以到 http://oss.oetiker.ch/rrdtool/pub/?M=D下载获得,也可以通过yum来安装; 注意安装顺序不能改换。


安装Time::HiRes

# tar zxvf Time-HiRes-1.9707.tar.gz
# cd Time-HiRes-1.9707
# perl Makefile.PL
# make
# make test
# make install

安装File::Tail

# tar zxvf File-Tail-0.99.3.tar.gz
# cd File-Tail-0.99.3
# perl Makefile.PL
# make
# make test
# make install

安装rrdtool

# yum install -y rrdtool

创建必要的符号链接(Extman会到这些路径下找相关的库文件)

# ln -sv /usr/lib64/perl5/auto/RRDs/RRDs.so /usr/lib64/perl5/5.10.0/x86_64-linux-thread-multi/CORE/
# ln -sv /usr/lib64/perl5/RRDp.pm /usr/lib64/perl5/5.10.0/
# ln -sv /usr/lib64/perl5/RRDs.pm /usr/lib64/perl5/5.10.0/
复制mailgraph_ext到/usr/local,并启动之
# cp -r /var/www/extsuite/extman/addon/mailgraph_ext /usr/local/
# /usr/local/mailgraph_ext/mailgraph-init start

启动cmdserver(在后台显示系统信息)

# /var/www/extsuite/extman/daemon/cmdserver --daemon

添加到自动启动队列

# 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里,点“图形日志”即可看到图形化的日志。具体每天,周,月,年的则点击相应的图片进入即可。



注意:安装过程中会出现很多问题,我把遇到的问题汇总一下,提供大家参考!请点此处