天天看點

利用yum安裝postfix郵件伺服器

一.首先介紹安裝環境:

    OS:centos5.4最小化安裝,選擇了元件 base,devlopment libraries  development tools editors text-base internet 這幾個元件,其他的一概不選。安裝系統完畢,直接進行yum update -y &更新。

    在終端輸入setup指令 選擇firewall configuration 将security level 與SELinux 改為disabled,目的是為了避免在安裝調試時産生不必要的麻煩,在系統安裝完成後,可以自己去調試安全部分,在這裡将不作詳細介紹。 

二.元件選擇說明:

    我們能用RPM 安裝的,我們将采用rpm 安裝,如果rpm沒有的,我們将建立自己的rpm。部分軟體簡介:

    1、AMP ---apache(web服務) 、mysql(資料庫)、php(非必需) 的簡稱

    2、postfix --mta 郵件系統核心

    3、courier-authlib--一個為courier-imap,maildrop,sasl2 提供使用者資訊的背景程序式

    4、courier-imap --提供pop3,pop3s,imap,imaps 服務的程式

    5、dspam clamd --提供内容過濾    

    6、extmail --一個著名的webmail程式

    7、extman --與extmail內建的背景管理程式

三:元件安裝

    1、安裝AMP

     [root@localhost ~]# yum -y install httpd httpd-devel mysql mysql-devel mysql-server php php-xml php-cli php-pdo php-mbstring php-mcrypt php-gd php-common php-devel php-mysql

    修改啟動配置并啟動

    [root@localhost ~]# chkconfig --level 345  mysqld on

    [root@localhost ~]# chkconfig --level 345  httpd on 

    [root@localhost ~]# service mysqld start

    [root@localhost ~]# service httpd start

    2.下載下傳extman(http://www.extmail.org/cgi-bin/download.cgi)

    [root@localhost ~]# tar zxf extman-1.1.tar.gz 

    [root@localhost ~]# cd extman-1.1/docs/

    [root@localhost docs]# mysql -uroot <extmail.sql 

    [root@localhost docs]# mysql -uroot <init.sql

    3、RPM 簡介

    下面我們将采用rpm 的方式安裝軟體

    /usr/src/redhat/SPEC spec 檔案夾,通常是rpm檔案夾

    /usr/src/redhat/RPMS 是生成rpm 的檔案,下面還有i368,i486 等子檔案夾,一般我們用i386 架構

    /usr/src/redhat/SOURCE 源檔案檔案夾

    為了安裝時切換目錄的友善,我們将目錄存放到一個shell變量裡。

    [root@localhost ~]# export RPMS=/usr/src/redhat/RPMS

    [root@localhost ~]# export SOURCE=/usr/src/redhat/SOURCES

    [root@localhost ~]# export SPEC=/usr/src/redhat/SPECS

    在下面安裝過程中,大家要記住cd $RPMS 與cd /usr/src/redhat/RPMS 的目的是一樣的,隻是shell變量的替換。

    4、安裝authlib (courier-authlib-0.63.0)

    A、安裝依懶的包

    [root@localhost ~]# yum -y install postgresql-devel expect libtool-ltdl-devel

    [root@localhost ~]# wget http://prdownloads.sourceforge.net/courier/courier-authlib-0.63.0.tar.bz2

    [root@localhost ~]# tar xjf courier-authlib-0.63.0.tar.bz2 

    [root@localhost ~]# cd courier-authlib-0.63.0

    [root@localhost courier-authlib-0.63.0]# cp courier-authlib.spec $SPEC

    [root@localhost courier-authlib-0.63.0]# cd ..

    [root@localhost ~]# cp courier-authlib-0.63.0.tar.bz2 $SOURCE

    [root@localhost ~]# cd $SPEC

    [root@localhost SPECS]# rpmbuild -bb courier-authlib.spec (建立rpm檔案)

    B、安裝生成的rpm

    [root@localhost SPECS]# cd $RPMS/i386

    [root@localhost i386]# rpm -ivh courier-authlib-0.63.0-1.i386.rpm 

    [root@localhost i386]# rpm -ivh courier-authlib-devel-0.63.0-1.i386.rpm 

    [root@localhost i386]# rpm -ivh courier-authlib-mysql-0.63.0-1.i386.rpm 

    C、配置authlibdaemon

    [root@localhost i386]# cd /etc/authlib/

    [root@localhost authlib]# mv authdaemonrc authdaemonrc.bak

    [root@localhost authlib]# vim authdaemonrc

    輸入下面的内容:

    authmodulelist="authmysql"

    authmodulelistorig="authmysql"

    daemons=10

    authdaemonvar=/var/spool/authdaemon

    DEBUG_LOGIN=2

    DEFAULTOPTIONS="wbnodsn=1"

    LOGGEROPTS=""

    D、配置authmysqlrc

    [root@localhost authlib]# mv authmysqlrc authmysqlrc.bak

    [root@localhost authlib]# vim authmysqlrc

    MYSQL_SERVER    localhost

    MYSQL_USERNAME  extmail

    MYSQL_PASSWORD  extmail

    MYSQL_PORT      0

    MYSQL_SOCKET    /var/lib/mysql/mysql.sock

    MYSQL_OPT       0

    MYSQL_DATABASE  extmail

    MYSQL_SELECT_CLAUSE     SELECT username,password,"",uidnumber,gidnumber,\

                        CONCAT('/home/domains/',homedir),               \

                        CONCAT('/home/domains/',maildir),               \

                        quota,                                          \

                        name                                            \

                        FROM mailbox                                    \

                        WHERE username = '$(local_part)@$(domain)' 

    E、啟動authlib

    [root@localhost ~]# service courier-authlib start

    Starting Courier authentication services: authdaemond

    [root@localhost ~]# chmod 777 /var/spool/authdaemon/

    5.安裝courier-imap

    由于courier-imap 不能用root身份生成rpm那麼我們建立一個普通使用者,并建立一個rpm環境

    [root@localhost ~]# useradd yiyou 在這裡添加自己的使用者

    軟體包依懶需要安裝openldap-server,rpm建立完成後可以删除

    [root@localhost ~]# yum -y install openldap-servers

    [root@localhost ~]# su - yiyou   

    [yiyou@localhost ~]$ mkdir -p rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

    [yiyou@localhost ~]$ vim  ~/.rpmmacros

    輸入内容:

    %_topdir /home/yiyou/rpm

    [yiyou@localhost ~]$ wget http://prdownloads.sourceforge.net/courier/courier-imap-4.7.0.tar.bz2

    [yiyou@localhost ~]$ cp courier-imap-4.7.0.tar.bz2 rpm/SOURCES/

    [yiyou@localhost ~]$ tar xjf courier-imap-4.7.0.tar.bz2 

    [yiyou@localhost ~]$ cd courier-imap-4.7.0

    [yiyou@localhost courier-imap-4.7.0]$ cp courier-imap.spec ../rpm/SPECS/

    [yiyou@localhost courier-imap-4.7.0]$ cd ../rpm/SPECS/

    編輯courier-imap.spec 将下面的内容:

    %if %suse_version

    BuildRequires: rpm >= 3.0.5 /usr/bin/sed openldap2 openldap2-devel %([ %{suse_version} -gt 819 ] && echo /usr/include/fam.h)

    %else

    BuildRequires: rpm >= 4.0.2 sed /usr/include/fam.h openldap-devel openldap-servers

    %endif

    改為:将此處fam.h 去掉 此處好像沒有openldap相關選項 2010-1-12

    %if %suse_version   在這裡好像提示沒有安裝openldap-server yum install openldap* 還真沒裝

    BuildRequires: rpm >= 3.0.5 /usr/bin/sed openldap2 openldap2-devel 

    BuildRequires: rpm >= 4.0.2 sed openldap-devel openldap-servers

    建立rpm

    [yiyou@localhost SPECS]$ rpmbuild -bb courier-imap.spec 

    完成rpm後,輸入exit退出目前使用者。

    [root@localhost ~]# rpm -ivh /home/yiyou/rpm/RPMS/i386/courier-imap-4.7.0-1.i386.rpm 

    [root@localhost ~]# service courier-imap start

    6.安裝cyrus-sasl

    [root@localhost ~]# yum install -y cyrus-sasl-devel cyrus-sasl-plain cyrus-sasl cyrus-sasl-lib

    A.配置sasl2

    建立/usr/lib/sasl2/smtpd.conf 檔案,輸入下面的内容:

    pwcheck_method:authdaemond

    log_level:3

    mech_list:PLAIN LOGIN

    authdaemond_path:/var/spool/authdaemon/socket

    7、安裝postfix 更新至2.6.5

    A、下載下傳軟體包

    [root@localhost i386]# cd

    [root@localhost ~]# wget http://ftp.wl0.org/official/2.6/SRPMS/postfix-2.6.5-1.src.rpm

    [root@localhost ~]# rpm -ivh postfix-2.6.5-1.src.rpm 

    編輯postfix.spec 修改下面的選項

    %define distribution rhel-5.0

    %define with_mysql_redhat 1

    %define with_sasl         2

    %define with_vda          1

    %define with_pcre         1

    B、建立rpm

    [root@localhost SPECS]# rpmbuild -bb postfix.spec 

    [root@localhost RPMS]# cd $RPMS/i386

    [root@localhost i386]# rpm -ivh postfix-2.6.5-1.rhel5.i386.rpm 

    C、切換mta

    [root@localhost i386]# alternatives --config mta (選擇2,postfix)

    [root@localhost i386]# rpm -e sendmail

    [root@localhost i386]# cd /usr/sbin/

    [root@localhost sbin]# mv sendmail sendmail.bak

    [root@localhost sbin]# newaliases

    [root@localhost sbin]# ln -s sendmail.postfix sendmail

    D、配置postfix

    [root@localhost ~]# cd /etc/postfix/

    [root@localhost postfix]# mv main.cf main.cf.bak

    [root@localhost postfix]# vim main.cf

    queue_directory = /var/spool/postfix

    command_directory = /usr/sbin

    daemon_directory = /usr/libexec/postfix

    data_directory = /var/lib/postfix

    mail_owner = postfix

    unknown_local_recipient_reject_code = 550

    debug_peer_level = 2

    debugger_command =

         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin

         ddd $daemon_directory/$process_name $process_id & sleep 5

    sendmail_path = /usr/sbin/sendmail.postfix

    newaliases_path = /usr/bin/newaliases.postfix

    mailq_path = /usr/bin/mailq.postfix

    setgid_group = postdrop

    html_directory = /usr/share/doc/postfix-2.6.5-documentation/html

    manpage_directory = /usr/share/man

    sample_directory = /etc/postfix

    readme_directory = /usr/share/doc/postfix-2.6.5-documentation/readme

    alias_database = hash:/etc/postfix/aliases

    alias_maps = hash:/etc/postfix/aliases

    myhostname = mail.360book.com.cn(修改為自己的域名)

    mydomain = 360book.com.cn(修改為自己的域名)

    mydestination = $myhostname

    smtpd_banner = 360book.com.cn ESMTP Mail System(修改為自己的域名)

    message_size_limit = 14680064

    virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf

    virtual_mailbox_base = /home/domains

    virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf

    virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf

    virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_limit_maps.cf

    virtual_transport = maildrop

    maildrop_destination_recipient_limit = 1

    #sasl

    smtpd_sasl_auth_enable = yes

    smtpd_sasl2_auth_enable = yes

    smtpd_sasl_security_options = noanonymous

    broken_sasl_auth_clients = yes

    smtpd_recipient_restrictions =

     permit_mynetworks,

     permit_sasl_authenticated,

     reject_unauth_destination,

     reject_unauth_pipelining,

     reject_invalid_hostname

    dspam_destination_recipient_limit = 1

    複制extman/docs 目錄的cf 檔案到/etc/postfix

    [root@localhost postfix]# cp /root/extman-1.1/docs/mysql_virtual_* .

    因為配置檔案的使用者、密碼與資料庫時一樣,是以不用修改。

    [root@localhost postfix]# service postfix start

    9、安裝maildrop

    A、下載下傳生成RPM

    [root@localhost postfix]# cd

    [root@localhost ~]# wget http://prdownloads.sourceforge.net/courier/maildrop-2.3.0.tar.bz2

    [root@localhost ~]# cp maildrop-2.3.0.tar.bz2 $SOURCE

    [root@localhost ~]# tar xjf maildrop-2.3.0.tar.bz2 

    [root@localhost ~]# cd maildrop-2.3.0

    [root@localhost maildrop-2.3.0]# cp maildrop.spec $SPEC

    [root@localhost maildrop-2.3.0]# cd $SPEC

    編輯maildrop.spec 把下面:

    BuildRequires: /usr/include/fam.h gdbm-devel pcre-devel

    改為:

    BuildRequires:  gdbm-devel pcre-devel

    把下面:

    %configure --with-devel --enable-userdb --enable-maildirquota  \

    --enable-syslog=1 --enable-trusted-users='root mail daemon postmaster qmaild mmdf' \

    --enable-restrict-trusted=0 \

    --enable-sendmail=/usr/sbin/sendmail

    %configure --with-devel --enable-userdb --enable-maildirquota \

    --enable-restrict-trusted=0 --enable-sendmail=/usr/sbin/sendmail \

    --enable-authlib

    B、建立安裝maildrop rpm

    [root@localhost SPECS]# yum -y install pcre-devel

    [root@localhost SPECS]# rpmbuild -bb maildrop.spec 

    [root@localhost i386]# rpm -ivh maildrop-2.3.0-1.i386.rpm 

    C、添加虛似使用者及建立目錄

    [root@localhost i386]# cd  在這裡提示已經添加了使用者 應該是安裝postfix的時候建立的使用者

    [root@localhost ~]# groupadd -g 1000 vgroup

    [root@localhost ~]# useradd -g 1000 -u 1000 -s /sbin/nologin -d /dev/null vuser

    編輯/etc/postfix/master.cf ,加入下面的内容:

    maildrop unix -     n     n     -     -     pipe

     flags=DRhu user=vuser argv=/usr/bin/maildrop -d ${user}@${nexthop} ${recipient} ${user} ${extension} ${nexthop}

    D、建立郵件存儲目錄

    [root@localhost ~]# mkdir -p /home/domains/extmail.org/postmaster

    [root@localhost ~]# maildirmake /home/domains/extmail.org/postmaster/Maildir

    [root@localhost ~]# chown -R vuser:vgroup /home/domains/    

    E、測試maildrop

    [root@localhost ~]# echo "test" | maildrop -V 10 -d [email protected]

    maildrop: authlib: groupid=1000

    maildrop: authlib: userid=1000   這裡改成自己相應的

    maildrop: authlib:[email protected],

    home=/home/domains/extmail.org/postmaster, mail=/home/data/domains/extmail.org/postmaster/Maildir/

    maildrop: Changing to /home/data/domains/extmail.org/postmaster

    Message start at 0 bytes, envelope [email protected]

    maildrop: Attempting .mailfilter

    maildrop: Delivery complete.

    出現上以資訊說明authlib,maildrop 工作正常

    F、測試pop3

    [root@localhost ~]# telnet localhost 110

    Trying 127.0.0.1...

    Connected to localhost.localdomain (127.0.0.1).

    Escape character is '^]'.

    +OK Hello there.

    user [email protected]

    +OK Password required.

    pass extmail

    +OK logged in.

    list

    +OK POP3 clients that break here, they violate STD53.

    1 6

    .

    retr 1

    +OK 6 octets follow.

    test

    quit

    +OK Bye-bye.

    Connection closed by foreign host.

    G、測試postfix

    [root@localhost ~]# perl -MMIME::Base64 -e 'print encode_base64("postmaster\@extmail.org");'

    cG9zdG1hc3RlckBleHRtYWlsLm9yZw==

    [root@localhost ~]# perl -MMIME::Base64 -e 'print encode_base64("extmail");'                

    ZXh0bWFpbA==

    [root@localhost ~]# service postfix restart (重新開機一下)

    [root@localhost ~]# telnet localhost 25

    220 yiyou.org ESMTP Mail System

    ehlo test.com

    250-mail.yiyou.org

    250-PIPELINING

    250-SIZE 14680064

    250-VRFY

    250-ETRN

    250-AUTH LOGIN PLAIN

    250-AUTH=LOGIN PLAIN

    250-ENHANCEDSTATUSCODES

    250-8BITMIME

    250 DSN

    auth login

    334 VXNlcm5hbWU6

    334 UGFzc3dvcmQ6

    235 2.7.0 Authentication successful

    221 2.0.0 Bye

    Connection closed by foreign host

    10.安裝extman/extmail

    添加emos 1.5的源 

    EMOS-Base

    #

    # Created by ExtMail Dev Team: http://www.extmail.org/

    # $Id$

    [EMOS-base]

    name=EMOS-Base

    baseurl=http://mirror.extmail.org/yum/emos/1.5/os/$basearch/

    gpgcheck=0

    priority=0

    protect=0

    [EMOS-update]

    name=EMOS-Updates

    baseurl=http://mirror.extmail.org/yum/emos/1.5/updates/$basearch/

    A、配置apache

    修改/etc/httpd/conf/httpd.conf,将下面的選項改為:

    User vuser

    Group vgroup

    在檔案最後輸入:Include /etc/httpd/conf.d/extmail.conf

    [root@localhost ~]# cd /etc/httpd/conf.d/

    [root@localhost conf.d]# vim extmail.conf

    Alias /extman/cgi/ /var/www/extsuite/extman/cgi/

    Alias /extman /var/www/extsuite/extman/html/

    <Location "/extman/cgi">

    SetHandler cgi-script

    Options +ExecCGI

    </Location>

    # config for ExtMail

    Alias /extmail/cgi/ /var/www/extsuite/extmail/cgi/

    Alias /extmail /var/www/extsuite/extmail/html/

    <Location "/extmail/cgi">

    [root@localhost ~]# mkdir /var/www/extsuite

    [root@localhost ~]# tar zxf extmail-1.2.tar.gz 

    [root@localhost ~]# cp -r extmail-1.2 /var/www/extsuite/extmail

    [root@localhost ~]# cp -r extman-1.1 /var/www/extsuite/extman 

    B、配置extmail參數

    [root@localhost ~]# cd /var/www/extsuite/extmail/

    [root@localhost extmail]# cp webmail.cf.default webmail.cf

    修改webmail.cf 下面的選項

    SYS_SESS_DIR = /tmp/extmail

    SYS_MAILDIR_BASE = /home/domains

    SYS_MYSQL_USER = extmail

    SYS_MYSQL_PASS = extmail

    C、配置extman參數

    [root@localhost extmail]# cd /var/www/extsuite/extman/

    修改webman.cf 下面的選項:

    D、建立session 目錄

    [root@localhost extman]# mkdir /tmp/{extman,extmail}

    [root@localhost extman]# chown vuser:vgroup /tmp/ext*

    E、配置背景日志分析工具

    [root@localhost addon]# cd /var/www/extsuite/extman/addon/

    [root@localhost addon]# cp -r mailgraph_ext /usr/local/

    将下面的内容加入/etc/rc.local

    /usr/local/mailgraph_ext/mailgraph-init start &

    /var/www/extsuite/extman/daemon/cmdserver --daemon &

    安裝所依懶的軟體包

    [root@localhost mailgraph_ext]# yum -y install rrdtool perl-rrdtool perl-GD perl-File-Tail

    F、啟動背景程序

    [root@localhost mailgraph_ext]# /usr/local/mailgraph_ext/mailgraph-init start

    Starting mail statistics grapher: mailgraph_ext

    G、通路extmail/extman

    打開浏覽器,輸入http://your_ip_address/extmail 注意:your_ip_address 為你實際的ip

    第一個測試使用者為:postmaster 密碼:extmail 域:extmail

    打開浏覽器,輸入http://your_ip_address/extman

    extman root 使用者為:[email protected] 密碼為:extmail*123*

    注意:進入系統後要修改密碼及找回密碼的提示問題、答案。

    11.安裝dspam clamd

    yum install clamd dspam*

    進入mysql

    create database dspam;

    grant all on dspam.* to dspam@localhost identified by 'dspam';

    cd /usr/share/dspam/sql/mysql/

    修改mysql_objects-4.1.sql   virtual_users.sql,在兩個檔案前添加 use dspam; 

    mysql -u dspam -p< mysql_objects-4.1.sql 

    mysql -u dspam -p< virtual_users.sql

    crontab -e

    0 0 * * * /usr/bin/mysql -u dspam -p'dspam' dspam < /usr/share/dspam/sql/mysql/purge-4.1.sql

    0 0 * * * /usr/bin/dspam_logrotate -a 30 -d /var/spool/dspam/data

    修改/etc/dspam/dspam.conf

    MySQLServer             /var/lib/mysql/mysql.sock

    MySQLUser               dspam

    MySQLPass               dspam

    MySQLDb                 dspam

    MySQLCompress           true

    MySQLUIDInSignature        on

    ClamAVPort                3310   //開啟clamd

    ClamAVHost                127.0.0.1

    ClamAVResponse        spam

    ServerPID              /var/run/dspam.pid

    ServerMode auto

    ServerParameters        "--user dspam --deliver=innocent,spam"

    ServerDomainSocketPath  "/tmp/dspam.sock"

    DeliveryHost        127.0.0.1

    DeliveryPort        10026

    DeliveryIdent       localhost

    DeliveryProto       SMTP

    ParseToHeaders on

    ChangeModeOnParse on

    ChangeUserOnParse full

    vi /etc/postfix/master.cf

    smtp      inet         n       -       n       -       -        smtpd

    To:

       -o content_filter=lmtp:unix:/tmp/dspam.sock(-o 前面有三個空格)

    lmtp      unix  -       -       n       -       -       lmtp

    127.0.0.1:10026 inet    n    -    n    -    -    smtpd

        -o content_filter=

        -o local_recipient_maps=

        -o relay_recipient_maps=

        -o smtpd_restriction_classes=

        -o smtpd_helo_restrictions=

        -o smtpd_sender_restrictions=

        -o smtpd_recipient_restrictions=permit_mynetworks,reject

        -o mynetworks=127.0.0.0/8

        -o strict_rfc821_envelopes=yes

        -o smtpd_error_sleep_time=0

        -o smtpd_soft_error_limit=1001

        -o smtpd_hard_error_limit=1000

        -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks

    配置dspam web 在/usr/share/dspam/webui/cgi-bin

    vi admins

    添加admin

    [root@localhost ~]#htpasswd -c .htpasswd admin

    xx

    /etc/init.d/dspam-webd start

    [root@localhost ~]#chkconfig --level 345 dspamd on

    [root@localhost ~]#chmod -R 777 /usr/share/dspam

    [root@localhost ~]#service dspamd start

    至此,整個postfix算是安裝完畢。重新開機計算機,通路:http://your ip/extmail/就能直接通路了。

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

繼續閱讀