基于Centos6.2 X64系统下的邮件系统
 
所使用到的软件:
Postfix+Dovecot+Mysql+PostfixAdmin+Roundcubemail
Amavisd-new+ClamAV+SpamAssassin

 

基于Centos6.2 X64系统下的邮件系统(一)_postfix

 

 

1、准备工作
在安装前请把防火墙和selinux关闭,以防出错
163开源镜像站把CentOS的YUM软件仓库配置下载到本地
添加rpmforge软件仓库
 
[root@mail ~]# cd /etc/yum.repos.d/
[root@mail yum.repos.d]# mkdir bak
[root@mail yum.repos.d]# mv CentOS* bak
[root@mail etc]# cd
[root@mail ~]# cp -var CentOS6-Base-163.repo /etc/yum.repos.d/
[root@mail ~]# rpm -ivh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
 
[root@mail ~]# ll /etc/yum.repos.d/
total 24
drwxr-xr-x. 2 root root 4096 Apr 25 03:21 bak
-rw-r--r--. 1 root root 2006 Apr 16 19:52 CentOS6-Base-163.repo
-rw-r--r--. 1 root root 739 Nov 13 2010 mirrors-rpmforge
-rw-r--r--. 1 root root 717 Nov 13 2010 mirrors-rpmforge-extras
-rw-r--r--. 1 root root 728 Nov 13 2010 mirrors-rpmforge-testing
-rw-r--r--. 1 root root 1113 Nov 13 2010 rpmforge.repo
 
[root@mail ~]# yum clean all
[root@mail ~]# yum update
 
Postfix用6.2系统自带的,因为CentOS6里面的postfix包已经支持mysql
 
创建一个vmail用户,用作管理虚拟邮箱的文件夹
useradd -u 2000 -d /var/vmail -m -s /sbin/nologin vmail
 
安装dovecot软件
[root@mail ~]# yum install dovecot dovecot-devel dovecot-mysql –y
 
Dovecot设置为开机启动
[root@mail ~]# chkconfig dovecot on
 
2、安装Mysql+Httpd+Postfixadmin
 
[root@mail ~]# yum install mysql mysql-server mysql-devel httpd php –y
 
启动mysql与httpd
[root@mail ~]# /etc/init.d/mysqld start
[root@mail ~]# /etc/init.d/httpd start
 
设置为开机启动
[root@mail ~]# chkconfig mysqld on
[root@mail ~]# chkconfig httpd on
 
 
3、配置PostfixAdmin
在上面的网址下载最新的PostfixAdmin软件包,我的版本是postfixadmin-2.3.5
把安装包移动到/var/www/html目录下
[root@mail ~]# tar xvf postfixadmin-2.3.5.tar.gz
[root@mail ~]# mv postfixadmin-2.3.5 /var/www/html/postfixadmin
 
为PostfixAdmin创建Mysql数据库与权限
 
[root@mail ~]# mysql
mysql> create database mail;
mysql> GRANT ALL PRIVILEGES ON mail.* TO admin@"localhost" IDENTIFIED BY '123123' WITH GRANT OPTION;
 
修改PostfixAdmin的配置文件
[root@mail ~]# vim /var/www/html/postfixadmin/config.inc.php
 
$CONF['configured'] = true;
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'admin';
$CONF['database_password'] = '123123';
$CONF['database_name'] = 'mail';
$CONF['admin_email'] = 'postmaster@example.com';
$CONF['encrypt'] = 'dovecot:CRAM-MD5';
$CONF['dovecotpw'] = "/usr/bin/doveadm pw";
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['aliases'] = '1000';
$CONF['mailboxes'] = '1000';
$CONF['maxquota'] = '1000';
$CONF['fetchmail'] = 'NO';
$CONF['quota'] = 'YES';
$CONF['used_quotas'] = 'YES';
$CONF['new_quota_table'] = 'YES';
 
配置完毕后在浏览器上运行http://mail.example.com/postfixadmin/setup.php

基于Centos6.2 X64系统下的邮件系统(一)_CentOS_02 

 

上图错误为缺少php-mysql包,安装后重启httpd服务即可
[root@mail ~# yum install php-mysql –y
[root@mail ~# /etc/init.d/httpd restart

 

基于Centos6.2 X64系统下的邮件系统(一)_CentOS_03 

 

安装上图缺少的php扩展包,重启httpd服务
[root@mail ~# yum install php-mbstring php-imap –y
[root@mail ~# /etc/init.d/httpd restart
 
 设置setup password
把生成的hash值复制到
$CONF['setup_password'] = 'changeme'
 
postfxiadmin不能自动创建目录,增加自动建立目录的功能
建立创建虚拟邮箱脚本,脚本名称 /usr/local/bin/maildir-creation.sh ,脚本内容如下:
 
#!/bin/bash
HOME_DIR="/var/vmail"
USER_NAME="vmail"
GROUP_NAME="vmail"
if [ ! -d ${HOME_DIR}/$1 ] ; then
    mkdir ${HOME_DIR}/$1
    chown -R ${USER_NAME}.${GROUP_NAME} ${HOME_DIR}/$1
fi
mkdir ${HOME_DIR}/$1/$2
chown -R ${USER_NAME}.${GROUP_NAME} ${HOME_DIR}/$1/$2

建立删除虚拟邮箱脚本,脚本名称 /usr/local/bin/maildir-deletion.sh ,脚本内容如下:
 
#!/bin/bash
#
# vmta ALL = NOPASSWD: /usr/local/bin/maildir-deletion.sh
#
if [ $# -ne 2 ] ; then
  exit 127
fi
DOMAIN="$1"
USER="$2"
HOME_DIR="/var/vmail"
USER_DIR="${HOME_DIR}/${DOMAIN}/${USER}"
TRASH_DIR="${HOME_DIR}/deleted-maildirs"
DATE=`date "+%Y%m%d_%H%M%S"`
if [ ! -d "${TRASH_DIR}/${DOMAIN}" ] ; then
    mkdir -p "${TRASH_DIR}/${DOMAIN}"
fi
if [ -d "${USER_DIR}" ] ; then
    mv ${USER_DIR} ${TRASH_DIR}/${DOMAIN}/${USER}-${DATE}
fi

建立删除目录
[root@mail html]# mkdir /var/vmail/deleted-maildirs
[root@mail html]# chown -R vmail.vmail /var/vmail/deleted-maildirs/
 
赋予脚本可执行权限
[root@mail html]# chmod 750 /usr/local/bin/maildir-*
[root@mail html]# chown vmail.vmail /usr/local/bin/maildir-*
 
配置sudo
在 /etc/sudoers 增加一行
vmail ALL = NOPASSWD: /usr/local/bin/maildir-creation.sh
vmail ALL = NOPASSWD: /usr/local/bin/maildir-deletion.sh
 
在/etc/sudoers 注释掉下面内容
#Defaults    requiretty
 
 
修改postfixadmin的相关文件
修改create-mailbox.php 文件,229行内容应该是:
db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
 
在该行前面增加下面一行:
system("sudo /usr/local/bin/maildir-creation.sh $fDomain ".$_POST['fUsername']);
 
修改delete.php 文件,146行内容应该是:
db_log ($SESSID_USERNAME, $fDomain, 'delete_mailbox', $fDelete);
 
在该行下面增加下面4行:
$userarray=explode("@",$fDelete);
$user=$userarray[0];
$domain=$userarray[1];
system("sudo /usr/local/bin/maildir-deletion.sh $domain $user");

4、配置Postfix
修改/etc/postfix/main.cf文件:
 
基本配置
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = localhost
mynetworks_style = host
 
虚拟域名配置
# ADDRESS REDIRECTION (VIRTUAL DOMAIN)
#
# The VIRTUAL_README document gives information about the many forms
# of domain hosting that Postfix supports.
# See: http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-ubuntu8.04-p2
# The follwing lines connect Postfix with the MySQL database that contains information about
# the virtual users/accounts hosted. See proxymap(8) virtual(5) and mysql_table(5)
#
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql_virtual_domains_maps.cf
#
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql_virtual_alias_maps.cf
#
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
#
# Additional for quota support
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, this user has exceeded their disk space quota, please try again later.
virtual_overquota_bounce = yes
#
#Specify the user/group that owns the mail folders. I'm not sure if this is strictly necessary when using Dovecot's LDA.
virtual_uid_maps = static:2000
virtual_gid_maps = static:2000
#
#Specifies which tables proxymap can read: http://www.postfix.org/postconf.5.html#proxy_read_maps
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps
 

创建Mysql脚本
[root@mail ~]# vim /etc/postfix/mysql_virtual_domains_maps.cf
 
user = admin
password = 123123
hosts = localhost
dbname = mail
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
#optional query to use when relaying for backup MX
#query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1'

[root@mail ~]# vim /etc/postfix/mysql_virtual_alias_maps.cf
 
user = admin
password = 123123
hosts = localhost
dbname = mail
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
 
[root@mail ~]# vim /etc/postfix/mysql_virtual_mailbox_maps.cf
user = admin
password = 123123
hosts = localhost
dbname = mail
query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1'

[root@mail ~]# vim /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
 
user = admin
password = 123123
hosts = localhost
dbname = mail
query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'

SMTP加密设定
 
#SASL SUPPORT FOR CLIENTS
#
# The following options set parameters needed by Postfix to enable
# SMTP AUTH support using Dovecot's SASL component for authentication of mail clients.
# See: /usr/share/doc/postfix-2.3.3/README_FILES/SASL_README - http://www.postfix.org/SASL_README.html
# And /usr/share/doc/dovecot-1.0.7/wiki/HowTo.PostfixAndDovecotSASL.txt - http://wiki.dovecot.org/HowTo/PostfixAndDovecotSASL
#
# Turns on sasl authorization
smtpd_sasl_auth_enable = yes
#
#Use dovecot for authentication
smtpd_sasl_type = dovecot
#
# Path to UNIX socket for SASL
smtpd_sasl_path = /var/run/dovecot/auth-client
#
#Disable anonymous login. We don't want to run an open relay for spammers.
smtpd_sasl_security_options = noanonymous
#
#Adds support for email software that doesn't follow RFC 4954.
#This includes most versions of Microsoft Outlook before 2007.
broken_sasl_auth_clients = yes
#
smtpd_recipient_restrictions =  permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination

使用Dovecot做为投递
# TRANSPORT MAP
#
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1
 
修改master.cf文件
[root@mail ~]# vim /etc/postfix/master.cf
dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
 
 
5、配置Dovecot

因为配置文件比较分散,我把需要修改的配置文件的内容列出来
[root@mail ~]# vim /etc/dovecot/dovecot.conf
protocols = imap pop3
listen = *
dict {
  quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
}
!include conf.d/*.conf

[root@mail ~]# vim /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login cram-md5
!include auth-sql.conf.ext

[root@mail ~]# vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:%hMaildir
mbox_write_locks = fcntl

[root@mail ~]# vim /etc/dovecot/conf.d/10-master.conf
service imap-login {
  inet_listener imap {
  }
  inet_listener imaps {
  }
}
service pop3-login {
  inet_listener pop3 {
  }
  inet_listener pop3s {
  }
}
service lmtp {
  unix_listener lmtp {
  }
}
service imap {
}
service pop3 {
}
service auth {
  unix_listener auth-userdb {
    mode = 0600
    user = vmail
    group = vmail
  }
  unix_listener auth-client {
    mode = 0600
    user = postfix
    group = postfix
  }
}
service auth-worker {
}
service dict {
  unix_listener dict {
    mode = 0600
    user = vmail
    group = vmail
  }
}

[root@mail ~]# vim /etc/dovecot/conf.d/15-lda.conf
protocol lda {
  mail_plugins = quota
  postmaster_address = postmaster@example.com
}

[root@mail ~]# vim /etc/dovecot/conf.d/20-imap.conf
protocol imap {
        mail_plugins = quota imap_quota
}

[root@mail ~]# vim /etc/dovecot/conf.d/20-pop3.conf
protocol pop3 {
  pop3_uidl_format = %08Xu%08Xv
  mail_plugins = quota
}

[root@mail ~]# vim /etc/dovecot/conf.d/90-quota.conf
plugin {
  quota_rule = *:storage=1G
}
plugin {
}
plugin {
  quota = dict:User quota::proxy::quota
}
plugin {
}

[root@mail ~]# vim /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=localhost dbname=mail user=admin password=123123
default_pass_scheme = CRAM-MD5
user_query = SELECT CONCAT('/var/vmail/', maildir) AS home, 2000 AS uid, 2000 AS gid, CONCAT('*:bytes=', quota) as quota_rule FROM mailbox WHERE username = '%u' AND active='1'
password_query = SELECT username AS user, password, CONCAT('/var/vmail/', maildir) AS userdb_home, 2000 AS userdb_uid, 2000 AS userdb_gid, CONCAT('*:bytes=', quota) as userdb_quota_rule FROM mailbox WHERE username = '%u' AND active='1'

[root@mail ~]# vim /etc/dovecot/dovecot-dict-sql.conf.ext
connect = host=localhost dbname=mail user=admin password=123123
map {
  pattern = priv/quota/storage
  table = quota2
  username_field = username
  value_field = bytes
}
map {
  pattern = priv/quota/messages
  table = quota2
  username_field = username
  value_field = messages
}

6、测试SMTP与POP3服务
创建虚拟域

基于Centos6.2 X64系统下的邮件系统(一)_postfix_04 

创建邮箱

基于Centos6.2 X64系统下的邮件系统(一)_CentOS_05 


连接25端口
[root@mail ~]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.example.com ESMTP Postfix
ehlo example.com
250-mail.example.com
250-PIPELINING
250-SIZE 20480000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN CRAM-MD5
250-AUTH=PLAIN LOGIN CRAM-MD5
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

连接110端口
[root@mail ~]# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user test@example.com
+OK
pass test
+OK Logged in.

已经自动创建邮箱
[root@mail ~]# ll /var/vmail/example.com/test/Maildir/
total 40
drwx------. 2 vmail vmail  4096 Apr 26 05:44 cur
-rw-------. 1 vmail vmail 16384 Apr 26 05:43 dovecot.index.cache
-rw-------. 1 vmail vmail   608 Apr 26 05:44 dovecot.index.log
-rw-------. 1 vmail vmail    95 Apr 26 05:43 dovecot-uidlist
-rw-------. 1 vmail vmail     8 Apr 26 05:43 dovecot-uidvalidity
-r--r--r--. 1 vmail vmail     0 Apr 26 05:43 dovecot-uidvalidity.4f986fe9
drwx------. 2 vmail vmail  4096 Apr 26 05:44 new
drwx------. 2 vmail vmail  4096 Apr 26 05:43 tmp

收发邮件正常

基于Centos6.2 X64系统下的邮件系统(一)_CentOS_06