设为首页 收藏本站
查看: 2301|回复: 0

[经验分享] 邮件系统服务器搭建记录(一)(Postfix+Cyrus-sasl+Courier-authlib+Dovecot+ExtMail+MySQL)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-7 08:49:43 | 显示全部楼层 |阅读模式
注:本文介绍的是有关软件的安装过程和配置方法,不涉及原理介绍。如要了解邮件系统的运行原理,请参考附件中的链接。文中涉及技术和资料来源于网络,非本人原创,本文仅供个人总结和学习参考。

首先介绍下搭建所需的软件和部署环境:
MTA: Postfix 3.0.3
SASL: Cyrus-sasl 2.1.23 ; Courier-authlib 0.66.1(Courier-authlib使用Cyrus-sasl库文件访问MySQL)
MDA: Dovecot 2.0.9
DataBase: MySQL 5.1.73
WebMail: ExtMail 1.2(普通用户界面); ExtMan 1.1(管理员界面)

Web服务器(用于发布WebMail程序): Apache 2.2.15
服务器操作系统版本:RHEL 6.5

下面介绍搭建步骤:
1.通过yum安装mysql服务器和开发包
由于postfix的编译安装的配置参数中需要链接MySQL的开发头文件、库文件和SASL的开发头文件,因此首先安装mysql-server和mysql-devel包:
1
2
[iyunv@mail postfix-3.0.3]# yum install mysql-server
[iyunv@mail postfix-3.0.3]# yum install mysql-devel




2.通过yum安装cyrus-sasl包和cyrus-sasl-devel包
1
2
[iyunv@mail postfix-3.0.3]# yum install cyrus-sasl
[iyunv@mail postfix-3.0.3]# yum install cyrus-sasl-devel




3.编译安装postfix

在RHEL6.5上,默认安装了postfix,并通过postfix来发送邮件:
1
2
3
4
5
6
7
8
[iyunv@mail ~]# netstat -tunlp | grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3510/master         
tcp        0      0 ::1:25                      :::*                        LISTEN      3510/master         
[iyunv@mail ~]# ps -ef | grep 3510
root      3510     1  0  2014 ?        00:01:11 /usr/libexec/postfix/master
postfix   3521  3510  0  2014 ?        00:00:15 qmgr -l -t fifo -u
postfix  11818  3510  0 20:41 ?        00:00:00 pickup -l -t fifo -u
root     11989 11958  0 21:43 pts/0    00:00:00 grep 3510




但是默认安装的postfix,其配置的属性参数可能不满足搭建邮件服务器的要求,因此本文建议将其卸载掉,重新通过编译安装包的方式进行安装。
首先停止服务,卸载postfix安装包:

1
2
3
[iyunv@mail ~]# service postfix stop
Shutting down postfix:                                     [  OK  ]
[iyunv@mail ~]# rpm -e postfix



然后去postfix的官网(http://www.postfix.org/)下载安装包并上传到服务器,解压并进行编译安装,在编译时注意配置mysql和cyrus-sasl所提供的头文件和库文件所在的路径:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@mail ~]# ls -l postfix-3.0.3.tar.gz
-rw-r--r--. 1 root root 4240588 Dec 20 09:35 postfix-3.0.3.tar.gz
[iyunv@mail ~]# tar -xf postfix-3.0.3.tar.gz
[iyunv@mail ~]# cd postfix-3.0.3/
[iyunv@mail postfix-3.0.3]# make makefiles 'CCARGS=-DHAS_MYSQL -I /usr/include/mysql \
-DUSE_SASL_AUTH \
-DUSE_CYRUS_SASL -I /usr/include/sasl \
-DUSE_TLS'\
'AUXLIBS= -L/usr/lib64/mysql\
-lmysqlclient \
-lz \
-lrt \
-lm \
-L/usr/lib64/sasl2 \
-lsasl2 \
-lssl \
-lcrypto'



之后执行make&&make install操作,在执行make install操作后,安装程序会进行交互,要求对postfix的运行参数进行一些配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
[iyunv@mail postfix-3.0.3]# make && make install
...
Please specify the prefix for installed file names. Specify this ONLY
if you are building ready-to-install packages for distribution to OTHER
machines. See PACKAGE_README for instructions.
install_root: [/]

Please specify a directory for scratch files while installing Postfix. You
must have write permission in this directory.
tempdir: [/root/postfix-3.0.3] /tmp/postfix

Please specify the final destination directory for installed Postfix
configuration files.
config_directory: [/etc/postfix]

Please specify the final destination directory for installed Postfix
administrative commands. This directory should be in the command search
path of adminstrative users.
command_directory: [/usr/sbin]

Please specify the final destination directory for installed Postfix
daemon programs. This directory should not be in the command search path
of any users.
daemon_directory: [/usr/libexec/postfix]

Please specify the final destination directory for Postfix-writable
data files such as caches or random numbers. This directory should not
be shared with non-Postfix software.
data_directory: [/var/lib/postfix]

Please specify the final destination directory for the Postfix HTML
files. Specify "no" if you do not want to install these files.
html_directory: [no]

Please specify the owner of the Postfix queue. Specify an account with
numerical user ID and group ID values that are not used by any other
accounts on the system.
mail_owner: [postfix]

Please specify the final destination pathname for the installed Postfix
mailq command. This is the Sendmail-compatible mail queue listing command.
mailq_path: [/usr/bin/mailq]

Please specify the final destination directory for the Postfix on-line
manual pages. You can no longer specify "no" here.
manpage_directory: [/usr/local/man]

Please specify the final destination pathname for the installed Postfix
newaliases command. This is the Sendmail-compatible command to build
alias databases for the Postfix local delivery agent.
newaliases_path: [/usr/bin/newaliases]

Please specify the final destination directory for Postfix queues.
queue_directory: [/var/spool/postfix]

Please specify the final destination directory for the Postfix README
files. Specify "no" if you do not want to install these files.
readme_directory: [no]

Please specify the final destination pathname for the installed Postfix
sendmail command. This is the Sendmail-compatible mail posting interface.
sendmail_path: [/usr/sbin/sendmail]

Please specify the group for mail submission and for queue management
commands. Specify a group name with a numerical group ID that is
not shared with other accounts, not even with the Postfix mail_owner
account. You can no longer specify "no" here.
setgid_group: [postdrop]

Please specify the final destination directory for Postfix shared-library
files.
shlib_directory: [no]

Please specify the final destination directory for non-executable files
that are shared among multiple Postfix instances, such as postfix-files,
dynamicmaps.cf, as well as the multi-instance template files main.cf.proto
and master.cf.proto.
meta_directory: [/etc/postfix]
Updating /etc/postfix/main.cf.proto...
...



上述过程中要求配置参数中,以下几个配置项需要注意:
mail_owner: postfix中邮件队列的所有者,要求是系统用户的UID,因此通常是在系统中新建一个用户名为postix的用户,并将用户名填入此项。
setgid_group: 需要设置成一个不同于mail_owner用户名不同的组名。
因此为了稳妥起见,可以新建2个普通用户/用户组postfix和postdrop,需要注意的是RHEL6.5上已存在postfix用户名/组,该用户/组的UID/GID小于500,为特权用户/组,请先删除该用户/组后再添加:
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@mail postfix-3.0.3]# userdel postfix
[iyunv@mail postfix-3.0.3]# userdel postdrop
[iyunv@mail postfix-3.0.3]# groupdel postfix
[iyunv@mail postfix-3.0.3]# groupdel postdrop
[iyunv@mail postfix-3.0.3]# groupadd -g 2525 postfix
[iyunv@mail postfix-3.0.3]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
[iyunv@mail postfix-3.0.3]# groupadd -g 2526 postdrop
[iyunv@mail postfix-3.0.3]# useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop
[iyunv@mail postfix-3.0.3]# id postfix
uid=2525(postfix) gid=2525(postfix) groups=2525(postfix),12(mail)
[iyunv@mail postfix-3.0.3]# id postdrop
uid=2526(postdrop) gid=2526(postdrop) groups=2526(postdrop)



请注意,在部署邮件服务器前,系统中已有部分文件/目录属主属组为postfix或postdrop,如果遇到相关文件/目录时,请将这些文件/目录的属主属组修改成新的postfix/postdrop的uid和gid。

4.配置postfix并测试邮件发送

postfix有两个主要的配置文件,master.cf和maincf,其中:
postfix的服务是由多个daemon进程来协调完成的,master.cf定义了为实现特定的要求,哪些进程可以运行,将以何种方式运行。
main.cf则定义了postfix运行的环境参数。
为保证postfix可以发送邮件,应对main.cf如下参数进行配置:
myhostname: 邮件系统主机名,应与操作系统的主机名相一致。
myorigin: 邮件后缀名
mydomain: 邮件域名
mydestination: 定义接收邮件的后缀名
mynetworks: 为其进行中继的网段
配置如下:
1
2
3
4
5
6
[iyunv@mail ~]# cat /etc/postfix/main.cf | grep "^my"
myhostname = mail.asika.com
mydomain = asika.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8    # 只给通过127.0.0.0,即只给来自本机的用户中继邮件



配置完成,重启postfix服务:
1
2
3
[iyunv@mail ~]# postfix stop;postfix start
postfix/postfix-script: stopping the Postfix mail system
postfix/postfix-script: starting the Postfix mail system



注:如果想通过SysV方式启停postfix服务,则可以搜索postfix SysV服务脚本,并使用chkconfig命令来添加postfix服务以及配置在对应的runlevel上启停postfix服务。

重启完postfix服务后,可以看到postfix服务默认监听在25端口上:
1
2
3
4
5
6
7
[iyunv@mail ~]# netstat -tunlp | grep master
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      22279/master        
[iyunv@mail ~]# ps -ef | grep 22279
root     22279     1  0 21:30 ?        00:00:00 /usr/libexec/postfix/master -w
postfix  22280 22279  0 21:30 ?        00:00:00 pickup -l -t unix -u
postfix  22281 22279  0 21:30 ?        00:00:00 qmgr -l -t unix -u
root     22406 22040  0 21:35 pts/1    00:00:00 grep 22279



使用telnet访问25号端口,可以对postfix发送邮件功能进行测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[iyunv@mail ~]# telnet 127.0.0.1 25  
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 mail.asika.com ESMTP Postfix
ehlo mail.asika.com
250-mail.asika.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:root
250 2.1.0 Ok
rcpt to:root
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject:hello
123
.
250 2.0.0 Ok: queued as 88B343AB



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@mail ~]# mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 1 messages 1 new 1 unread
>N  1 root@asika.com        Mon Jan  4 21:43  14/420   "hello"
& 1
Message  1:
From root@asika.com  Mon Jan  4 21:43:14 2016
Return-Path: <root@asika.com>
X-Original-To: root
Delivered-To: root@asika.com
subject:hello
Date: Mon,  4 Jan 2016 21:42:41 +0800 (CST)
From: root@asika.com
Status: R
123



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[iyunv@mail ~]# telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 mail.asika.com ESMTP Postfix
ehlo mail.asika.com
250-mail.asika.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:a@b.com
250 2.1.0 Ok
rcpt to:d@c.com
250 2.1.5 Ok
data   
354 End data with <CR><LF>.<CR><LF>
subject:hello
123
.
250 2.0.0 Ok: queued as CE65F35F



查看/var/log/maillog日志,发现postfix发送了邮件,当收件人地址在本地时,会将邮件投递到本地用户的mailbox中,如果收件人地址后缀非本机所在域名,会尝试访问地址后缀对应的网址,试图为该邮件进行中继:
1
2
3
4
5
6
7
8
9
10
[iyunv@mail ~]# tail /var/log/maillog
Jan  4 21:39:55 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1]
Jan  4 21:41:09 mail postfix/smtpd[22508]: disconnect from localhost[127.0.0.1] quit=1 commands=1
Jan  4 21:42:23 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1]
Jan  4 21:42:51 mail postfix/smtpd[22508]: 88B343AB: client=localhost[127.0.0.1]
Jan  4 21:43:14 mail postfix/cleanup[22648]: 88B343AB: message-id=<20160104134251.88B343AB@mail.asika.com>
Jan  4 21:43:14 mail postfix/qmgr[22281]: 88B343AB: from=<root@asika.com>, size=303, nrcpt=1 (queue active)
Jan  4 21:43:14 mail postfix/local[22667]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled
Jan  4 21:43:14 mail postfix/local[22667]: 88B343AB: to=<root@asika.com>, orig_to=<root>, relay=local, delay=33, delays=32/0.09/0/0.03, dsn=2.0.0, status=sent (delivered to mailbox)
Jan  4 21:43:14 mail postfix/qmgr[22281]: 88B343AB: removed



1
2
3
4
5
6
7
8
[iyunv@mail ~]# tail /var/log/maillog
Jan  4 21:30:38 mail postfix/master[22279]: daemon started -- version 3.0.1, configuration /etc/postfix
Jan  4 21:38:44 mail postfix/smtpd[22508]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled
Jan  4 21:38:44 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1]
Jan  4 21:39:11 mail postfix/smtpd[22508]: CE65F35F: client=localhost[127.0.0.1]
Jan  4 21:39:44 mail postfix/cleanup[22512]: CE65F35F: message-id=<20160104133911.CE65F35F@mail.asika.com>
Jan  4 21:39:44 mail postfix/qmgr[22281]: CE65F35F: from=<a@b.com>, size=299, nrcpt=1 (queue active)
Jan  4 21:39:44 mail postfix/smtp[22531]: CE65F35F: to=<d@c.com>, relay=none, delay=40, delays=40/0.13/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=c.com type=MX: Host not found, try again)




下一篇文章将介绍dovecot的安装和配置,通过dovecot来接收邮件、以及使用sasl来对postfix的收件人进行验证。





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-161203-1-1.html 上篇帖子: 有没有传奇的版本能发级我吗大哥 QQ3838303382 amigozhen@gmail.com 下篇帖子: 邮件系统服务器搭建记录(二)(Postfix+Cyrus-sasl+Courier-authlib+Dovecot+ExtMail+MySQL) 服务器 邮件系统 记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表