基於虛擬賬號的郵件系統postfix+extmail+mysql
目前流行的電子郵件系統的特點:
1.支持web方式的訪問
2.自由免費註冊賬號
3.支持網盤,個性化設置
4支持.虛擬賬號,把註冊的賬號存放到數據庫中
一、安裝前的準備工作:
1、安裝所需的rpm包,這包括以下這些:
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,cache-nameserver,bind-chroot,bind
yuminstallhttpdmysqlmysql-servermysql-developenssl-develdovecotperl-DBD-MySQLtcltcl-devellibart_lgpllibart_lgpl-devellibtool-ltdllibtool-ltdl-develexpectcaching-nameserverbind-chrootbind
2、關閉sendmail,並將它的隨系統自動啓動功能關閉:
#servicesendmailstop
#chkconfigsendmailoff
3、切換到/var/named/chroot/etc/目錄下複製named.caching-nameserver.conf爲named.conf,打開named.conf,修改localhost爲any
到區域文件namd.rfc1912.zones裏聲明一個區域a.com
創建a.com.zone區域文件,並創建紀錄
修改主機名爲mail.a.com
打開/etc/resolv.conf,把dns指向自己
4、啓動mysql數據庫,並給mysql的root用戶設置密碼:
#servicemysqldstart
#chkconfigmysqldon
#mysqladmin-urootpassword'your_password'
也可以
---------------------------------授權本地用戶
SETPASSWORDFORroot@'localhost'=PASSWORD('redhat');
SETPASSWORDFORroot@'127.0.0.1'=PASSWORD('redhat');
FLUSHPRIVILEGES;
-------------------------------授權遠程用戶
GRANTALLPRIVILEGESON*.*TOroot@'%'IDENTIFIEDBY'redhat';
FLUSHPRIVILEGES;
#5、啓動saslauthd服務,並將其加入到自動啓動隊列:
#servicesaslauthdstart
#chkconfigsaslauthdon
二、安裝配置postfix
軟件包:courier-authlib-0.63.0.tar.bz2postfix與mysql數據庫之間進行身份驗證的程序
extmail-1.2.tar.gz基於web頁面的收發郵件
extman-1.1.tar.gz基於web頁面的賬號註冊
postfix-2.8.2.tar.gzpostfix發送郵件
Unix-Syslog-0.100.tar.gz日誌補丁
#groupadd-g2525postfix
#useradd-gpostfix-u2525-s/sbin/nologin-Mpostfix
#groupadd-g2526postdrop
#useradd-gpostdrop-u2526-s/bin/false-Mpostdrop
1,解壓源碼到/usr/local/src目錄下
#tarzxvfpostfix-2.6.5.tar.gz
#cdpostfix-2.6.5
#makemakefiles'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'
#make
#makeinstall
按照以下的提示輸入相關的路徑([]號中的是缺省值,”]”後的是輸入值,省略的表示採用默認值)
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]
readme_directory:[no]
生成別名二進制文件,這個步驟如果忽略,會造成postfix效率極低:
#newaliases
2.進行一些基本配置,測試啓動postfix並進行發信
#vi/etc/postfix/main.cf
修改以下幾項爲您需要的配置
myhostname=mail.a.com
myorigin=a.com
mydomain=a.com
mydestination=$myhostname,localhost.$mydomain,localhost,$mydomain
mynetworks=192.168.20.0/24,127.0.0.0/8
說明:
myorigin參數用來指明發件人所在的域名;
mydestination參數指定postfix接收郵件時收件人的域名,即您的postfix系統要接收到哪個域名的郵件;
myhostname參數指定運行postfix郵件系統的主機的主機名,默認情況下,其值被設定爲本地機器名;
mydomain參數指定您的域名,默認情況下,postfix將myhostname的第一部分刪除而作爲mydomain的值;
mynetworks參數指定你所在的網絡的網絡地址,postfix系統根據其值來區別用戶是遠程的還是本地的,如果是本地網絡用戶則允許其訪問;
inet_interfaces參數指定postfix系統監聽的網絡接口;
注意:
1、在postfix的配置文件中,參數行和註釋行是不能處在同一行中的;
2、任何一個參數的值都不需要加引號,否則,引號將會被當作參數值的一部分來使用;
3、每修改參數及其值後執行postfixreload即可令其生效;但若修改了inet_interfaces,則需重新啓動postfix;
4、如果一個參數的值有多個,可以將它們放在不同的行中,只需要在其後的每個行前多置一個空格即可;postfix會把第一個字符爲空格或tab的文本行視爲上一行的延續;
啓動postfix
/usr/local/postfix/sbin/postfixstart
3.連接postfix,驗正服務啓動狀況:
#telnetlocalhost25
Trying127.0.0.1...
Connectedtolocalhost.localdomain(127.0.0.1).
Escapecharacteris'^]'.
220mail.test.comESMTPPostfix
ehlomail.test.com
250-mail.test.com
250-PIPELINING
250-SIZE10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250DSN
mailfrom:[email protected]
2502.1.0Ok
rcptto:[email protected]
2502.1.5Ok
data
354Enddatawith<CR><LF>.<CR><LF>
subject:mailtest
mailtest
.
2502.0.0Ok:queuedas2C1CE1079E6
quit
2212.0.0Bye
Connectionclosedbyforeignhost.
4.切換到管理員進行收信:
[root@mail~]#mail
Mailversion8.16/6/93.Type?forhelp.
"/var/spool/mail/root":2messages2new
>[email protected]:1242/1578"Logwatchforlocalhost.localdomain(Linux)"
[email protected]:2214/435"mailtest"
收發郵件成功
5.但是沒有身份驗證,使用postfix結合cyrus-sasl進行身份驗證
在/usr/lib/sasl2/目錄下有sendmai.conf文件,而postfix結合sasl是要靠smtpd.conf,所以拷貝sendmail.conf爲smtpd.conf
cp-pSendmail.confsmtpd.conf
servicesaslauthdrestart
強制啓用身份驗證打開postfix的配置文件
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=Welcometoour$myhostnameESMTP,Warning:VersionnotAvailable!
三、
1.安裝Courierauthenticationlibrary連接數據庫進行身份驗證的程序
#tarjxvfcourier-authlib-0.62.4.tar.bz2
#cdcourier-authlib-0.62.4
#./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\與mysql連接驗證的說明文件
--with-authdaemonrc=/etc/authdaemonrc\與mysql連接的所要調用的模塊文件
--with-ltdl-lib=/usr/lib\
--with-ltdl-include=/usr/include
#make
#makeinstall
courier-authlib是以文件sock來提供服務的
#chmod755/usr/local/courier-authlib/var/spool/authdaemon
#cp/etc/authdaemonrc.dist/etc/authdaemonrc
#cp/etc/authmysqlrc.dist/etc/authmysqlrc
2.修改/etc/authdaemonrc文件
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10
編輯/etc/authmysqlrc爲以下內容,其中2525,2525爲postfix用戶的UID和GID。
MYSQL_SERVERlocalhost
MYSQL_PORT3306(指定你的mysql監聽的端口,這裏使用默認的3306)
MYSQL_USERNAMEextmail(這時爲後文要用的數據庫的所有者的用戶名)
MYSQL_PASSWORDextmail(密碼)
MYSQL_SOCKET/var/lib/mysql/mysql.sock
MYSQL_DATABASEextmail
MYSQL_USER_TABLEmailbox
MYSQL_CRYPT_PWFIELDpassword
MYSQL_UID_FIELD'2525'
MYSQL_GID_FIELD'2525'
MYSQL_LOGIN_FIELDusername
MYSQL_HOME_FIELDconcat('/var/mailbox/',homedir)
MYSQL_NAME_FIELDname
MYSQL_MAILDIR_FIELDconcat('/var/mailbox/',maildir)
3.拷貝cpcourier-authlib的服務腳本的控制文件的樣例文件到目錄下/etc/init.d/courier-authlib
#cpcourier-authlib.sysvinit/etc/init.d/courier-authlib
#chmod755/etc/init.d/courier-authlib
#chkconfig--addcourier-authlib
#chkconfig--level2345courier-authlibon
#echo"/usr/local/courier-authlib/lib/courier-authlib">>/etc/ld.so.conf.d/courier-authlib.conf
#ldconfig-v
#servicecourier-authlibstart(啓動服務)
新建虛擬用戶郵箱所在的目錄,並將其權限賦予postfix用戶:
#mkdir-pv/var/mailbox
#chown–Rpostfix/var/mailbox
4.接下來重新配置SMTP認證,編輯/usr/local/lib/sasl2/smtpd.conf,確保其爲以下內容:
pwcheck_method:authdaemond
log_level:3
mech_list:PLAINLOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
四、讓postfix支持虛擬域和虛擬用戶
1、編輯/etc/postfix/main.cf,添加如下內容:
########################VirtualMailboxSettings########################
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
##########################QUOTASettings########################
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,theuser'smaildirhasoverdrawnhisdiskspacequota,pleaseTidyyourmailboxandtryagainlater.
virtual_overquota_bounce=yes
2、使用extman源碼目錄下docs目錄中的extmail.sql和init.sql建立數據庫:
#tarzxvfextman-1.1.tar.gz
#cdextman-1.1/docs
-------------------------------------------------------------------------------------------------------------
#mysql-uroot-p<extmail.sql
#mysql-uroot-p<init.sql
mysql-uroot-p進行驗證
#cpmysql*/etc/postfix/
3、授予用戶extmail訪問extmail數據庫的權限
mysql>GRANTallprivilegesonextmail.*TOextmail@localhostIDENTIFIEDBY'extmail';
mysql>GRANTallprivilegesonextmail.*[email protected]'extmail';
mysql>FLUSHPRIVILEGES;讓設置的內容生效
cpmysql_virtual_*/etc/postfix/
servicepostfixrestart
說明:啓用虛擬域以後,需要取消中心域,即註釋掉myhostname,mydestination,mydomain,myorigin幾個指令;當然,你也可以把mydestionation的值改爲你自己需要的。
五、配置dovecot
#vi/etc/dovecot.conf
mail_location=maildir:/var/mailbox/%d/%n/Maildir
……
authdefault{
mechanisms=plain
passdbsql{
args=/etc/dovecot-mysql.conf
}
userdbsql{
args=/etc/dovecot-mysql.conf
}
……
把userdb的其他相關禁用
vim/etc/postfix/main.cf
#postfix的配置文件也要該
vim/etc/postfix/main.cf
home_mailbox=Maildir/
#vi/etc/dovecot-mysql.conf
driver=mysql
connect=host=localhostdbname=extmailuser=extmailpassword=extmail(如mysql源碼安裝,host=/tmp/mysql.sock)
default_pass_scheme=CRYPT
password_query=SELECTusernameASuser,passwordASpasswordFROMmailboxWHEREusername='%u'
user_query=SELECTmaildir,uidnumberASuid,gidnumberASgidFROMmailboxWHEREusername='%u'
接下來啓動dovecot服務:
#servicedovecotstart
#chkconfigdovecoton
六、安裝Extmail-1.2(先安裝httpd)
1、安裝
#tarzxvfextmail-1.2.tar.gz
#mkdir-pv/var/www/extsuite
#mvextmail-1.2/var/www/extsuite/extmail
#cp/var/www/extsuite/extmail/webmail.cf.default/var/www/extsuite/extmail/webmail.cf
2、修改主配置文件
#vi/var/www/extsuite/extmail/webmail.cf
部分修改選項的說明:
SYS_MESSAGE_SIZE_LIMIT=5242880
用戶可以發送的最大郵件
SYS_USER_LANG=en_US
語言選項,可改作:
SYS_USER_LANG=zh_CN
SYS_MAILDIR_BASE=/home/domains
此處即爲您在前文所設置的用戶郵件的存放目錄,可改作:
SYS_MAILDIR_BASE=/var/mailbox
SYS_MYSQL_USER=db_user
SYS_MYSQL_PASS=db_pass
以上兩句句用來設置連接數據庫服務器所使用用戶名、密碼和郵件服務器用到的數據庫,這裏修改爲:
SYS_MYSQL_USER=extmail
SYS_MYSQL_PASS=extmail
SYS_MYSQL_HOST=localhost
指明數據庫服務器主機名,這裏默認即可
SYS_MYSQL_TABLE=mailbox
SYS_MYSQL_ATTR_USERNAME=username
SYS_MYSQL_ATTR_DOMAIN=domain
SYS_MYSQL_ATTR_PASSWD=password
以上用來指定驗正用戶登錄裏所用到的表,以及用戶名、域名和用戶密碼分別對應的表中列的名稱;這裏默認即可
SYS_AUTHLIB_SOCKET=/var/spool/authdaemon/socket
此句用來指明authdaemosocket文件的位置,這裏修改爲:
SYS_AUTHLIB_SOCKET=/usr/local/courier-authlib/var/spool/authdaemon/socket
3、apache相關配置
由於extmail要進行本地郵件的投遞操作,故必須將運行apache服務器用戶的身份修改爲您的郵件投遞代理的用戶;本例中打開了apache服務器的suexec功能,故使用以下方法來實現虛擬主機運行身份的指定。此例中的MDA爲postfix自帶,因此將指定爲postfix用戶:
<VirtualHost*:80>
ServerNamemail.test.com
DocumentRoot/var/www/extsuite/extmail/html/
ScriptAlias/extmail/cgi/var/www/extsuite/extmail/cgi
Alias/extmail/var/www/extsuite/extmail/html
SuexecUserGrouppostfixpostfix
</VirtualHost>
修改cgi執行文件屬主爲apache運行身份用戶:
#chown-Rpostfix.postfix/var/www/extsuite/extmail/cgi/
如果您沒有打開apache服務器的suexec功能,也可以使用以下方法解決:
#vi/etc/httpd/httpd.conf
Userpostfix
Grouppostfix
<VirtualHost*:80>
ServerNamemail.test.com
DocumentRoot/var/www/extsuite/extmail/html/
ScriptAlias/extmail/cgi/var/www/extsuite/extmail/cgi
Alias/extmail/var/www/extsuite/extmail/html
</VirtualHost>
4、依賴關係的解決
extmail將會用到perl的Unix::syslogd功能,您可以去http://search.cpan.org搜索下載原碼包進行安裝。
#tarzxvfUnix-Syslog-0.100.tar.gz
#cdUnix-Syslog-0.100
#perlMakefile.PL
#make
#makeinstall
5、啓動apache服務
#servicehttpdstart
#chkconfighttpdon
七、安裝Extman-1.1
1、安裝及基本配置
#tarzxvfextman-1.1.tar.gz
#mvextman-1.1/var/www/extsuite/extman
修改配置文件以符合本例的需要:
#cp/var/www/extsuite/extman/webman.cf.default/var/www/extsuite/extman/webman.cf
#vi/var/www/extsuite/extman/webman.cf
SYS_MAILDIR_BASE=/home/domains
此處即爲您在前文所設置的用戶郵件的存放目錄,可改作:
SYS_MAILDIR_BASE=/var/mailbox
修改
SYS_CAPTCHA_ON=1
爲
SYS_CAPTCHA_ON=0
修改cgi目錄的屬主:
#chown-Rpostfix.postfix/var/www/extsuite/extman/cgi/
在apache的主配置文件中Extmail的虛擬主機部分,添加如下兩行:
ScriptAlias/extman/cgi/var/www/extsuite/extman/cgi
Alias/extman/var/www/extsuite/extman/html
創建其運行時所需的臨時目錄,並修改其相應的權限:
#mkdir-pv/tmp/extman
#chownpostfix.postfix/tmp/extman
好了,到此爲止,重新啓動apache服務器後,您的Webmail和Extman已經可以使用了,可以在瀏覽器中輸入指定的虛擬主機的名稱進行訪問
選擇管理即可登入extman進行後臺管理了。默認管理帳號爲:[email protected]密碼爲:extmail*123*
創建虛擬域b.com
在b.com域中註冊虛擬賬號user1
使用[email protected]登錄extmail,進行在線收發郵件