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

[经验分享] 配置全功能的Postfix 服务器(参考)

[复制链接]

尚未签到

发表于 2015-11-24 11:37:32 | 显示全部楼层 |阅读模式
RHEL 4 上配置全功能的Postfix 服务器


简介
本文讲述在安装了Redhat Enterprice AS 4 U2PC上,配置一台Postfix邮件服务器的过程。实现虚拟域、虚拟用户、POP3POP3SSMTPSMTPSIMAPIMAPS、防毒、防SPAM、网页邮件功能。



涉及的软件
1.        Redhat Enterprice AS 4 U2
2.        Mysql
系统自带
用于存储虚拟域、虚拟用户等信息。
3.        Apache
系统自带
运行网页邮件SquirrelMailPostfixAdmin时使用。
4.        PHP
系统自带
运行网页邮件SquirrelMailPostfixAdmin时使用。
5.        Cyrus-sasl
系统自带
实现带验证的SMTP时使用。
6.        Courier authentication library
http://www.courier-mta.org/ 下载
实现带验证的SMTP时使用。
7.        Postfix
http://postfix.org 下载
一个MTA,虽然RHEL 4 自带Postfix,但因为其不支持SSLMysql,所以我们需要自行编译。
8.        PostfixAdmin
http://postfixadmin.com/ 下载
虚拟域、虚拟用户等信息是放在Mysql内的,安装PostfixAdmin后,就可以用浏览器管理这些信息。
9.        SquirrelMail
系统自带
一个基于IMAPWebmail客户端。
10.        Dovecot
系统自带
提供POP3POP3SIMAPIMAPS功能。
11.        F-prot
http://www.f-prot.com 下载,Linux workstation 版个人使用是免费的
提供杀毒功能,据闻速度和病毒库比开源的Clamav好。
12.        SpamAssassin
系统自带
提供过滤垃圾邮件功能。
13.        MailScanner
http://www.sng.ecs.soton.ac.uk/mailscanner/ 下载
Postfix
是使用MailScanner 调用f-prot SpamAssassin,或者其它调用方法效率更好,但MailScanner配置比较直观,且最近拿了一个大奖 http://www.linuxawards.co.uk/content/view/26/42/



配置过程
1
        基本软件安装
安装RHEL 4 U2,使用默认安装,语系为简体中文。关闭SE Linux,配置网卡,使其能访问Internet。进入X window,开始->系统设置->添加/删除应用程序,安装上文中提及系统自带的软件,包括:
万维网服务器:mysql & php 相关的
邮件服务器:SquirrelMail perl-cyrus
SQL
数据库:全部。
下载其它需要的软件。

2
        配置PostfixAdminMysqlApache
[iyunv@localhost tmp]# tar xzf postfixadmin-2.1.0.tgz
[iyunv@localhost tmp]# mv postfixadmin-2.1.0 /var/www/html/pa
[iyunv@localhost tmp]# service mysqld start
[iyunv@localhost tmp]# mysql -uroot < /var/www/html/pa/DATABASE_MYSQL.TXT
[iyunv@localhost tmp]# cp /var/www/html/pa/config.inc.php.sample  /var/www/html/pa/config.inc.php
[iyunv@localhost tmp]# service httpd start

打开浏览器访问 http://localhost/pa,然后按提示增加两个虚拟域名d1.com d2.com,增加两个虚拟用户a@d1.comb@d2.com

设置MysqlApache自动启动
[iyunv@localhost tmp]# chkconfig --level 35 mysqld on
[iyunv@localhost tmp]# chkconfig --level 35 httpd on

3
        编译、配置Postfix
先删除sendmail
[iyunv@localhost tmp]# rpm –e sendmail –nodeps
[iyunv@localhost tmp]# groupadd postfix
[iyunv@localhost tmp]# groupadd postdrop
[iyunv@localhost tmp]#useradd postfix -g postfix -c &quot;Postfix user&quot; -d /dev/null -s /sbin/nologin
[iyunv@localhost tmp]# tar xzf postfix-2.2.7.tar.gz
[iyunv@localhost tmp]# cd postfix-2.2.7
[iyunv@localhost postfix-2.2.7]# make -f Makefile.init makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_TLS -DUSE_SASL_AUTH -I/usr/include/sasl' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib -lssl -lcrypto -lsasl2'
[iyunv@localhost postfix-2.2.7]# make
[iyunv@localhost postfix-2.2.7]# make install
注:“make install”命令后的所有问题都直接敲回车键即可。

编辑/etc/postfix/main.cf 为以下内容
#=====================BASE=========================
myhostname = mail.d1.com
mydomain = d1.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 192.168.1.0/24 127.0.0.0/8
inet_interfaces = all
#=====================Vritual Mailbox settings=========================
virtual_mailbox_base = /var/spool/mail
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_alias_domains =
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:501
virtual_gid_maps = static:502
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
#====================QUOTA========================
message_size_limit = 14336000
virtual_mailbox_limit = 20971520
virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf
virtual_mailbox_limit_override = yes
virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later.
virtual_overquota_bounce = yes
#====================SASL========================
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,permit
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP &quot;Version not Available&quot;

readme_directory = no
sample_directory = /etc/postfix
sendmail_path = /usr/sbin/sendmail
html_directory = no
setgid_group = postdrop
command_directory = /usr/sbin
manpage_directory = /usr/local/man
daemon_directory = /usr/libexec/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
queue_directory = /var/spool/postfix
mail_owner = postfix

编辑/etc/postfix/ mysql_virtual_alias_maps.cf 为以下内容
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address

编辑/etc/postfix/ mysql_virtual_domains_maps.cf 为以下内容
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = domain
select_field = description
where_field = domain

编辑/etc/postfix/ mysql_virtual_mailbox_limit_maps.cf 为以下内容
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username

编辑/etc/postfix/ mysql_virtual_mailbox_maps.cf 为以下内容
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username



4
        配置SMTP 认证
编辑 /usr/lib/sasl2/smtpd.conf 为以下内容
pwcheck_method: authdaemond
log_level: 3
mech_list: plain login
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket

安装Courier authentication library
[iyunv@localhost tmp]# tar jxf courier-authlib-0.58.tar.bz2
[iyunv@localhost tmp]# cd courier-authlib-0.58
[iyunv@localhost courier-authlib-0.58]# ./configure --prefix=/usr/local/courier-authlib --without-authpam --without-authldap --without-authpwd --without-authshadow --without-authvchkpw --without-authpgsql  --with-authmysql --with-mysql-libs=/usr/lib/mysql --with-mysql-includes=/usr/include/mysql --with-redhat  --with-authmysqlrc=/usr/local/courier-authlib/etc/authmysqlrc  --with-authdaemonrc=/usr/local/courier-authlib/etc/authdaemonrc CFLAGS=&quot;-march=i686 -O2 -fexpensive-optimizations&quot; CXXFLAGS=&quot;-march=i686 -O2 -fexpensive-optimizations&quot;
[iyunv@localhost courier-authlib-0.58]# make
[iyunv@localhost courier-authlib-0.58]# make install
[iyunv@localhost courier-authlib-0.58]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
[iyunv@localhost courier-authlib-0.58]# cp /usr/local/courier-authlib/etc/authdaemonrc.dist /usr/local/courier-authlib/etc/authdaemonrc

修改/usr/local/courier-authlib/etc/authdaemonrc 文件
authmodulelist=&quot;authmysql&quot;
authmodulelistorig=&quot;authmysql&quot;
daemons=10

编辑/usr/local/courier-authlib/etc/authmysqlrc 为以下内容,其中501502 postfix 用户的UIDGID
MYSQL_SERVER         localhost
MYSQL_USERNAME       postfix
MYSQL_PASSWORD       postfix
MYSQL_SOCKET         /var/lib/mysql/mysql.sock
MYSQL_DATABASE       postfix
MYSQL_USER_TABLE     mailbox
MYSQL_CRYPT_PWFIELD   password
MYSQL_UID_FIELD       '501'
MYSQL_GID_FIELD       '502'
MYSQL_LOGIN_FIELD     username
MYSQL_HOME_FIELD     concat('/var/spool/mail/',maildir)
MYSQL_MAILDIR_FIELD   concat('/var/spool/mail/',maildir)
MYSQL_NAME_FIELD     name

# cp courier-authlib.sysvinit /etc/init.d/courier-authlib
# chmod 755 /etc/init.d/courier-authlib
# chkconfig --add courier-authlib
# chkconfig --level 35 courier-authlib on
手动启动服务:
# service courier-authlib start

此时你已经拥有一台带验证的SMTP 服务器,用Outlook Foxmail测试一下吧,用户名采用usename@domainName.com 形式。

5        增加SSL功能,配置SMTPS服务
/etc/postfix/main.cf 增加以下内容
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/ssl/smtpd.pem
smtpd_tls_cert_file = /etc/ssl/smtpd.pem
smtpd_tls_CAfile = /etc/ssl/smtpd.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

生成证书
# mkdir /etc/ssl
# cd /etc/ssl
# openssl req -new -x509 -nodes -out smtpd.pem -keyout smtpd.pem -days 3650

重新启动postfix
# postfix reload

这时SMTPS功能应该能正常工作了。有能力测试的网友们请验证一下这功能,因为使用SMTPS + Outlook 时有一错误提示。



6
        配置Dovecot,增加IMAPIMAPSPOP3POP3S功能
由于Dovecot 是系统自带的,配置两个文件,再生成证书就可以了。

编辑 /etc/dovecot.conf 为以下内容
base_dir = /var/run/dovecot/
protocols = imap imaps pop3 pop3s
imap_listen = [::]
pop3_listen = [::]
ssl_disable = no
ssl_cert_file = /etc/ssl/certs/imapd.pem
ssl_key_file = /etc/ssl/private/imapd.pem
ssl_parameters_file = /var/run/dovecot/ssl-parameters.dat
login_dir = /var/run/dovecot-login
login = imap
login = pop3
mbox_locks = fcntl
auth = default
auth_mechanisms = plain
auth_userdb = passwd
auth_user = root
default_mail_env = maildir:/var/spool/mail/%u/
auth_userdb = mysql /etc/dovecot-mysql.conf
auth_passdb = mysql /etc/dovecot-mysql.conf
first_valid_uid = 501

编辑 /etc/dovecot-mysql.conf 为以下内容
db_host = localhost
db_port = 3306
db_unix_socket = /var/lib/mysql/mysql.sock
db = postfix
db_user = postfix
db_passwd = postfix
db_client_flags = 0
default_pass_scheme = PLAIN-MD5
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, 501 AS uid, 502 AS gid FROM mailbox WHERE username = '%u'

生成证书
# mkdir /etc/ssl/certs
# mkdir /etc/ssl private
# cd /usr/share/doc/dovecot-0.99.11/
# sh mkcert.sh

启动Dovecot 服务
# service dovecot start
# chkconfig --level 35 dovecot on

顺利的话,此时SMTPSMTPSPOP3POP3S已经配置完成。



7
        配置SquirrelMail网页邮件
很简单,用浏览器访问 http://localhost/webmail



8
        配置杀毒、过滤SPAM功能
如果你打算将杀毒、过滤SPAM功能安排在各个客户端,以下是不必要操作的。

安装f-prot
http://files.f-prot.com/files/linux-x86/fp-linux-ws.rpm 下载
# rpm -ivh fp-linux-ws.rpm

启动SpamAssassin
# service spamassassin start
# chkconfig --level 35 spamassassin on

安装、配置MailScanner
# tar xzf MailScanner-4.48.4-2.rpm.tar.gz
# cd MailScanner-4.48.4-2
# ./install.sh

修改 /etc/MailScanner/MailScanner.conf
Run As User = postfix
Run As Group = postfix
Incoming Queue Dir = /var/spool/postfix/hold
Outgoing Queue Dir = /var/spool/postfix/incoming
MTA = postfix
Virus Scanners = f-prot
Always Include SpamAssassin Report = yes
Use SpamAssassin = yes
Required SpamAssassin Score = 4
SpamAssassin User State Dir = /var/spool/MailScanner/spamassassin
SpamAssassin Install Prefix = /usr/bin
SpamAssassin Local Rules Dir = /etc/MailScanner

Postfix 调用MailScanner,在/etc/postfix/main.cf 加入以下一行
header_checks = regexp:/etc/postfix/header_checks

编辑/etc/postfix/header_checks 为以下内容
/^Received:/ HOLD

修正权限
# chown postfix.postfix /var/spool/MailScanner/incoming
# chown postfix.postfix /var/spool/MailScanner/quarantine

启用
# postfix stop
# service MailScanner start
# chkconfig --level 35 MailScanner on

注:MailScanner 会自动启动Postfix

测试SpamAssassin
发一封邮件带如下内容,接收后,标题应该带有标记:
XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

测试f-prot
升级病毒库:
# /usr/local/f-prot/tools/check-updates.pl
访问http://www.webmail.us/testvirus,申请寄一封带virus 的邮件寄到自己的邮箱


9
        如果上面的操作顺利,整个配置完成



其它补充
在应用在生产环境前,请重新检查各个软件的配置文件,进行完整的测试,有发现请联系笔者,谢谢!


主要参考文档
http://bbs.iyunv.com/viewthread.php?tid=640481
http://anti-spam.org.cn/forums/i ... topic=3357&st=0

运维网声明 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-143037-1-1.html 上篇帖子: Centos6 Postfix+Dovecot+Postfixadmin+Roundcube 搭建Webmail邮件系统 下篇帖子: [Postfix]队列监控告警,发送告警邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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