Postfix郵件服務器架構

 用alternatives --display mta 查看一下自己的默認MTA

  1. # alternatives --display mta  
  2. mta - 狀態是自動。 
  3.  鏈接目前指向 /usr/sbin/sendmail.sendmail 
  4. /usr/sbin/sendmail.sendmail - 優先度 90 
  5.  從 mta-mailq:/usr/bin/mailq.sendmail 
  6.  從 mta-newaliases:/usr/bin/newaliases.sendmail 
  7.  從 mta-rmail:/usr/bin/rmail.sendmail 
  8.  從 mta-sendmail:/usr/lib/sendmail.sendmail 
  9.  從 mta-pam:/etc/pam.d/smtp.sendmail 
  10.  從 mta-sendmailman:/usr/share/man/man8/sendmail.sendmail.8.gz 
  11.  從 mta-mailqman:/usr/share/man/man1/mailq.sendmail.1.gz 
  12.  從 mta-newaliasesman:/usr/share/man/man1/newaliases.sendmail.1.gz 
  13.  從 mta-aliasesman:/usr/share/man/man5/aliases.sendmail.5.gz 
  14. 當前“最佳”版本是 /usr/sbin/sendmail.sendmail。 

我的郵件系統默認爲sendmail,我們需要的是Postfix。 

關閉sendmail服務。

因爲系統運行需要郵件服務,Postfix沒安裝啊前,暫時不用#rpm -e sendmail卸載此郵件服務

  1. # service sendmail stop 
  2. 關閉 sm-client:[確定] 
  3. 關閉 sendmail:[確定] 
  4. # chkconfig sendmail off 

Postfix最新版本爲 Postfix 2.9 experimental release,我們下載穩定版本Postfix 2.8 stable release

  1. # wget http://postfix.it-austria.net/releases/official/postfix-2.8.4.tar.gz  

!!!安裝數據庫

在安裝Postfix2.8時,需要指定數據庫的位置,我們先來下載安裝最新的Mysql-5.5.14

  1. # wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.14.tar.gz/from/http://mysql.stu.edu.tw/ 

安裝一下Mysql所依賴的軟件gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libmcrypt* libtool* expect

  1. # yum -y install gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libmcrypt* libtool* expect 

創建Mysql安裝目錄及數據存放目錄

  1. # mkdir /usr/local/mysql 
  2. # mkdir /var/mysqldata 

創建用戶和組

  1. # groupadd mysql 
  2. # useradd -g mysql mysql 

賦予數據存放的權限

  1. # chown mysql.mysql -R /var/mysqldata/ 

安裝cmake

我用的是最新的Mysql-5.5.14,Mysql5.5以後是用cmake來編譯的,我們下載並安裝cmake

  1. # wget http://www.cmake.org/files/v2.8/cmake-2.8.5.tar.gz 
  2. # tar zxvf cmake-2.8.5.tar.gz 
  3. # cd cmake-2.8.5 
  4. # ./configure 
  5. # make && make install 

安裝Bison

看M4安裝的位置並寫入環境變量

  1. # whereis m4 
  2. # vi /etc/exports 
  3. PATH=$PATH:/usr/bin/m4 

下載並安裝Bison

  1. # wget ftp://mirrors.kernel.org/gnu/bison/bison-2.5.tar.gz 
  2. # tar zxvf bison-2.5.tar.gz 
  3. # cd bison-2.5 
  4. # ./configure --prefix=/usr/local/bison --with-libiconv-prefix=/usr/local/lib
  5. # make
  6. # make install

添加Bison到環境變量並刷新環境變量(或 yum -y install bison)

  1. # vi /etc/exports 
  2. PATH=$PATH:/usr/bin/bison 
  3. # source /etc/exports

安裝Mysql

  1. # tar zxvf mysql-5.5.14.tar.gz 
  2. # cd mysql-5.5.14 
  3. # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 
  4. -DMYSQL_UNIX_ADDR=/var/mysqldata/mysql.sock \ 
  5. -DDEFAULT_CHARSET=utf8 \ 
  6. -DDEFAULT_COLLATION=utf8_general_ci \ 
  7. -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \ 
  8. -DWITH_MYISAM_STORAGE_ENGINE=1 \ 
  9. -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 
  10. -DWITH_READLINE=1 \ 
  11. -DENABLED_LOCAL_INFILE=1 \ 
  12. -DMYSQL_DATADIR=/var/mysqldata \ 
  13. -DMYSQL_TCP_PORT=3306
  14. # make
  15. # make install

複製配置文件

  1. # cp support-files/my-medium.cnf /etc/my.cnf 

初始化數據

初始化前需要賦值給scripts/mysql_install_db執行權限

  1. # chmod 755 scripts/mysql_install_db 
  2. # scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/var/mysqldata/ 

設置開機啓動Mysql

  1. # cp support-files/mysql.server /etc/init.d/mysql 
  2. # chmod 755 /etc/init.d/mysql 
  3. # chkconfig mysql on 

爲Mysql添加環境變量並添加兩個別名方便開啓(#source /etc/profile 刷新後起效)

  1. # vi /etc/profile 
  2. export PATH=/usr/local/mysql/bin:$PATH 
  3. alias mysql_start="mysqld_safe &" 
  4. alias mysql_stop="mysqladmin –u root -p shutdown" 

啓動Mysql

  1. # /etc/init.d/mysql start 

設置密碼

數據庫的密碼開始爲空

  1. # /usr/local/mysql/bin/mysqladmin -u root -p password 

 !!!安裝Postfix

爲Postfix添加用戶和組

  1. # groupadd -g 2525 postfix 
  2. # useradd -g postfix -u 2525 -s /sbin/nologin -M postfix 
  3. # groupadd -g 2526 postdrop 
  4. # useradd -g postdrop -u 2526 -s /bin/false -M postdrop 

安裝以下開發所用到的rpm包組

  1. #yum groupinstall -y "Development Libraries"  "Development Tools" "Legacy Software Development"  "X Software Development"  

啓動saslauth認證並加入自動啓動中

  1. # service saslauthd start 
  2. # chkconfig saslauthd on 

 

安裝Postfix

  1. # tar zxvf postfix-2.8.4.tar.gz  
  2. # cd postfix-2.8.4 
  3. # make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2 -lssl -lcrypto'
  4. # make
  5. # make install

 在安裝的時候如果報 bin/postconf: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

  1. # vi /etc/ld.so.conf 
  2. /usr/local/mysql/lib 
  3. # ldconfig 

我的安裝選項 默認也行

  1. install_root: [/] / 
  2. tempdir: [/usr/local/src/postfix-2.8.4] /tmp 
  3. config_directory: [/etc/postfix]  
  4. command_directory: [/usr/sbin]  
  5. daemon_directory: [/usr/libexec/postfix]  
  6. data_directory: [/var/lib/postfix]  
  7. html_directory: [no]  
  8. mail_owner: [postfix]  
  9. mailq_path: [/usr/bin/mailq]  
  10. manpage_directory: [/usr/local/man]  
  11. newaliases_path: [/usr/bin/newaliases]  
  12. queue_directory: [/var/spool/postfix]  
  13. readme_directory: [no]  
  14. sendmail_path: [/usr/sbin/sendmail]  
  15. setgid_group: [postdrop]  

(注意)生成別名二進制文件,這個步驟如果忽略,會造成postfix效率極低:

  1. #  newaliases 

已經安裝好postfix了,修改一下配置選項並啓動測試

  1. # vi /etc/postfix/main.cf 
  2. myhostname = mail.test.com   
  3. myorigin = test.com   
  4. mydomain = test.com   
  5. mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain   
  6. mynetworks = 192.168.1.0/24, 127.0.0.0/8   

說明:
myorigin參數用來指明發件人所在的域名;
mydestination參數指定postfix接收郵件時收件人的域名,即您的postfix系統要接收到哪個域名的郵件;
myhostname 參數指定運行postfix郵件系統的主機的主機名,默認情況下,其值被設定爲本地機器名;
mydomain參數指定您的域名,默認情況下,postfix將myhostname的第一部分刪除而作爲mydomain的值;
mynetworks 參數指定你所在的網絡的網絡地址,postfix系統根據其值來區別用戶是遠程的還是本地的,如果是本地網絡用戶則允許其訪問;
inet_interfaces 參數指定postfix系統監聽的網絡接口;


啓動postfix

  1. # /usr/sbin/postfix start 

連接Postfix,驗證服務狀況

  1. #netstat -tnlp | grep :25   
  2. tcp   0    0 0.0.0.0:25   0.0.0.0:*     LISTEN   15497/master   
  3. # telnet localhost 25   
  4. Trying 127.0.0.1...   
  5. Connected to localhost.localdomain (127.0.0.1).   
  6. Escape character is '^]'.   
  7. 220 mail.test.com ESMTP Postfix   
  8. ehlo mail.test.com     //本postfix的hostname字段; 
  9. 250-mail.test.com   
  10. 250-PIPELINING   
  11. 250-SIZE 10240000   
  12. 250-VRFY   
  13. 250-ETRN   
  14. 250-ENHANCEDSTATUSCODES   
  15. 250-8BITMIME   
  16. 250 DSN   
  17. mail from:[email protected]   //輸入發信人郵箱   
  18. 250 2.1.0 Ok   
  19. rcpt to:[email protected]  //輸入收信人郵箱   
  20. 250 2.1.5 Ok   
  21. data                     //輸入郵件數據信息   
  22. subject:Mail test!       //輸入郵件主題   
  23. hello hanfeng!!!         //輸入郵件內容   
  24. .                        //輸入結束符.   
  25. 250 2.0.0 Ok: queued as C55863E016D   
  26. quit                    //輸入quit退出郵件系統   
  27. 221 2.0.0 Bye   
  28. Connection closed by foreign host.   
  29. [root@station123 ~]#grep C55863E016D /var/log/maillog //查找發信狀態   

爲postfix開啓cyrus-sasl認證

使用一下命令驗證Postfix是否支持cyrus-sasl認證,如果輸出結果如下,則是支持的:

  1. # /usr/sbin/postconf -a 
  2. cyrus 
  3. dovecot 

# vi /etc/postfix/main.cf
添加一下內容

  1. ############################CYRUS-SASL############################   
  2. broken_sasl_auth_clients = yes  
  3.   
  4. smtpd_recipient_restrictions=permit_mynetworks,  //下一行與此行爲同一行,下一行開頭,空一格   
  5.  permit_sasl_authenticated,reject_invalid_hostname,   
  6.  reject_non_fqdn_hostname,reject_unknown_sender_domain,   
  7.  reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,   
  8.  reject_unauth_pipelining,reject_unauth_destination   
  9.   
  10. smtpd_sasl_auth_enable = yes  
  11. smtpd_sasl_local_domain = $myhostname   
  12. smtpd_sasl_security_options = noanonymous  
  13. smtpdsmtpd_sasl_application_name = smtpd   
  14. smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!   

#vi /usr/lib/sasl2/smtpd.conf
添加如下內容:

  1. pwcheck_method: saslauthd 
  2. mech_list: PLAIN LOGIN 

重新加載Postfix

  1. # /usr/sbin/postfix reload 

下載並安裝courier-authlib(CFLAGS="-march=i686 -O2 -fexpensive-optimizations" CXXFLAGS="-march=i686 -O2 -fexpensive-optimizations" 這個選項放在後面就錯,沒加載不知道有什麼影響)

  1. # wget http://downloads.sourceforge.net/project/courier/authlib/0.63.0/courier-authlib-0.63.0.tar.bz2?r=http%3A%2F%2Fwww.courier-mta.org%2Fdownload.php&ts=1311251851&use_mirror=ncu
  2. # tar jxvf courier-authlib-0.63.0.tar.bz2 
  3. # cd courier-authlib-0.63.0
  4. # ./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --with-authmysql --with-mysql-libs=/usr/local/mysql/lib --with-mysql-includes=/usr/local/mysql/include --with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc
  5. # make
  6. # make install

 

 

  1. # chmod 755 /usr/local/courier-authlib/var/spool/authdaemon 
  2. # cp /etc/authdaemonrc.dist  /etc/authdaemonrc 
  3. # cp /etc/authmysqlrc.dist  /etc/authmysqlrc 
  4. # vi /etc/authdaemonrc 
  5. authmodulelist="authmysql"  
  6. authmodulelistorig="authmysql"  
  7. daemons=10  
  8. # vi /etc/authmysqlrc  爲以下內容,其中2525,2525 爲postfix 用戶的UID和GID。
  9.     MYSQL_SERVER localhost   
  10.     MYSQL_PORT 3306                   (指定你的mysql監聽的端口,這裏使用默認的3306)   
  11.     MYSQL_USERNAME  extmail           (這時爲後文要用的數據庫的所有者的用戶名)   
  12.     MYSQL_PASSWORD extmail            (密碼)   
  13.     MYSQL_SOCKET  /var/mysqldata/mysql.sock   
  14.     MYSQL_DATABASE  extmail   
  15.     MYSQL_USER_TABLE  mailbox   
  16.     MYSQL_CRYPT_PWFIELD  password   
  17.     MYSQL_UID_FIELD  '2525'   
  18.     MYSQL_GID_FIELD  '2525'   
  19.     MYSQL_LOGIN_FIELD  username   
  20.     MYSQL_HOME_FIELD  concat('/var/mailbox/',homedir)   
  21.     MYSQL_NAME_FIELD  name   
  22.     MYSQL_MAILDIR_FIELD  concat('/var/mailbox/',maildir)   
  23.     !!!保證此文件的內容不能有錯,否則不能啓用postfix與mysql的連接;   
  1. # cp courier-authlib.sysvinit /etc/init.d/courier-authlib 
  2. # chmod 755 /etc/init.d/courier-authlib 
  3. # chkconfig --add courier-authlib 
  4. # chkconfig --level 2345 courier-authlib on 
  5. #echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf 
  6. # ldconfig -v 
  7. # service courier-authlib start   (啓動服務) 
  8.  Starting Courier authentication services: authdaemond (可信的, 可靠的) 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章