最近公司需要一台邮件服务器,在查阅了大量的资料以后决定采用Postfix + Dovecot + MySQL来搭建这套邮件系统。由于仅仅需要通过客户端来收发邮件,因此不安装Postfixadmin这类WEB管理工具。
系统环境
操作系统:CentOS 5.5 x64
安装需要的包
更新系统
- yum update
YUM安装Postfix,Dovecot,MySQL
CentOS base和update repository的Postfix版本不包括对MySQL的支持,我们需要从CentOS plus repository安装Postfix,在使用YUM安装之前,先修改etc/yum.repos.d/CentOS-Base.repo文件
- [base]
- name=CentOS-$releasever - Base
- ...
- exclude=postfix*
- #released updates
- [updates]
- name=CentOS-$releasever - Updates
- ...
- exclude=postfix*
YUM安装所需软件包
- yum --enablerepo=centosplus install postfix
- yum install dovecot mysql-server
配置MySQL
设置MySQL开机启动
- chkconfig mysqld on
- service mysqld start
设置root密码,删除匿名账号,删除test库
- mysql_secure_installation
创建Virtual Domains和Users
登陆MySQL,创建postfix数据库
- CREATE DATABASE postfix
创建MySQL账号,并赋权限
- GRANT SELECT, INSERT, UPDATE, DELETE ON postfix.* TO 'postfix_admin'@'localhost' IDENTIFIED BY 'postfix_admin_password';
- GRANT SELECT, INSERT, UPDATE, DELETE ON postfix.* TO 'postfix_admin'@'localhost.locahdomain' IDENTIFIED BY 'postfix_admin_password';
- FLUSH PRIVILEGES;
创建表结构
- CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );
- CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );
- CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
- CREATE TABLE transport ( domain varchar(128) NOT NULL default '', transport varchar(128) NOT NULL default '', UNIQUE KEY domain (domain) );
配置Postfix
创建virtual domain配置文件/etc/postfix/mysql-virtual_domains.cf
- user = postfix_admin
- password = postfix_admin_password
- dbname = postfix
- query = SELECT domain AS virtual FROM domains WHERE domain='%s'
- hosts = 127.0.0.1
创建virtual forwarding配置文件/etc/postfix/mysql-virtual_forwardings.cf
- user = postfix_admin
- password = postfix_admin_password
- dbname = postfix
- query = SELECT destination FROM forwardings WHERE source='%s'
- hosts = 127.0.0.1
创建virtual mailbox配置文件/etc/postfix/mysql-virtual_mailboxes.cf
- user = postfix_admin
- password = postfix_admin_password
- dbname = postfix
- query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
- hosts = 127.0.0.1
创建virtual email配置文件/etc/postfix/mysql-virtual_email2email.cf
- user = postfix_admin
- password = postfix_admin_password
- dbname = postfix
- query = SELECT email FROM users WHERE email='%s'
- hosts = 127.0.0.1
给配置文件设置合适的权限
- chmod o= /etc/postfix/mysql-virtual_*.cf
- chgrp postfix /etc/postfix/mysql-virtual_*.cf
创建处理email的用户和组,所有的邮件都会存放在该用户的home目录下
- groupadd -g 5000 vmail
- useradd -g vmail -u 5000 vmail -d /home/vmail -m
执行以下命令完成Postfix的配置
- postconf -e 'myhostname = server.example.com'
- postconf -e 'mydestination = $myhostname, localhost, localhost.localdomain'
- postconf -e 'mynetworks = 127.0.0.0/8'
- postconf -e 'inet_interfaces = all'
- postconf -e 'message_size_limit = 30720000'
- postconf -e 'virtual_alias_domains ='
- postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf'
- postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf'
- postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf'
- postconf -e 'virtual_mailbox_base = /home/vmail'
- postconf -e 'virtual_uid_maps = static:5000'
- postconf -e 'virtual_gid_maps = static:5000'
- postconf -e 'smtpd_sasl_type = dovecot'
- postconf -e 'smtpd_sasl_path = private/auth'
- postconf -e 'smtpd_sasl_auth_enable = yes'
- postconf -e 'broken_sasl_auth_clients = yes'
- postconf -e 'smtpd_sasl_authenticated_header = yes'
- postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'
- postconf -e 'smtpd_use_tls = yes'
- postconf -e 'smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem'
- postconf -e 'smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem'
- postconf -e 'virtual_create_maildirsize = yes'
- postconf -e 'virtual_maildir_extended = yes'
- postconf -e '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'
- postconf -e 'virtual_transport = dovecot'
- postconf -e 'dovecot_destination_recipient_limit = 1'
编辑/etc/postfix/master.cf文件,在文件底部增加dovecot服务
- dovecot unix - n n - - pipe
- flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}
停止sendmail,启动postfix
- service sendmail stop
- chkconfig sendmail off
- chkconfig postfix on
- service postfix start
Postfix配置到这里就完成了
配置Dovecot
Dovecot的配置文件是/etc/dovecot.conf,在编辑之前我们先备份一下
- cp -a /etc/dovecot.conf /etc/dovecot.conf-backup
修改/etc/dovecot.conf,修改过后的配置文件应该是这样的
- grep -v "#" /etc/dovecot.conf | sed "/^\s*$/d"
- protocols = imap imaps pop3 pop3s
- log_timestamp = "%Y-%m-%d %H:%M:%S "
- mail_location = maildir:/home/vmail/%d/%n/Maildir
- ssl_cert_file = /etc/pki/dovecot/certs/dovecot.pem
- ssl_key_file = /etc/pki/dovecot/private/dovecot.pem
- namespace private {
- separator = .
- prefix = INBOX.
- inbox = yes
- }
- protocol lda {
- log_path = /home/vmail/dovecot-deliver.log
- auth_socket_path = /var/run/dovecot/auth-master
- postmaster_address = [email protected]
- }
- protocol pop3 {
- pop3_uidl_format = %08Xu%08Xv
- }
- auth default {
- user = root
- passdb sql {
- args = /etc/dovecot-sql.conf
- }
- userdb static {
- args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
- }
- socket listen {
- master {
- path = /var/run/dovecot/auth-master
- mode = 0600
- user = vmail
- }
- client {
- path = /var/spool/postfix/private/auth
- mode = 0660
- user = postfix
- group = postfix
- }
- }
- }
由于用户名和密码是存放在MySQL中的,因此我们需要创建配置文件/etc/dovecot-sql.conf
- driver = mysql
- connect = host=127.0.0.1 dbname=postfix user=postfix_admin password=postfix_admin_password
- default_pass_scheme = CRYPT
- password_query = SELECT email as user, password FROM users WHERE email='%u';
更改/etc/dovecot-sql.conf权限,只允许dovecot组访问
- chgrp dovecot /etc/dovecot-sql.conf
- chmod o= /etc/dovecot-sql.conf
启动Dovecot
- chkconfig dovecot on
- service dovecot start
Dovecot配置完成
配置别名
编辑/etc/aliases
增加以下两行内容:
- postmaster: root
- root: [email protected]
执行以下命令使新的别名生效,并重启Postfix
- newaliases
- service postfix restart
测试Postfix和Dovecot
测试Postfix
使用telnet测试Postfix
- telnet localhost 25
连接到Postfix后输入
- ehlo localhost
Postfix工作正常的话,输出是
- Trying 127.0.0.1...
- Connected to localhost.
- Escape character is '^]'.
- 220 plato.example.com ESMTP Postfix
- ehlo localhost
- 250-plato.example.com
- 250-PIPELINING
- 250-SIZE 30720000
- 250-VRFY
- 250-ETRN
- 250-STARTTLS
- 250-AUTH PLAIN
- 250-AUTH=PLAIN
- 250-ENHANCEDSTATUSCODES
- 250-8BITMIME
- 250 DSN
测试Dovecot
使用telnet测试Dovecot
- telnet localhost pop3
Dovecot正常工作的话,输出应该是
- Trying 127.0.0.1...
- Connected to localhost.localdomain.
- Escape character is '^]'.
- +OK Dovecot ready.
当Postfix和Dovecot测试都通过以后,我们接下来应该建立域和邮箱用户了
建立域和邮箱用户
在建立域之前,首先需要确认你的DNS服务器做了正确的MX解析,DNS做好之后,我们登录MySQL插入域和邮箱用户的记录
- mysql -u root -p
- USE mail;
- INSERT INTO domains (domain) VALUES ('example.com');
- INSERT INTO users (email, password) VALUES ('[email protected]', ENCRYPT('password'));
测试MailBox
域和邮箱建立好之后,我们试着给用户发送一封测试邮件
- mailx [email protected]
为了确认是否收到邮件,我们进入邮箱目录/home/vmail/example.com/sales/Maildir,输入find命令
- find
应该能够看到以下输出
- .
- ./dovecot-uidlist
- ./cur
- ./new
- ./new/1285609582.P6115Q0M368794.li172-137
- ./dovecot.index
- ./dovecot.index.log
- ./tmp
接下来可以使用Foxmail,Outlook之类的客户端工具来收发邮件了。
排错
在安装过程中有问题,可以通过以下日志文件来查看日志,排查问题
- /var/log/maillog
- /home/vmail/dovecot-deliver.log