实战部署Postfix+MySQL+Dovecot+Postfixadmin+Extmail邮件系统(1)
-----构建开源邮件系统系列
如果您对本系列文章感兴趣,可以通过点击以下链接查看全文:
点击 《实战部署MySQL用户认证的Postfix邮件系统(1)》 查看第一篇
点击 《实战部署MySQL用户认证的Postfix邮件系统(2)》 查看第二篇
点击 《实战部署MySQL用户认证的Postfix邮件系统(3)》 查看第三篇
一、安装前的准备工作
邮件服务器的安装、配置工作是一个相对复杂的过称,在正式安装Postfix之前,我们必须要一些准备工作。
1、 基本网络的设置
在一个网络环境中,主机名是识别某个计算机的唯一标识,而在一个单机环境中,主机名只给出了系统的称呼。让修改的主机名永久生效, 则必须要修改/etc/sysconfig/network 文件,在该文件中,修改HOSTNAME一行:
[root@mail ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=mail.xifeng.com
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=mail.xifeng.com
也可以在/etc/sysconfig/network增加缺省网关,在该文件中添加GATEWAY=网关地址字段即可。
在RedHat Linux 中,要设置主机的IP 地址,通用的方法是直接更改脚本。例如,当主机中只有一块网卡时,其设备名为eth0,此时要给该网卡设置IP 地址,只需要修改/etc/sysconfig/network-scripts/ifcfg-eth0 文件即可,下面是一个ifcfg-eth0 文件的配置示例:
[root@noc ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.1.1.255
HWADDR=00:0C:29:47:8F:7A
IPADDR=10.1.1.120
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
NETWORK=10.1.1.0
ONBOOT=yes
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.1.1.255
HWADDR=00:0C:29:47:8F:7A
IPADDR=10.1.1.120
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
NETWORK=10.1.1.0
ONBOOT=yes
/etc/hosts 它给每个主机赋一个IP 地址,即使计算机不在网络上,在/etc/hosts 中也会包含用户自己的主机名。
[root@mail ~]# cat /etc/hosts
……
10.1.1.120 mail.xifeng.com
……
10.1.1.120 mail.xifeng.com
设置DNS 服务器的方法比较简单,只需修改/etc/resolv.conf 文件即可。
[root@noc ~]# cat /etc/resolv.conf
nameserver 10.1.1.120
nameserver 10.1.1.120
2、停止或卸载老的MTA软件
Redhat Enterprise Linux 在安装系统的过程中默认就将Sendmail这个古老的邮件系统安装到你的系统中。既然我们选择使用Postfix这个邮件系统,俗话说“一山不容二虎,除非一公一母”,这个时候,又让我们做一个艰难的决定了,只能让我们二选一了。
首先,使用chkconfig命令将Sendmail服务设置默认开机时关闭,同时将该服务关闭。
[root@mail ~]# chkconfig sendmail off
[root@mail ~]# /etc/init.d/sendmail stop
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
[root@mail ~]#
[root@mail ~]# /etc/init.d/sendmail stop
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
[root@mail ~]#
或者,将 Sendmail软件包直接卸载,如果使用rpm命令卸载的话,由于软件包之间存在依赖关系,可能无法直接卸载的掉,可以使用--nodeps参数忽略软件包的依赖关系卸载。这里推荐使用yum remove命令删除,使用yum命令删除的好处是自动处理软件包的依赖关系。
[root@mail ~]# yum remove sendmail
3、部署LAMP环境
由于我们使用的postfixadmin和extmail是基于php和perl编写的,首先需要安装lamp环境,用于解析php界面,我们直接使用yum安装即可。
[root@mail ~]# yum install httpd* mysql* php-*
启动apache并将apache设置为开机自启动
[root@mail~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
[root@mail ~]# chkconfig --level 35 httpd on
Starting httpd: [ OK ]
[root@mail ~]# chkconfig --level 35 httpd on
启动MySQL数据库服务并设置为开机自启动
[root@mail ~]# /etc/init.d/mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h mail.xifeng.com password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[ OK ]
Starting MySQL: [ OK ]
[root@mail ~]# chkconfig --level 35 mysqld on
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h mail.xifeng.com password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[ OK ]
Starting MySQL: [ OK ]
[root@mail ~]# chkconfig --level 35 mysqld on
设置MySQL数据库密码并验证登录
[root@mail ~]# mysqladmin -u root password 123456
[root@mail ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
[root@mail ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
二、Postfix的安装
在安装Postfix之前,由于Postfix默认的安装包不支持MySQL,我们首先要在RedHat的FTP站点(ftp://ftp.redhat.com/pub/redhat/linux/enterprise/)下载Postfix的源码RPM软件包,重新编译Postfix使其支持MySQL。
一般源码RPM软件包的命名格式如下:
<软件包名称.软件的版本号>.src.rpm
这种RPM文件封装着应用软件的源代码,所以被称为源码RPM软件包。源码软件包RPM文件主要用来制作(Build)出其他种类的RPM软件包文件。使用rpmbuild命令来制作成二进制的RPM软件包,以下命令是检查rpm-build的软件包是否被安装,如果没有安装安装即可。
[root@mail ~]# rpm -q rpm-build
package rpm-build is not installed
[root@mail ~]# yum install rpm-build
package rpm-build is not installed
[root@mail ~]# yum install rpm-build
安装rpm-build软件包后,会在/usr/src/redhat/目录下产生如下目录结构:
[root@mail ~]# ls /usr/src/redhat/
BUILD RPMS SOURCES SPECS SRPMS
BUILD RPMS SOURCES SPECS SRPMS
各个目录的说明:
l BUILD:源码软件包编译过程中的临时目录
l RPMS:从源码RPM软件包生成的二进制RPM软件包存放目录
l SOURCES :实际的源码包,通常是tar压缩包
l SPECS:建立二进制RPM软件包的规范文件spec存放位置
l SRPMS:在制作过程中生成的源码RPM软件包
将下载回来的Postfix源码RPM软件包,直接执行rpm -i安装后,软件的源码会安装在/usr/src/redhat/SOURCES/目录下,
[root@mail ~]# rpm -i postfix-2.3.3-2.3.el5_6.src.rpm
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
省略若干……
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
省略若干……
你要自己编译二进制的RPM软件包文件,需要进入 /usr/src/redhat/SPECS目录下修改spec脚本即可。Postfix的源码软件包会生成一个叫做postfix.spec的脚本文件,如下所示:
[root@mail ~]# cd /usr/src/redhat/SPECS/
[root@mail SPECS]# ls
postfix.spec
[root@mail SPECS]# ls
postfix.spec
修改postfix.spec,将与MySQL和postfix用户的uid和gid相关行修改即可。
[root@mail SPECS]# vim postfix.spec
%define LDAP 2
%define MYSQL 1 #将默认的0修改为1
%define postfix_uid 1000 #将默认的89修改为1000
… …
%define postfix_gid 1000 #将默认的89修改为1000
… …
%define LDAP 2
%define MYSQL 1 #将默认的0修改为1
%define postfix_uid 1000 #将默认的89修改为1000
… …
%define postfix_gid 1000 #将默认的89修改为1000
… …
使用rpmbuild命令重新编译Postfix,这个过程需要时间根据系统而定,有时候需要花费很长时间。
[root@mail SPECS]# rpmbuild -ba postfix.spec
… …
+ /bin/rm -rf /var/tmp/postfix-buildroot
+ exit 0
[root@mail SPECS]#
… …
+ /bin/rm -rf /var/tmp/postfix-buildroot
+ exit 0
[root@mail SPECS]#
编译完成后,默认RedHat将这个包放到了/usr/src/redhat/RPMS/i386这个目录下,进入该目录安装即可。
[root@mail SPECS]# cd ../RPMS/i386/
[root@mail i386]# rpm -ivh postfix-2.3.3-2.3.i386.rpm
Preparing... ########################################### [100%]
1:postfix ########################################### [100%]
[root@mail i386]#
[root@mail i386]# rpm -ivh postfix-2.3.3-2.3.i386.rpm
Preparing... ########################################### [100%]
1:postfix ########################################### [100%]
[root@mail i386]#
安装完成后我们可以使用postconf –m命令验证postfix是否支持mysql。
[root@mail i386]# postconf -m
......
ldap
mysql
......
[root@mail i386]#
......
ldap
mysql
......
[root@mail i386]#
三、 Postfix的基本配置
Postfix最主要的配置文件保存在/etc/postfix/main.cf和/etc/postfix/master.cf两个文件中。main.cf中保存了Postfix的基本配置项,而master.cf中定义了组成Postfix的各个守护进程的调用信息。
首先要完成一些外围工作:
[root@mail i386]# touch /etc/postfix/mysql_virtual_alias_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_domains_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_mailbox_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_domains_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
[root@mail i386]# touch /etc/postfix/mysql_virtual_mailbox_maps.cf
接下来编辑Postfix的配置文件了。首先,我们要编辑main.cf文件,修改部分配置的参数为下面的内容:
[root@mail i386]# cat /etc/postfix/main.cf
#==============================BASE=============================
myhostname = mail.xifeng.com
mydomain = xifeng.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 127.0.0.0/8
inet_interfaces = all
#==============================BASE=============================
myhostname = mail.xifeng.com
mydomain = xifeng.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 127.0.0.0/8
inet_interfaces = all
修改完以上述信息之后我们还需要添加一些配置信息:
#======================Virtual Mailbox Settings========================
virtual_minimum_uid = 100
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
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:1000
virtual_gid_maps = static:1000
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
#==============================QUOTA===========================
message_size_limit = 52428800
mailbox_size_limit = 209715200
virtual_mailbox_limit = 209715200
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_type = dovecot
smtpd_sasl_path = /var/run/dovecot/auth-client
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP "Version not Available"
#==============================================================
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
virtual_minimum_uid = 100
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
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:1000
virtual_gid_maps = static:1000
virtual_transport = virtual
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1
#==============================QUOTA===========================
message_size_limit = 52428800
mailbox_size_limit = 209715200
virtual_mailbox_limit = 209715200
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_type = dovecot
smtpd_sasl_path = /var/run/dovecot/auth-client
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP "Version not Available"
#==============================================================
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
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf:这个配置参数指明服务器上邮箱文件的存储路径。
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf:这个配置参数执行邮件服务器上所有的虚拟域。
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf:这个配置参数指明邮件服务器上虚拟别名和实际邮件地址间的对应关系。
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf:这个配置参数指明服务器上邮箱的一些限制参数。
通常我们将这些配置参数称为查询表,它定义了这个参数项的实际配置数据的存储位置和格式。比如“mysql:/etc/postfix/mysql_virtual_alias_maps.cf”的含义就是:查询表是MySQL数据库格式,访问这个数据库表的方法在文件“/etc/postfix/mysql_virtual_alias_maps.cf”中存放。另一种查询表格式是“alias_maps = hash:/etc/aliases”,它表示查询表是一个哈希文件,文件的路径是/etc/aliases。Postfix在需要的时候读取这些配置信息,然后根据这些配置信息的指示,到另外的文件或者数据库中去读取实际的数据。
由于老是提示文章篇幅超过8万字符,特分为几篇发布,点击
点击 《实战部署MySQL用户认证的Postfix邮件系统(1)》 查看第一篇
点击 《实战部署MySQL用户认证的Postfix邮件系统(2)》 查看第二篇
点击 《实战部署MySQL用户认证的Postfix邮件系统(3)》 查看第三篇
温馨提示:所有配置文件,是直接使用WinRAR压缩的,可以直接解压查看。postfixadmin2.3.6.tar.gz(1.52MB) 和extmail-1.2.tar.gz(529kb)下载后将文件名修改为postfixadmin2.3.6.tar.gz和extmail-1.2.tar.gz即可。
其它软件到http://down.51cto.com/71938下载
附件:http://down.51cto.com/data/2362199
本文转自yjlsy 51CTO博客,原文链接:http://blog.51cto.com/baidu/1123497,如需转载请自行联系原作者