邮件系统架设-postfix+dovecot+mysql+postfixadmin+roundcube

有运维或运维开发方面的需求,可以联系博主QQ 452336092或Email:admin#centos.bz(收费)

文章目录
[隐藏]

这篇文章介绍一个实现Web收发的邮件系统方案,利用postfix作为SMTP服务器,实现邮件的发送与接收,使用dovecot作为IMAP服务器,roundcube作为web MUA(邮件用户代理),实现web在线邮件发送与接收。而postfix的虚拟域名和虚拟用户则由postfixadmin管理。

系统及软件环境

系统:centos-5.8 32位
软件:postfix-2.8.7,dovecot-1.0.7,apache-2.2.22,php-5.2.17,mysql-5.1.58,postfixadmin-2.3.5,roundcubemail-0.7.2
注意:本教程并不通用,使用不同的软件版本可能配置方法不一样, 请注意。

安装LAMP

1、第三方库添加
由于postfixadmin要求php-5.2以上的版本,且我们这里使用yum安装lamp,默认的版本是5.1,所以需要添加第三方的rpm库安装php 5.2版本。

  1. rpm --import http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
  2. vi /etc/yum.repos.d/CentOS-Base.repo

加入:

  1. [utterramblings]
  2. name=Jason's Utter Ramblings Repo
  3. baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
  4. enabled=1
  5. gpgcheck=1
  6. gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka

2、开始安装

  1. yum -y install httpd mysql mysql-devel mysql-server php php-pecl-Fileinfo php-mcrypt php-devel php-mysql hp-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc pcre pcre-devel

3、启动服务

  1. service httpd start
  2. service mysqld start
  3. mysqladmin -uroot password 'mysql密码'

安装postfix

由于centos自带的postfix不支持mysql,所以只能选择编译安装。
首先删除自带的sendmail

  1. rpm -e sendmail
  1. yum install db4-devel
  2. groupadd -g 1001 postfix
  3. groupadd postdrop
  4. useradd -M -u 1001 -g postfix  -s /sbin/nologin postfix
  5. cd /tmp
  6. wget http://postfix.energybeam.com/source/official/postfix-2.8.7.tar.gz
  7. tar xzf postfix-2.8.7.tar.gz
  8. cd postfix-2.8.7
  9. make -f Makefile.init makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql'   'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm'
  10. make && make install

简单配置一下postfix:

  1. vi /etc/postfix/main.cf

添加如下代码:

  1. virtual_mailbox_base = /var/mail/vmail
  2. virtual_uid_maps = static:1001
  3. virtual_gid_maps = static:1001

1001分别为postfix用户和postfix用户组的id

安装dovecot

  1. yum -y install dovecot

安装postfixadmin

  1. cd /tmp
  2. wget http://softlayer.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.3.5/postfixadmin-2.3.5.tar.gz
  3. tar xzf postfixadmin-2.3.5.tar.gz -C /var/www/html
  4. mv /var/www/html/postfixadmin-2.3.5 /var/www/html/postfixadmin
  5. cd /var/www/html/postfixadmin
  6. mysql -uroot -p
  7. mysql > CREATE DATABASE postfix;
  8. mysql > CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'postfix_password';
  9. mysql > GRANT ALL PRIVILEGES ON `postfix` . * TO 'postfix'@'localhost';

postfix_password替换成自己的。
修改配置文件:

  1. vi config.inc.php

找到下面的参数并作修改:

  1. $CONF['configured'] = true;
  2. $CONF['database_password'] = 'postfix_password';
  3. $CONF['default_language'] = 'cn';
  4. $CONF['used_quotas'] = 'YES';
  5. $CONF['quota'] = 'YES';

postfix_password即上一步设置的postfix用户密码。
打开浏览器,输入下面的网址开始安装:
http://your-domain/postfixadmin/setup.php

postfix与postfixadmin整合

  1. vi /etc/postfix/main.cf

按如下添加或修改:

  1. virtual_mailbox_domains = proxy:mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf
  2. virtual_alias_maps =
  3.    proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf,
  4.    proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf,
  5.    proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf
  6. virtual_mailbox_maps =
  7.    proxy:mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf,
  8.    proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf
  9.   
  10. # Additional for quota support
  11. virtual_create_maildirsize = yes
  12. virtual_mailbox_extended = yes
  13. virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf
  14. virtual_mailbox_limit_override = yes
  15. virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later.
  16. virtual_overquota_bounce = yes

建立目录sql

  1. mkdir /etc/postfix/sql

以下的几个文件注意修改postfix_password。

  1. vi /etc/postfix/sql/mysql_virtual_alias_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
  6. #expansion_limit = 100
  1. vi /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
  1. vi /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

内容:

  1. # handles catch-all settings of target-domain
  2. user = postfix
  3. password = postfix_password
  4. hosts = localhost
  5. dbname = postfix
  6. query  = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
  1. vi /etc/postfix/sql/mysql_virtual_domains_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query          = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
  6. #query          = SELECT domain FROM domain WHERE domain='%s'
  7. #optional query to use when relaying for backup MX
  8. #query           = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1'
  9. #expansion_limit = 100
  1. vi /etc/postfix/sql/mysql_virtual_mailbox_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query  = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
  6. #expansion_limit = 100
  1. vi /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
  1. vi /etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf

内容:

  1. user = postfix
  2. password = postfix_password
  3. hosts = localhost
  4. dbname = postfix
  5. query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'

dovecot与postfixadmin整合

  1. vi /etc/dovecot.conf

按如下修改或添加:

  1. default_mail_env = maildir:/var/mail/vmail/%u/
  2.  
  3. auth default {
  4.   mechanisms plain
  5.   userdb sql {
  6.     # Path for SQL configuration file, see doc/dovecot-sql-example.conf
  7.     args = /etc/dovecot-mysql.conf
  8.   }
  9.   passdb sql {
  10.     # Path for SQL configuration file, see doc/dovecot-sql-example.conf
  11.     args = /etc/dovecot-mysql.conf
  12.   }
  13. }
  14.  
  15. # Valid UID range for users, defaults to 500 and above.
  16. first_valid_uid = 1001  # Change this to your postfix UID
  17.  
  18. ## IMAP quota
  19. protocol imap {
  20.   mail_plugins = quota imap_quota
  21. }
  22.  
  23. ## POP quota
  24. protocol pop3 {
  25.   mail_plugins = quota
  26. }
  27.  
  28. ## Local Delivery Agent
  29. protocol lda {
  30.   mail_plugins = quota
  31. }
  32.  
  33. ## Dictionary DB proxy
  34. dict {
  35.   quota = mysql:/etc/dovecot-dict-quota.conf
  36. }
  37.  
  38. ## Default quota values
  39. plugin {
  40. quota = dict:storage=200000 proxy::quota
  41. }

以下两个文件注意修改postfix_password。

  1. vi /etc/dovecot-mysql.conf

内容:

  1. connect = host=localhost dbname=postfix user=postfix password=postfix_password
  2. driver = mysql
  3.  
  4. # Default password scheme.
  5. # depends on your $CONF['encrypt'] setting:
  6. # md5crypt  -> MD5-CRYPT
  7. # md5       -> PLAIN-MD5
  8. # cleartext -> PLAIN
  9. default_pass_scheme = MD5-CRYPT
  10.  
  11. # Query to retrieve password. user can be used to retrieve username in other
  12. # formats also.
  13.  
  14. password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1'
  15.  
  16. # Query to retrieve user information.
  17.  
  18. user_query = SELECT maildir, 1001 AS uid, 1001 AS gid, CONCAT('dict:storage=',floor(quota/1000),' proxy::quota') as quota FROM mailbox WHERE username = '%u' AND active='1'
  1. vi /etc/dovecot-dict-quota.conf

内容:

  1. driver = mysql
  2. connect = host=localhost dbname=postfix user=postfix password=postfix_password
  3. default_pass_scheme = MD5-CRYPT
  4. table = quota
  5. select_field = current
  6. where_field = path
  7. username_field = username

启动服务

  1. postmap /etc/aliases
  2. /usr/sbin/postfix start
  3. service dovecot start

roundcube安装

  1. cd /tmp
  2. wget http://voxel.dl.sourceforge.net/project/roundcubemail/roundcubemail/0.7.2/roundcubemail-0.7.2.tar.gz
  3. tar xzf roundcubemail-0.7.2.tar.gz  -C /var/www/html
  4. mv /var/www/html/roundcubemail-0.7.2 /var/www/html/webmail
  5. mysql -uroot -p
  6. mysql> CREATE DATABASE roundcubemail;
  7. mysql> GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcube@localhost IDENTIFIED BY 'password';
  8. mysql> FLUSH PRIVILEGES;

现在你可以浏览器打开以下网址安装:
http://yourdomain/webmail/installer/
详细的roundcubemail安装说明可以参考:http://blog.dvxj.com/pandola/roundcube-install-config.html
这样就实现了完整的邮件系统功能,没有实现的是smtp的认证,即无法使用像outlook express客户端发送邮件,如有需要,可以搜索sasl方面的教程配置。
相关网站:
http://www.postfix.org/
http://www.dovecot.org/
http://postfixadmin.sourceforge.net/
http://roundcube.net/

转载请保留原文链接:Linux运维日志 » 邮件系统架设-postfix+dovecot+mysql+postfixadmin+roundcube

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^