基於虛擬帳號的郵件系統extmail
如果你經常使用郵箱的話, 你發現像網易、163等郵箱都是通過使用網頁直接申請並且使用的,它提供給用戶一種更快捷方便的操作,但我們前面所講的sendmail與postfix似乎還沒有這個功能,並且sendmail與postfix在創建一個用戶的時候都是通過在系統上手工創建的,試想一下,如果在一個訪問量比較大的郵件服務器,在網絡上供人隨意的註冊用戶的話,那麼單是管理員創建帳號就會被累死的啊,這還沒有考慮效率方面。
ExtMail是一個以perl語言編寫的,面向大容量/isp級應用,並且免費的高性能webmail軟件,主要包括ExtMail,Extman兩個部分的程序套件。ExtMail套件主要提供從瀏覽器中登錄,使用郵件系統web操作界面。
ExtMail配置示例
下面我在網上找了一個虛擬的ExtMail的構想圖,我們可以借鑑一下
從上面的圖示中,看到在安裝的過程中需要的步驟,我們分解來做這個郵件系統
一.POSTFIX的安裝
1> 配置DNS服務器
在配置時我們使用域名test.com,配置內容自己搞定,自己做測試,區域文件內容如下即可
2> 在上圖中我們可以看出,在Extmail使用中需要httpd php,mysql,openssl dovecot的軟件,我們可以先安裝。
[root@localhost named]# 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
3> 在安裝postfix之前,我們要先把系統開機啓動的sendmail停止,並且設爲開機不啓動,以免與postfix起衝突
[root@localhost named]# service sendmail stop
[root@localhost named]# chkconfig sendmail off
4> 因爲我們這次配置postfix是使用源碼安裝,我們我們要先安裝源碼解析工具
[root@localhost named]# yum groupinstall "Development Libraries" "Development Tools" "X Software Development" "Legacy Software Development"
可以使用yum grouplist 查看一下,沒有安裝的在按照此方法安裝
5> 安裝開發環境之後就可以安裝Postfix源碼包了
先爲postfix建立一個postfix組與用戶,並且安裝過程中會讓做一些選項,我們按照默認就行了
[root@localhost postfix-2.9.5]# groupadd -g 2525 postfix
[root@localhost postfix-2.9.5]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
[root@localhost postfix-2.9.5]# groupadd -g 2526 postdrop
[root@localhost postfix-2.9.5]# useradd -g postdrop -u 2526 -s /bin/false -M postdrop
[root@localhost postfix-2.9.5]#
[root@localhost ~]# tar -zxvf postfix-2.9.5.tar.gz -C /usr/local/src/
[root@localhost postfix-2.9.5]# cd /usr/local/src/postfix-2.9.5/
在進入目錄之後,我們會發現並沒有configure文件,我們可以使用另一種方法實現
[root@localhost postfix-2.9.5]# 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'
[root@localhost postfix-2.9.5]# make && make install
生成別名二進制文件,如果沒有這一步,postfix的效率會很低
[root@localhost postfix-2.9.5]# newaliases
配置文件,測試發信成功
[root@localhost postfix-2.9.5]# vim /etc/postfix/main.cf 修改如下內容
myhostname = mail.test.com
myorigin = $myhostname
mydomain = test.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 192.168.2.0/24, 127.0.0.0/8
開啓postfix服務
在開啓服務的時候,你會發現我們現在還不能使用service的方式啓動文件,這是因爲在目錄/etc/init.d/下還沒有postfix的啓動文件,我們可以通過兩種方法來實現使用service的方法直接實現
1.直接copy解壓目錄中的postfix啓動文件到/etc/init.d/目錄下
[root@localhost bin]# cd /usr/local/src/postfix-2.9.5/bin
[root@localhost bin]# cp postfix /etc/init.d/
但是這兩種方法下的postfix服務不能使用restart命令,僅僅可以使用stop與start,還不可以加入開機啓動項,所以基本上不用這種方法
2.這個方法比較麻煩一點,就是把rpm包中的postfix啓動腳本文件分離出來copy到/etc/init.d目錄中
[root@localhost bin]# cd /mnt/cdrom/Server/
[root@localhost Server]# cp postfix-2.3.3-2.1.el5_2.i386.rpm ~/
[root@localhost Server]# cd
[root@localhost ~]# mkdir postfix
[root@localhost ~]# cd postfix
[root@localhost postfix]# rpm2cpio ../postfix-2.3.3-2.1.el5_2.i386.rpm | cpio -id
16333 blocks
[root@localhost postfix]# cp etc/rc.d/init.d/postfix /etc/init.d/
把Postfix加入開機啓動行列
[root@localhost postfix]# chkconfig --add postfix
[root@localhost postfix]# chkconfig postfix on
開啓服務
[root@localhost postfix]# service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
6> 配置postfix開啓cyrus-sasl的認證功能
開啓saslauthd服務,並且加入開機啓動序列。
[root@localhost named]# service saslauthd start
Starting saslauthd: [ OK ]
[root@localhost named]# chkconfig saslauthd on
查看postfix是否支持sasl認證功能
[root@localhost ~]# /usr/local/src/postfix-2.9.5/bin/postconf -a
cyrus //出現這個說明支持sasl認證
dovecot
配置postfix主配置文件,開啓cyruus-sasl認證功能
[root@localhost ~]# vim /etc/postfix/main.cf 添加如下內容
############################CYRUS-SASL############################
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_inva
lid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_s
ender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipeli
ning,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
[root@localhost ~]# vim /usr/lib/sasl2/smtpd.conf 添加如下內容
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
重啓postfix服務
[root@localhost ~]# service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
查看sasl有沒有被加載
[root@localhost ~]# telnet 127.0.0.1 25
進去之後使用命令 ehlo mail.test.com
查看是否有這兩行,如果有的話,則配置正確
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
7> 爲mysql配置密碼123,並加入開機啓動
[root@localhost ~]# service mysqld start
[root@localhost ~]# mysqladmin -u root password "123"
[root@localhost ~]# chkconfig mysqld on
8> 我們要與mysql進行結合,使用讓postfix與courier auth進行結合,安裝Courier authentication library
[root@localhost ~]# tar -jxvf courier-authlib-0.63.0.tar.bz2 -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/courier-authlib-0.63.0/
[root@localhost 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
[root@localhost courier-authlib-0.63.0]# make && make install
[root@localhost courier-authlib-0.63.0]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
[root@localhost courier-authlib-0.63.0]# cp /etc/authdaemonrc.dist /etc/authdaemonrc[root@localhost courier-authlib-0.63.0]# cp /etc/authmysqlrc.dist /etc/authmysqlrc
修改/etc/authdaemonrc文件
[root@localhost ~]# vim /etc/authdaemonrc 修改如下
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10
修改/etc/authmysqlrc文件
[root@localhost ~]# vim /etc/authmysqlrc
修改如下
MYSQL_SERVER localhost
MYSQL_PORT 3306 (指定你的mysql監聽的端口,這裏使用默認的3306)
MYSQL_USERNAME extmail (這時爲後文要用的數據庫的所有者的用戶名)
MYSQL_PASSWORD extmail (密碼)
MYSQL_SOCKET /var/lib/mysql/mysql.sock
MYSQL_DATABASE extmail
MYSQL_USER_TABLE mailbox
MYSQL_CRYPT_PWFIELD password
MYSQL_UID_FIELD '2525'
MYSQL_GID_FIELD '2525'
MYSQL_LOGIN_FIELD username
MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)
MYSQL_NAME_FIELD name
MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)
創建啓動文件,並加入開機啓動行列
[root@localhost ~]# cp /usr/local/src/courier-authlib-0.63.0/courier-authlib.sysvinit /etc/init.d/courier-authlib
[root@localhost ~]# chmod 755 /etc/init.d/courier-authlib
[root@localhost ~]# chkconfig --add courier-authlib
[root@localhost ~]# chkconfig courier-authlib on
把頭文件存放在標準路徑
[root@localhost ~]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf
查看一下是否有頭文件
[root@localhost ~]# ldconfig -v
開啓服務
[root@localhost ~]# service courier-authlib start
Starting Courier authentication services: authdaemond
9> 新建虛擬用戶郵箱的所在目錄,並賦予其權限
[root@localhost ~]# mkdir -pv /var/mailbox
[root@localhost ~]# chown -R postfix /var/mailbox
10> 配置SMTP認證
[root@localhost ~]# vim /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
11> 現在讓postfix支持虛擬域和虛擬用戶
編輯postfix主文件/etc/postfix/main.cf添加支持虛擬域的代碼
[root@localhost ~]# vim /etc/postfix/main.cf
########################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
12> 建立數據庫
[root@localhost ~]# tar -zxvf extman-1.1.tar.gz
[root@localhost ~]# cd extman-1.1/docs/
直接使用本目錄下的數據庫
[root@localhost docs]# mysql -u root -p < extmail.sql
Enter password:
[root@localhost docs]# mysql -u root -p < init.sql
Enter password:
[root@localhost docs]# cp mysql* /etc/postfix/
13> 對用戶extmail進行授權
[root@localhost docs]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Y our MySQL connection id is 9
mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT all privileges on extmail.* TO [email protected] IDENTIFIED BY 'extmail';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES; //刷新數據庫,使配置生效
Query OK, 0 rows affected (0.00 sec)
14> 虛擬域現在已經配置好了,但是要想postfix正常工作,我們需要取消中心域
[root@localhost docs]# vim /etc/postfix/main.cf //註釋掉以下一行
161 #mydestination = $myhostname, localhost.$mydomain, localhost,
$mydomain
重啓一下postfix
[root@localhost docs]# service postfix restart