天天看點

實戰部署MySQL使用者認證的Postfix郵件系統(1)

實戰部署Postfix+MySQL+Dovecot+Postfixadmin+Extmail郵件系統(1)

-----建構開源郵件系統系列

   如果您對本系列文章感興趣,可以通過點選以下連結檢視全文:

一、安裝前的準備工作

郵件伺服器的安裝、配置工作是一個相對複雜的過稱,在正式安裝Postfix之前,我們必須要一些準備工作。

1、 基本網絡的設定

在一個網絡環境中,主機名是識别某個計算機的唯一辨別,而在一個單機環境中,主機名隻給出了系統的稱呼。讓修改的主機名永久生效, 則必須要修改/etc/sysconfig/network 檔案,在該檔案中,修改HOSTNAME一行:

[root@mail ~]# cat /etc/sysconfig/network 

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

/etc/hosts 它給每個主機賦一個IP 位址,即使計算機不在網絡上,在/etc/hosts 中也會包含使用者自己的主機名。

[root@mail ~]# cat /etc/hosts 

…… 

10.1.1.120 mail.xifeng.com

設定DNS 伺服器的方法比較簡單,隻需修改/etc/resolv.conf 檔案即可。

[root@noc ~]# cat /etc/resolv.conf 

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 ~]#

或者,将 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

啟動MySQL資料庫服務并設定為開機自啟動

[root@mail ~]# /etc/init.d/mysqld start 

Initializing MySQL database:    Installing MySQL system tables... 

OK 

Filling help tables... 

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>

二、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

安裝rpm-build軟體包後,會在/usr/src/redhat/目錄下産生如下目錄結構:

[root@mail ~]# ls /usr/src/redhat/ 

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 

省略若幹……

你要自己編譯二進制的RPM軟體封包件,需要進入 /usr/src/redhat/SPECS目錄下修改spec腳本即可。Postfix的源碼軟體包會生成一個叫做postfix.spec的腳本檔案,如下所示:

[root@mail ~]# cd /usr/src/redhat/SPECS/ 

[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 

… …

使用rpmbuild指令重新編譯Postfix,這個過程需要時間根據系統而定,有時候需要花費很長時間。

[root@mail SPECS]# rpmbuild -ba postfix.spec 

+ /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]#

安裝完成後我們可以使用postconf –m指令驗證postfix是否支援mysql。

[root@mail i386]# postconf -m 

...... 

ldap 

mysql 

......

三、 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

接下來編輯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

修改完以上述資訊之後我們還需要添加一些配置資訊:

#======================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_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萬字元,特分為幾篇釋出,點選

溫馨提示:所有配置檔案,是直接使用WinRAR壓縮的,可以直接解壓檢視。postfixadmin2.3.6.tar.gz(1.52MB) 和extmail-1.2.tar.gz(529kb)下載下傳後将檔案名修改為postfixadmin2.3.6.tar.gz和extmail-1.2.tar.gz即可。

<a href="http://down.51cto.com/data/2362199" target="_blank">附件:http://down.51cto.com/data/2362199</a>

     本文轉自yjlsy 51CTO部落格,原文連結:http://blog.51cto.com/baidu/1123497,如需轉載請自行聯系原作者