Postfix简介

Postfix是一个由IBM资助下由Wietse Venema负责开发的自由软件工程的一个产物,其目的是为用户提供除sendmail之外的邮件服务器选择。Postfix力图做到快速、易于管理、提供尽可能的安全性,同时尽量做到和sendmail邮件服务器保持兼容性以满足用户的使用习惯。起初,Postfix是以VMailer这个名字发布的,后来由于商标上的原因改名为Postfix。   Postfix设计中采用了web服务器的的设计技巧以减少进程创建开销,并且采用了其他的一些文件访问优化技术以提高效率,但同时保证了软件的可靠性。Postfix的设计目标就是成为Sendmail的替代者。

拓扑图

 一、安装前的准备工作
1.vim /etc/sysconfig/network //修改主机名字
文件内容修改:
HOSTNAME=mail.a.org
2.vim /etc/resolv.conf //dns指向
文件内容修改:
namedserver 192.168.2.100
3.安装dns服务
注意:vim /var/named/chroot/etc/named.conf
dns委派转发的配置:
28   forwarders  { 192.168.4.200; } ;
测试:
[root@mail ~]# dig -t mx a.org
; <<>> DiG 9.3.4-P1 <<>> -t mx a.org
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34385
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
 
;; QUESTION SECTION:
;a.org.                           IN   MX
 
;; ANSWER SECTION:
a.org.                     86400     IN   MX 10 mail.a.org.
 
;; AUTHORITY SECTION:
a.org.                     86400     IN   NS   ns.a.org.
 
;; ADDITIONAL SECTION:
mail.a.org.              86400     IN   A     192.168.2.100
ns.a.org.          86400     IN   A     192.168.2.100
 
;; Query time: 4 msec
;; SERVER: 192.168.2.100#53(192.168.2.100)
;; WHEN: Tue Aug 21 16:59:45 2012
;; MSG SIZE rcvd: 93
4.安装所需的rpm包,这包括以下这些:
[root@mail ~]# yum install httpd php php-mysql mysql mysql-server mysql-devel openssl-devel dovecot perl-DBD-MySQL tcl tcl-devel libart_lgpl libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect
5.关闭sendmail,并将它的随系统自动启动功能关闭:
[root@mail ~]# service sendmail stop
[root@mail ~]# chkconfig sendmail off
6.安装以下 源码开发所用到的rpm包组:
[root@mail ~]# yum grouplist
Development Libraries //管理工具
Development Tools     //开发工具
Legacy Software Development //传统的软件开发
X Software Development //图形界面软件开发
方法:
[root@mail ~]# yum groupinstall "packge_group_name"
7.启动mysql数据库,并给mysql的root用户设置密码:
[root@mail ~]# service mysqld start
[root@mail ~]# chkconfig mysqld on
授权本地用户:
[root@mail ~]# mysql
mysql> SET PASSWORD FOR root@'localhost'=PASSWORD('redhat');
mysql> SET PASSWORD FOR root@'127.0.0.1'=PASSWORD('redhat');
mysql> FLUSH PRIVILEGES; //刷新数据库
授权远程用户:
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'redhat';
mysql> FLUSH PRIVILEGES;
mysql> \q //退出数据库
8.启动saslauthd服务,并将其加入到自动启动队列:
[root@mail ~]# service saslauthd start
[root@mail ~]# chkconfig saslauthd on
二、安装配置postfix
1.安装过程
[root@mail ~]# tar -zxvf postfix-2.8.2.tar.gz -C /usr/local/src
[root@mail postfix-2.8.2]# groupadd -g 2525 postfix
[root@mail postfix-2.8.2]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
[root@mail postfix-2.8.2]# groupadd -g 2526 postdrop
[root@mail postfix-2.8.2]# useradd -g postdrop -u 2526 -s /bin/false -M postdrop
生成makefil文件:
[root@mail postfix-2.8.2]#make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2    -lssl -lcrypto'
命令参数说明:
CCARGS:变量
-DHAS_MYSQL:调用mysql
-I:include 头文件
AUXLIBS:辅助的库文件
[root@mail postfix-2.8.2]# make    //编译
[root@mail postfix-2.8.2]# make install //放入相应文档
按照以下的提示输入相关的路径([]号中的是缺省值,”]”后的是输入值,省略的表示采用默认值)
 install_root: [/] /   
 tempdir: [/usr/local/src/ postfix-2.6.5] /tmp //临时目录
 config_directory: [/etc/postfix] /etc/postfix //配置目录
 daemon_directory: [/usr/libexec/postfix]        //守护进程
 command_directory: [/usr/sbin]                  //命令目录
 queue_directory: [/var/spool/postfix]
 sendmail_path: [/usr/sbin/sendmail]
 newaliases_path: [/usr/bin/newaliases]
 mailq_path: [/usr/bin/mailq]                  //邮件队列
 mail_owner: [postfix]
 setgid_group: [postdrop]   
 html_directory: [no]
 manpages: [/usr/local/man]                   //man手册
 readme_directory: [no]
安装完毕
生成别名二进制文件,这个步骤如果忽略,会造成postfix效率极低:
[root@mail postfix-2.8.2]# newaliases
[root@mail postfix-2.8.2]# postfix start //启动postfix
 [root@mail postfix-2.8.2]# netstat -tupln |grep 25
tcp         0      0 0.0.0.0:25 (注意:当这里显示为127.0.0.1时,要重启一下,才能正常开启postfix服务)                 0.0.0.0:*                   LISTEN      11778/master
[root@mail postfix-2.8.2]# postconf -m //查看模块
mysql     //增加了mysql服务
[root@mail postfix-2.8.2]# postconf -a  //验证postfix是否支持cyrus风格的sasl认证
cyrus     //支持验证
安装启动脚本
[root@mail postfix-2.8.2]# mkdir /tmp/abc
[root@mail postfix-2.8.2]# cd /tmp/abc
[root@mail abc]# cp /mnt/cdrom/Server/postfix-2.3.3-2.1.el5_2.i386.rpm ./ //展开rpm包
[root@mail abc]# rpm2cpio postfix-2.3.3-2.1.el5_2.i386.rpm |cpio -id //-id表示建立相应目录
[root@mail abc]# cd etc/rc.d/init.d/
[root@mail init.d]# ll
总计 4
-rwxr-xr-x 1 root root 2404 08-21 18:26 postfix
[root@mail init.d]# cp postfix /etc/init.d/ //拷贝控制脚本
[root@mail init.d]# service postfix stop
[root@mail init.d]# service postfix start
[root@mail init.d]# netstat -tupln |grep 25
tcp         0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      11778/master
[root@mail init.d]# chkconfig --add postfix
[root@mail init.d]# chkconfig --list |grep postfix
[root@mail ~]# useradd user1
[root@mail ~]# passwd user1
2.进行一些基本配置,测试启动postfix并进行发信
[root@mail ~]#vim /etc/postfix/main.cf
修改以下几项为您需要的配置
75 myhostname = mail.a.org
83 mydomain = a.org
98 myorigin = $mydomain
112 inet_interfaces = all
160 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
259 mynetworks = 127.0.0.0/8 ,192.168.4.0 (邮件中继,接收邮件的网段)
说明:
myorigin参数用来指明发件人所在的域名;
mydestination参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到哪个域名的邮件;
myhostname 参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名;
mydomain参数指定您的域名,默认情况下,postfix将myhostname的第一部分删除而作为mydomain的值;
mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问;
inet_interfaces 参数指定postfix系统监听的网络接口;
注意:
1、在postfix的配置文件中,参数行和注释行是不能处在同一行中的;
2、任何一个参数的值都不需要加引号,否则,引号将会被当作参数值的一部分来使用;
3、每修改参数及其值后执行 postfix reload 即可令其生效;但若修改了inet_interfaces,则需重新启动postfix;
4、如果一个参数的值有多个,可以将它们放在不同的行中,只需要在其后的每个行前多置一个空格即可;postfix会把第一个字符为空格或tab的文本行视为上一行的延续;
重新启动postfix
[root@mail init.d]# service postfix restart
测试:
[root@mail ~]# telnet 127.0.0.1 25
helo mail.a.org
250 mail.a.org
mail from:root@localhost
250 2.1.0 Ok
rcpt to:user1@localhost
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello
.
250 2.0.0 Ok: queued as 210ED7804D
quit
三、为postfix开启基于cyrus-sasl的认证功能
1.[root@mail init.d]#vim /etc/postfix/main.cf
添加以下内容:
############################C1RUS-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
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!
2.[root@mail ~]# vim /usr/lib/sasl2/smtpd.conf
添加如下内容:
pwcheck_method: saslauthd //借助于sasl实现身份验证
mech_list: PLAIN LOGIN //验证机制
3.启动sasl服务,并让postfix重新加载配置文件
[root@mail ~]# service saslauthd start
[root@mail ~]# chkconfig saslauthd on
[root@mail ~]# service postfix restart
4.测试
[root@mail ~]# telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 mail.a.org ESMTP Postfix
ehlo mail.a.org
250-mail.a.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN    (请确保您的输出以类似两行,说明验证功能已经打开)
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
四、安装Courier authentication library
1.安装过程
[root@mail ~]# tar -jxvf courier-authlib-0.63.0.tar.bz2 -C /usr/local/src
[root@mail src]# cd courier-authlib-0.63.0/
[root@mail courier-authlib-0.63.0]#./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --with-authmysql --with-mysql-libs=/usr/lib/mysql --with-mysql-includes=/usr/include/mysql --with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-ltdl-lib=/usr/lib --with-ltdl-include=/usr/include
命令参数说明:
--with-redhat:针对rehat操作系统进行优化
[root@mail courier-authlib-0.63.0]# make
[root@mail courier-authlib-0.63.0]# make install
[root@mail courier-authlib-0.63.0]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
[root@mail courier-authlib-0.63.0]# cp /etc/authdaemonrc.dist /etc/authdaemonrc //验证调用模块
[root@mail courier-authlib-0.63.0]# cp /etc/authmysqlrc.dist /etc/authmysqlrc
2.编辑文件
vim /etc/authdaemonrc
文件内容修改:
27 authmodulelist="authmysql"
34 authmodulelistorig="authmysql"
53 daemons=10 //进程数量
vim /etc/authmysqlrc
 26 MYSQL_SERVER             localhost
 27 MYSQL_USERNAME           exmail //这时为后文要用的数据库的所有者的用户名
 28 MYSQL_PASSWORD           exmail //密码
 49 MYSQL_SOCKET             /var/lib/mysql/mysql.sock
 56 MYSQL_PORT              3306 //指定你的mysql监听的端口,这里使用默认的3306
 68 MYSQL_DATABASE           extmail
 83 MYSQL_USER_TABLE         mailbox
 92 MYSQL_CRYPT_PWFIELD       password
113 MYSQL_UID_FIELD          2525 // postfix 用户的UID
119 MYSQL_GID_FIELD          2525 // postfix 用户的UID
128 MYSQL_LOGIN_FIELD        username
133 MYSQL_HOME_FIELD         concat('/var/mailbox/',homedir) \\/虚拟帐号主目录
139 MYSQL_NAME_FIELD         name
150 MYSQL_MAILDIR_FIELD      concat('/var/mailbox/',maildir)
3.安装启动控制脚本
[root@mail courier-authlib-0.63.0]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib
控制脚本要具备可执行权限
[root@mail courier-authlib-0.63.0]# chmod 755 /etc/init.d/courier-authlib
[root@mail courier-authlib-0.63.0]# service courier-authlib start //启动服务
[root@mail courier-authlib-0.63.0]# chkconfig --add courier-authlib
[root@mail courier-authlib-0.63.0]# chkconfig courier-authlib on
[root@mail courier-authlib-0.63.0]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf
[root@mail courier-authlib-0.63.0]# ldconfig -v
[root@mail courier-authlib-0.63.0]# service courier-authlib restart    //重新启动服务
4.新建虚拟用户邮箱所在的目录,并将其权限赋予postfix用户:
[root@mail courier-authlib-0.63.0]#mkdir -pv /var/mailbox
[root@mail courier-authlib-0.63.0]#chown -R postfix /var/mailbox
5.重新配置SMTP 认证
vim /usr/lib/sasl2/smtpd.conf
文件内容:
pwcheck_method: authdaemond //形成的守护进程
mech_list:PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
[root@mail courier-authlib-0.63.0]# service saslauthd restart
[root@mail courier-authlib-0.63.0]# service courier-authlib restart
五、让postfix支持虚拟域和虚拟用户
1. vim /etc/postfix/main.cf
添加如下内容://注意:1.粘贴到文件内时,要把中文注释删去
                            2.粘贴时,要确保所有文件被粘贴进去,尤其是开头容易粘贴不进 
########################Virtual Mailbox Settings########################
virtual_mailbox_base = /var/mailbox
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:2525 //虚拟帐号映射为真实帐号
virtual_gid_maps = static:2525
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
##########################QUOTA Settings######################## //配额限定
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 Tidy your mailbox and try again later.
virtual_overquota_bounce = yes
2.使用extman源码目录下docs目录中的extmail.sql和init.sql建立数据库:
[root@mail ~]# tar zxvf extman-1.1.tar.gz //不需要安装
[root@mail ~]# cd extman-1.1/docs
[root@mail docs]# mysql -u root -p <extmail.sql //导入数据库
[root@mail docs]# mysql -u root -p <init.sql //初始化
[root@mail docs]# mysql -u root -p 进行验证
mysql> show databases;
+--------------------+
| Database            |
+--------------------+
| information_schema |
| extmail             |
| mysql               |
| test                |
+--------------------+
3.授予用户extmail访问extmail数据库的权限
mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
mysql> GRANT all privileges on extmail.* TO extmail@127.0.0.1 IDENTIFIED BY 'extmail';
mysql>FLUSH PRIVILEGES;    //刷新,让设置的内容生效
[root@mail docs]# cp mysql* /etc/postfix/    //拷贝映射文件
[root@mail docs]# service postfix restart
说明:启用虚拟域以后,需要取消中心域,即注释掉myhostname, mydestination, mydomain, myorigin几个指令;当然,你也可以把mydestionation的值改为你自己需要的。
六、配置dovecot
1. vim /etc/dovecot.conf //把userdb的其他相关禁用
文件内容修改:
 211 mail_location = mail_location = maildir:/var/mailbox/%d/%n/Maildir
 758 auth default {      //验证机制
 762 mechanisms = plain
 795 # passdb pam {     //关闭密码验证可查模块
 828    #}
 869    passdb sql {   //数据库调用密码
 871      args = /etc/dovecot-mysql.conf
 872    }
 896    #userdb passwd {
 903    #}
 930    userdb sql {
 932    args = /etc/dovecot-mysql.conf
 933 } 
2.vim  /etc/postfix/main.cf //修改配置文件
 414 home_mailbox = Maildir/
3.vim /etc/dovecot-mysql.conf
文件内容加入:              
driver = mysql
connect = host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'                            
user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = '%u'
4.接下来启动dovecot服务:
[root@mail ~]# service dovecot restart
[root@mail ~]# chkconfig dovecot on
七、安装Extmail-1.2 (先安装httpd)
1.安装
[root@mail ~]# tar -zxvf extmail-1.2.tar.gz
[root@mail ~]# mkdir -pv /var/www/extsuite
[root@mail ~]# mv extmail-1.2 /var/www/extsuite/extmail
[root@mail extmail]# cp webmail.cf.default webmail.cf
2.修改主配置文件
vim /var/www/extsuite/extmail/webmail.cf
文件内容修改:
44 SYS_LOG_ON = 1    //验证码
77 SYS_USER_LANG = zh_CN //中文
127 SYS_MAILDIR_BASE = /var/mailbox //用户邮件的存放目录
139 SYS_MYSQL_USER = extmail 
140 SYS_MYSQL_PASS = extmail //设置连接数据库服务器所使用用户名、密码和邮件服务器用到的数据库
197 SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket //指明authdaemo socket文件的位置
3.apache相关配置
由于extmail要进行本地邮件的投递操作,故必须将运行apache服务器用户的身份修改为您的邮件投递代理的用户;本例中打开了apache服务器的suexec功能,故使用以下方法来实现虚拟主机运行身份的指定。此例中的MDA为postfix自带,因此将指定为postfix用户:
在虚拟主机配置加上SuexecUserGroup postfix postfix即可
<VirtualHost>
SuexecUserGroup postfix postfix
</VirtualHost>
如果您没有打开apache服务器的suexec功能,也可以使用以下方法解决:
vim /etc/httpd/conf/httpd.conf
文件内容修改:
231 User postfix
232 Group postfix
992 <VirtualHost 192.168.2.100:80>
993 ServerName mail.a.org
994 DocumentRoot /var/www/extsuite/extmail/html/
995 ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
996 Alias /extmail /var/www/extsuite/extmail/html
997 </VirtualHost>
修改 cgi执行文件属组为apache运行身份用户:
[root@mail extmail]# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/
[root@mail extmail]# service httpd restart
4.依赖关系的解决
[root@mail ~]# tar zxvf Unix-Syslog-1.1.tar.gz
[root@mail ~]# cd Unix-Syslog-1.1
[root@mail Unix-Syslog-1.1]# perl Makefile.PL
[root@mail Unix-Syslog-1.1]# make
[root@mail Unix-Syslog-1.1]# make install
5.启动apache服务
[root@mail Unix-Syslog-1.1]# service httpd restart  //会报点儿小错误
[root@mail Unix-Syslog-1.1]# service named start //注意所有服务都要重启一遍
[root@mail Unix-Syslog-1.1]# chkconfig httpd on
访问站点: http://mail.a.org

八、安装Extman-1.1
1.安装及基本配置
[root@mail ~]# tar zxvf extman-1.1.tar.gz
[root@mail ~]# mv extman-1.1 /var/www/extsuite/extman
2.修改配置文件以符合本例的需要:
[root@mail extmail]# cd /var/www/extsuite/extman
[root@mail extman]# cp webman.cf.default webman.cf
[root@mail extman]# vim webman.cf
文件内容修改:
 12 SYS_MAILDIR_BASE = /var/mailbox //设置的用户邮件的存放目录
 21 SYS_CAPTCHA_ON = 0 //验证码
3.修改cgi目录的属主:
[root@mail extman]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
4.vim /etc/httpd/conf/httpd.conf
在apache的主配置文件中Extmail的虚拟主机部分,添加如下两行:
997 ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
998 Alias /extman /var/www/extsuite/extman/html
文件内容变为:
992 <VirtualHost 192.168.2.100:80>
993 ServerName mail.a.org
994 DocumentRoot /var/www/extsuite/extmail/html/
995 ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
996 Alias /extmail /var/www/extsuite/extmail/html
997 ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
998 Alias /extman /var/www/extsuite/extman/html
999 </VirtualHost>
5.创建其运行时所需的临时目录,并修改其相应的权限:
[root@mail extman]# mkdir -pv /tmp/extman
[root@mail extman]# chown postfix.postfix /tmp/extman
6.注意:vim /etc/postfix/main.cf
文件内容修改:
160 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain //要禁用
[root@mail Unix-Syslog-0.100]# service postfix restart
[root@mail Unix-Syslog-0.100]# service httpd  restart //不会报错
九、访问站点: http://mail.a.org
选择管理即可登入extman进行后台管理了。默认管理帐号为:root@extmail.org 密码为:extmail*123*

   

创建域名a.org,并设置选项
免费注册邮箱,注册新用户test和yang
用户test登录,给yang用户发邮件
用户yang登录,查收信件