FreeBSD6.3R 搭建实验邮件系统 - Postfix+Dovecot+Openwebmail

讨论其他BSD项目及其衍生品,如:FreeBSD,DragonFlyBSD,PC-BSD,DesktopBSD,FreeNAS,NAS4Free,m0n0wall,pfSense,pacBSD,OpenDarwin...
回复
头像
unreal
银 Ag
帖子: 1080
注册时间: 2010-06-07 18:52

FreeBSD6.3R 搭建实验邮件系统 - Postfix+Dovecot+Openwebmail

帖子 unreal » 2011-08-18 21:30

声明:由于年代较久,很多软件的版本已经更新,配置方法很可能已经不同,此文档可能毫无价值,请各位会员考量,以免浪费您的宝贵时间。

背景说明:这个邮件系统是我于2008年在某公司打工时期的一个技能考核科目,因为有邮件前端服务器(邮件网关)担负反垃圾邮件和杀毒,所以搭建时无需anti spam 和virus功能就能作为生产服务器上线;如果在木有邮件网关的环境下这个邮件系统只能作为实验性质,提供基本邮件功能,作教学练手之用。
=======================正文=======================
◆需求:
1. 要免費的
2. 要能轉移現在的 /etc/passwd 檔
3. 要能用 web Mail
4. 建在 BSD 上


◆实现方案:
为此选择的如下方案来搭建邮件系统:
---------------------------------------
OS: FreeBSD 6.3 Release
MTA: Postfix 2.5.4
MDA: Dovecot 1.1.3
SMTP AUTH: Dovecot SASL for Postfix
Web Server: Apache 2.2.9
Webmail: Openwebmail 2.53

---------------------------------------
以上皆为免费的开源软件,因为有功能完善的邮件网关,本邮件系统无需添加anti-spam/virus等配置,力求配置简洁和运行高效,使用系统账户进行登录验证,使用unix传统的mbox邮箱格式;POP/IMAP和SASL皆由dovecot担任;最重要的SMTP部分选用安全可靠灵活高效且兼容sendmail的Postfix;webmail选用在操作与使用方便上超过M$ Outlook 的 Openwebmail。整个邮件系统所需安装的软件达到最少,保持系统轻载高效:

代码: 全选

mail# pkg_info
apache-2.2.9_5      Version 2.2.x of Apache web server with prefork MPM.
autoconf-2.62       Automatically configure source code on many Un*x platforms
autoconf-wrapper-20071109 Wrapper script for GNU autoconf
dovecot-1.1.3_1     Secure and compact IMAP and POP3 servers
expat-2.0.1         XML 1.0 parser written in C
gettext-0.17_1      GNU gettext package
gmake-3.81_3        GNU version of 'make' utility
help2man-1.36.4_2   Automatically generating simple manual pages from program o
libiconv-1.11_1     A character set conversion library
libtool-1.5.26      Generic shared library support script
m4-1.4.11,1         GNU m4
openwebmail-2.53    A webmail system designed to manage big mail folder files e
p5-Authen-PAM-0.16_1 A Perl interface to the PAM library
p5-MIME-Base64-3.07 Perl5 module for Base64 and Quoted-Printable encodings
p5-Quota-1.6.2      Perl module that provides access to filesystem quotas
p5-Text-Iconv-1.5   Perl interface to iconv() codeset conversion function
p5-gettext-1.05_2   Message handling functions
pcre-7.7_1          Perl Compatible Regular Expressions library
perl-5.8.8_1        Practical Extraction and Report Language
postfix-2.5.4,1     A secure alternative to widely-used Sendmail
◆安装与配置:
◎1.最小化安装Freebsd 6.3 Release
因为所有收下的信件将会依照相关设定保存在/var/mail,所以/var 应分配较大的空间,最低不少于20GB。(如需配置成把信件放入用户的主目录,可创建/home 并分配较大空间。)
OS安装完毕,设置root口令、添加一个wheel组账号、设定网络参数、开启sshd后,照例要更新ports:

代码: 全选

csup -gL2 /usr/share/examples/cvsup/ports-supfile -h cvsup.freebsdchina.org
或者 portsnap fetch && portsnap update
但cvsup经常因网速缓慢导致耗时颇久,变通办法是直接从最近的主站抓取ports.tar.gz并释放到/usr,具体操作不再赘述。

◎2.禁用系统自带的sendmail

代码: 全选

#vi /etc/rc.conf
添加下列行:

代码: 全选

sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
禁掉sendmail的自动维护:

代码: 全选

#vi /etc/periodic.conf
添加下列行:

代码: 全选

daily_clean_hoststat_enable="NO"
daily_status_mail_rejects_enable="NO"
daily_status_include_submit_mailq="NO"
daily_submit_queuerun="NO"
◎3.安装配置Postfix

代码: 全选

#cd /usr/ports/mail/postfix
#make config
选择 PCRE、DOVECOT ,通过依赖关系一并安装Dovecot,perl。

代码: 全选

#make install clean
#rehash
设置开机自启动postfix:
在 /etc/rc.conf 中添加

代码: 全选

postfix_enable="YES"
替换掉系统自带的sendmail:

代码: 全选

#cp /usr/local/sbin/sendmail /usr/sbin/sendmail
第一次启动前需在DNS中添加相关的A记录和MX记录,并确认下是否已设置完整的主机名,本例为:

代码: 全选

#hostname
mail.szsn.net
如只有简名,则需设定postfix的myhostname参数:

代码: 全选

#postconf -e myhostname=mail.szsn.net
启动postfix:

代码: 全选

#postfix start
postfix/postfix-script: starting the Postfix mail system
配置文件:
postfix的配置文件都在 /usr/local/etc/postfix/ 目录下,最重要的两个文件是 master.cfmain.cf,每次修改文件后必须重新加载postfix才生效

代码: 全选

#postfix reload
通常情形下只需配置 main.cf 即可,在默认配置上修改下列项:

代码: 全选

mail_spool_directory = /var/spool/mail
修改为

代码: 全选

mail_spool_directory = /var/mail
(这条配置表明使用 mbox 格式将信件放在 /var/mail 目录下,如果想用 maildir 格式,须在目录名称之后加上 / 符号。)
然后注释掉 home_mailbox = Mailbox ,否则信件将会放到用户主目录下。
mboxmaildir的选择可能需视乎实际情况来测试,但就MDA的投递效率而言,mbox较快。
到此,postfix基本配置完成,reload & test:

代码: 全选

# telnet mail.szsn.net 25
Trying 192.168.1.167...
Connected to mail.szsn.net.
Escape character is '^]'.
220 mail.szsn.net ESMTP Postfix

头像
unreal
银 Ag
帖子: 1080
注册时间: 2010-06-07 18:52

帖子 unreal » 2011-08-18 21:33

啊啊,预览变成发帖了,还没写完那……×_×
==================继续继续'_'=================
◎4.安装配置Dovecot
Dovecot已经在前面安装Postfix时通过依赖关系安装。
设置开机启动:
/etc/rc.conf 中添加

代码: 全选

dovecot_enable="YES"
配置文件在/usr/local/etc/dovecot.conf

设定log档的路径

代码: 全选

log_path = /var/log/dovecot
由于采用 plain login 认证,需确认或修改以下项:

代码: 全选

disable_plaintext_auth = no
ssl_disable = yes
指定IMAP索引文件的位置与邮箱的位置:

代码: 全选

mail_location = mbox:~/mail/:INBOX=/var/mail/%u
到此Dovecot配置完成,start&test

代码: 全选

#/usr/local/etc/rc.d/dovecot start
Starting dovecot.

#telnet mail.szsn.net 110
Trying 192.168.1.167...
Connected to mail.szsn.net.
Escape character is '^]'.
+OK Dovecot ready.
◎5.配置 Dovecot SASL for Postfix
5.1 postfix的配置:

代码: 全选

vi /usr/local/etc/postfix/main.cf
添加下列行:

代码: 全选

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
重新加载postfix

代码: 全选

#postfix reload
postfix/postfix-script: refreshing the Postfix mail system
5.2 dovecot的配置:

代码: 全选

vi /usr/local/etc/dovecot.conf
逐项查找并修改下列项,中间夹杂许多说明注释项,修改时需注意有否遗漏:

代码: 全选

auth default {
      mechanisms = plain login
      passdb pam {
      }
      userdb passwd {
      }
      socket listen {
        client {
          # Assuming the default Postfix $queue_directory setting
          path = /var/spool/postfix/private/auth
          mode = 0660
          # Assuming the default Postfix user and group
          user = postfix
          group = postfix
        }
      }
    }
重启dovecot&test:

代码: 全选

# /usr/local/etc/rc.d/dovecot restart
Stopping dovecot.
Waiting for PIDS: 1773.
Starting dovecot.
由于本地不进行认证,需从另外的机器连接测试,以下从一台WinXP机器用命令行测试:

代码: 全选

c:\>telnet mail.szsn.net 25
220 mail.szsn.net ESMTP Postfix
helo 163.com
250 mail.szsn.net
mail from:<xxxxx@163.com>
250 2.1.0 Ok
rcpt to:<xxxx@sohu.com>
554 5.7.1 <xxxx@sohu.com>: Relay access denied

c:\>telnet mail.szsn.net 25
220 mail.szsn.net ESMTP Postfix
ehlo sohu.com
250-mail.szsn.net
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
◎6.webmail的安装配置
6.1 安装配置 apache

代码: 全选

#cd /usr/ports/www/apache22
#make config
在默认选项的基础上去掉 IPV6、SSL 以及与数据库有关的选项;确认选上 PCRE_FROM_PORTS、CGI

代码: 全选

#make install clean
#rehash
设置开机启动:
/etc/rc.conf 中添加

代码: 全选

apache22_enable="YES"
apache22_http_accept_enable="YES"
6.2 安装配置 openwebmail

代码: 全选

#cd /usr/ports/mail/openwebmail
#make config
只选 PAM、QUOTA

代码: 全选

#make install clean
6.3 配置apache和openwebmail:
apache的配置文件在 /usr/local/etc/apache22/httpd.conf
搜索并修改下列项:

代码: 全选

ServerName mail.szsn.net:80
DocumentRoot "/usr/local/www/"
<Directory "/usr/local/www/">
	Options ExecCGI
	AllowOverride All
	Order allow,deny
	Allow from all
</Directory>
DirectoryIndex /cgi-bin/openwebmail/openwebmail.pl
Alias /openwebmail "/usr/local/www/data/openwebmail"

注释掉 ScriptAlias /cgi-bin/ "/usr/local/www/apache22/cgi-bin/"

AddHandler cgi-script .cgi .pl
openwebmail的配置文件在 /usr/local/www/cgi-bin/openwebmail/etc/openwebmail.conf
修改确认下列项:

代码: 全选

domainnames             mail.szsn.net
auth_module             auth_unix.pl
mailspooldir            /var/mail
ow_cgidir               /usr/local/www/cgi-bin/openwebmail
ow_cgiurl               /cgi-bin/openwebmail
ow_htmldir              /usr/local/www/data/openwebmail
ow_htmlurl              /openwebmail
logfile                 /var/log/openwebmail.log
测试apache配置文件:

代码: 全选

#apachectl configtest
Syntax OK
启动和测试apache:

代码: 全选

#/usr/local/etc/rc.d/apache22 start
Performing sanity check on apache22 configuration:
Syntax OK
Starting apache22.

# telnet mail.szsn.net 80
Trying 192.168.1.167...
Connected to mail.szsn.net.
Escape character is '^]'.
登录openwebmail:
在客户端机器上打开浏览器,在地址栏输入 http://mail.szsn.net 回车:
图片
登入邮箱:
首次登入会先打开个人模板设定页面,可依据每人的使用情况设定,而后自动进入收件箱。openwebmail的界面友好,具体的操作一看便知。
图片
WindowsXP下MUA客户端设置:
图片
◆到此,邮件系统搭建完成。

(为保证图片与文字匹配,俺又无耻借用国内一位老兄的网站空间,給他的服务器增加负担啦~)

头像
leo
帖子: 2465
注册时间: 2010-01-21 3:27

帖子 leo » 2011-08-18 21:36

又见强文,顶一下。

头像
unreal
银 Ag
帖子: 1080
注册时间: 2010-06-07 18:52

帖子 unreal » 2011-08-18 22:31

惭愧,自2008以后再也木有弄过邮件服务器,都荒废了,刚才上帖过程中发现很多内容已经被我忘记的一干二净:<

回复

在线用户

正浏览此版面之用户: 没有注册用户 和 25 访客