当前位置: > Linux服务器 > mail >

CentOS服务器上搭建企业电子邮件系统

时间:2014-12-03 09:10来源:linux.it.net.cn 作者:IT
CentOS 6.5 X64下搭建企业电子邮件系统 (postfix + postfixadmin + dovecot)

环境介绍:
软件包版本
--------------------------------------
CentOS 6.5 X64 
postfixadmin-2.91.tar.gz
postfix-2.6.6-6.el6_5.i686 (yum)
postfix-2.11.1.tar.gz (Traball)
dovecot-2.0.9-7.el6_5.1.i686
--------------------------------------
域名及主机名
--------------------------------------
Domain:vqiu.cn
Hostname:mail.vqiu.cn
--------------------------------------
数据库信息
--------------------------------------
Mysql(postfixadmin)
DBNAME ===> postfixadmin
user         ===> postfix
password ===> postfixadmin
--------------------------------------


 
一、环境部署预备

1、域名及主机名设置
略……
 
2、配置新yum 源(网易)
[root@mail ~]# yum -y install wget
[root@mail ~]# cd /etc/yum.repos.d/
[root@mail yum.repos.d]# mkdir backup
[root@mail yum.repos.d]# mv CentOS-* backup/
[root@mail yum.repos.d]# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
//此步骤可选,目的为今后yum安装amavisd-new、clamav 等
[root@mail ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
//清空yum缓存及更新系统
[root@mail ~]# yum clean all; yum update -y 

3、LAMP 环境
[root@mail ~]# yum -y install httpd mysql mysql-devel mysql-server php php-pecl-Fileinfo php-mcrypt php-devel php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc pcre pcre-devel
//配置apache,并让apache支持php
[root@mail ~]# vi /etc/httpd/conf/httpd.conf
AddType application/x-httpd-php .php        #添加对php程序的支持
PHPIniDir "/etc/php.ini"                            #指定php.ini配置文件路径
DirectoryIndex index.php index.html          #指定首页文件类型
#取消ServerName前的注释,否则启动httpd 时提示  276行

4、配置mysql,为postfixadmin 准备好数据库及认证用户
[root@mail ~]# mysql -uroot -p
mysql> create database postfix character set utf8;
mysql> grant all privileges on postfix.* to postfix@localhost identified by 'postfixadmin';
mysql> flush privileges;
mysql> exit

5、添加邮件虚拟用户 vmail
[root@mail ~]# useradd -u 2000 -d /var/vmail -c "Virtual Mail user" -s /sbin/nologin vmail


二、配置postfix 
[root@mail ~]# vi /etc/postfix/main.cf

################# Main configure ####################
hostname = mail.vqiu.cn
mydomain = vqiu.cn
myorigin = $mydomain
inet_interfaces = all
#注意这里,不能设置成与hostname与mydomain一致,不然收不到邮件
mydestination = localhost
mynetworks = 127.0.0.0/8
#不对client进行反向域名解析
disable_dns_lookups = yes
#允许对一个目标域名发送的邮件并发数为20个
default_destination_concurrency_limit = 20
#邮件的并发进程限制(根据服务器的性能定义)
default_process_limit = 50
#只监听ipv4
net_interfaces = ipv4

################## Virtual mail configure #######################
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
virtual_mailbox_base = /var/vmail
#注意uid 与 gid 与之前的建立的vmail账号是一致的,可以用# id vmail查询
virtual_uid_maps = static:2000
virtual_gid_maps = static:2000
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1
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

################ SASL SUPPORT FOR CLIENTS #######################
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_security_options = noanonymous
#以下的两个选项可以任选一个,不过必须要与dovecot 一致 
#smtpd_sasl_path = /var/run/dovecot/auth-client
smtpd_sasl_path = private/auth
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

################## Mail Quota configure #######################
#mail transport max size:20M
message_size_limit = 20480000
# 虚拟邮件用户使用的最大容量,需在postfixadmin 中启用quota($CONF['used_quotas'] = 'YES';)才生效。
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf

# Queue lifetime control
maximal_queue_lifetime = 1d
bounce_queue_lifetime = 1d

 
[root@mail ~]# vi /etc/postfix/master.cf
#在末尾加入LDA
dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient

[root@mail ~]# vi /etc/postfix/mysql_virtual_domains_maps.cf
user = postfix
password = postfixadmin
hosts = localhost
dbname = postfix
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 ~]# vi /etc/postfix/mysql_virtual_alias_maps.cf
user = postfix
password = postfixadmin
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

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

[root@mail ~]# vi /etc/postfix/master.cf
user = postfix
password = postfixadmin
hosts = localhost
dbname = postfix
query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1'

 
三、配置postfixadmin 
[root@mail ~]# wget http://jaist.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.91/postfixadmin-2.91.tar.gz
[root@mail ~]# tar zxvf postfixadmin-2.91.tar.gz
[root@mail ~]#  mv postfixadmin-2.91 postfixadmin
[root@mail ~]#  mv postfixadmin /var/www/html/
[root@mail postfixadmin]# cp config.inc.php config.inc.php.bak
[root@mail postfixadmin]# vi config.inc.php
$CONF['configured'] = true;
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = 'postfixadmin';
$CONF['database_name'] = 'postfix';
# 定义doveadm的位置,可以用#whereis doveadm查询doveadm 的位置
$CONF['dovecotpw'] = "/usr/bin/doveadm pw";
# 定义管理员邮箱
$CONF['admin_email'] = 'postmaster@vqiu.cn';
# mysql密码加密类型
$CONF['encrypt'] = 'dovecot:CRAM-MD5';
# 使用空间配额
$CONF['used_quotas'] = 'YES';
# 密码策略
$CONF['password_validation'] = array(
保存,通过web访问:http://yourip/postfixadmin/setup.php
 
将生成的 函数添加到config.inc.php文件末尾中,添加个管理员账号:比如postmaster@vqiu.cn (添加完成后建议将setup.php这个文件进行更名,或者直接删除)

在postfixadmin添加vqiu.cn域,以及两个测试用户zhang3、li4.

 
 
四、配置dovecot
安装
[root@mail ~]#  yum -y install dovecot dovecot-devel dovecot-mysql
#主要配置
[root@vqiu ~]# dovecot -n
# 2.0.9: /etc/dovecot/dovecot.conf
# OS: Linux 2.6.32-431.20.3.el6.x86_64 x86_64 CentOS release 6.5 (Final) ext4
auth_mechanisms = plain login cram-md5
dict {
  quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
}
disable_plaintext_auth = no
listen = *
mail_location = maildir:/var/vmail/%d/%n
mbox_write_locks = fcntl
passdb {
  args = /etc/dovecot/dovecot-sql.conf.ext
  driver = sql
}
plugin {
  quota = dict:User quota::proxy::quota
  quota_rule = *:storage=1G
}
postmaster_address = postmaster@vqiu.cn
service auth {
  unix_listener /var/spool/postfix/private/auth {
    group = postfix
    mode = 0666
    user = postfix
  }
  unix_listener auth-client {
    group = postfix
    mode = 0600
    user = postfix
  }
  unix_listener auth-userdb {
    group = vmail
    mode = 0600
    user = vmail
  }
}
service dict {
  unix_listener dict {
    group = vmail
    mode = 0600
    user = vmail
  }
}
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
userdb {
  args = /etc/dovecot/dovecot-sql.conf.ext
  driver = sql
}
protocol lda {
  mail_plugins = quota
}
protocol pop3 {
  mail_plugins = quota
  pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
  pop3_uidl_format = %08Xu%08Xv

 
[root@mail ~]# vi /etc/dovecot/dovecot-dict-sql.conf.ext
connect = host=localhost dbname=postfix user=postfix password=postfixadmin
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
}
[root@mail ~]# vi /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=localhost dbname=postfix user=postfix password=postfixadmin
#与postfixadmin 中的要一致,否则出现密码不匹配的现象
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'
 
五、客户端测试

 [内部互发,注意message_limit_size哦]


 [发送至公网]

六、常见问题

Q:邮件发送正常,无法接收邮件

A:这个问题我也遇到过最多的,总结如下:
  • main.cf 中的mydetination 参数不能与myorigin相同;
  • virtual_tranport = dovecot ,网上有些教程是virtual;
  • vmail 的GID与UID 为2000
  • /var/vmail 权限问题,日志里会提示

Q:传输附件失败
A:没有定义最大邮件传递大小,postfix 默认的传递大小查询:
[root@mail ~]# postconf | grep message_size_limit
message_size_limit = 10240000   #默认为10M
#编辑main.cf 定义合适邮件传输的大小

#查询数据库中的数据
[root@mail ~]# postmap -q vqiu.cn mysql:/etc/postfix/mysql_virtual_domains_maps.cf
vqiu.cn
[root@mail ~]# postmap -q zhang3@vqiu.cn mysql:/etc/postfix/mysql_virtual_postfixbox_maps.cf
vqiu.cn/vqiu.cn/zhang3/


附: postfix-2.11.1.tar.gz tarball 安装
[root@mail src]# tar zxvf postfix-2.11.1.tar.gz 
[root@mail src]# cd postfix-2.11.1
#注意mysql 路径
[root@mail postfix-2.11.1]# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DDEF_SERVER_SASL_TYPE=\"dovecot\"' 'AUXLIBS=-L/usr/lib/mysql/ -lmysqlclient -lz -lm -lssl -lcrypto';make;make install 

dovecot-2.2.13.tar.gz tarball 安装
[root@mail src]# ./configure --prefix=/usr/local/dovecot --sysconfdir=/etc --localstatedir=/var --with-sql --with-mysql --with-zlib --with-ssl LDFLAGS=-L/usr/lib/mysql/;make;make install
#可不执行,定义postfixadmin的doveadm 路径即可
[root@mail ~]# ln -sv /usr/local/dovecot/bin/doveadm /usr/bin/doveadm    (postfixadmin)
#可不执行,定义postfix中master.cf配置文件中LDA路径即可 
[root@mail ~]# ln -s /usr/local/dovecot/libexec/dovecot/ /usr/libexec/ 



Bug: 由于Mairbox没有设置对应的相关目录(比如:.Sent[发件箱] .Trafts[草稿箱] .Sapms [垃圾邮件]等),客户端默认使用IMAP协议来接收邮件时,只存在一个收件箱及草稿箱,其它的目录需要手动建立(暂时没有好的思路),写了个简单得不能再简单的SHELL脚本。

#!/bin/bash
for i in .Drafts .Sent .Trash .Tempates .Spams;do
    mkdir /var/vmail/$1/$2/$i -pv;    
done
chown vmail:vmail /var/vmail/$1 -R;

例如:给zhang3建立相关的IMAP目录
shell> sh shell_script vqiu.cn zhang3

(责任编辑:IT)
------分隔线----------------------------