所以天父的兒子(耶穌)若叫你們自由,你們就真自由了。(約翰福音 八:36)

2016/11/23 , 三 - 18:52 By 資深工讀生

安裝 Postfix 在 CentOS平台上必須設好您的主機名(hostname),同時確保 iptables 防火牆不會阻止任何標準的郵件port(25,465,587,110,995,143,993),如果使用不同形式的防火牆,請確認不會阻塞任何必要的 port。

安裝需要的套件

1.升級已安裝的套件:
#yum update

2.修改 CentOS的 yum文件:

修改/etc/yum.repos.d/CentOS-Base.repo文件
[base]
name=CentOS-$releasever - Base
exclude=postfix

#released updates
[updates]
name=CentOS-$releasever - Updates
exclude=postfix

3.安裝要使用的套件:Postfix,MariaDB 資料庫,Dovecot 的 IMAP和 POP後端伺服器
#yum --enablerepo=centosplus install postfix
#yum install dovecot mariadb-server dovecot-mysql

--------------------------

在 MariaDB中設定虛擬帳號

1. 設定 MariaDB 在CentOS 開機時啟動,並啟動 MariaDB:
#systemctl enable mariadb.service
#systemctl start mariadb.service

2. 執行 mysql_secure_installation ,對 MariaDB 進行安全調整:
#mysql_secure_installation

3. 登入MariaDB:
#mysql -u root -p

4. 新增一個 mail 資料庫並進入 mail資料庫:
>>CREATE DATABASE mail;
>>USE mail;

5. 新增一個 mail資料庫的管理帳號 mail_admin,並且設定密碼:
>>GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost' IDENTIFIED BY 'mail_admin_password';
>>GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost.localdomain' IDENTIFIED BY 'mail_admin_password';
>>FLUSH PRIVILEGES;
(mail_admin_password 請改成自己的 password)

6.新增虛擬域名 TABLE、郵件轉發 TABLE、用戶 TABLE、傳輸 TABLE:
>>CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );
>>CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );
>>CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
>>CREATE TABLE transport ( domain varchar(128) NOT NULL default '', transport varchar(128) NOT NULL default '', UNIQUE KEY domain (domain) );

7. 退出 MariaDB
>>quit

8. 在/etc/my.cnf 中,將 localhost 設為 127.0.0.1
bind-address=127.0.0.1

9.重新啟動 MariaDB :
#systemctl restart  mariadb.service

-------------------------------------

設定 Postfix 的通信參數

※請注意:
這裡假設 mail_admin 用戶密碼為 mail_admin_password,要根據實際需要做修改

1.新增虛擬網域設定檔 /etc/postfix/mysql-virtual_domains.cf
user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT domain AS virtual FROM domains WHERE domain='%s'
hosts = 127.0.0.1

2.新增虛擬轉發設定檔 /etc/postfix/mysql-virtual_forwardings.cf
user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1

3.新增虛擬信箱設定檔 /etc/postfix/mysql-virtual_mailboxes.cf
user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
hosts = 127.0.0.1

4.新增虛擬郵件映射設定檔 /etc/postfix/mysql-virtual_email2email.cf
user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT email FROM users WHERE email='%s'
hosts = 127.0.0.1

5.設定文件權限:
#chmod o= /etc/postfix/mysql-virtual_*.cf
#chgrp postfix /etc/postfix/mysql-virtual_*.cf

6.新增郵件處理的用戶和群組(vmail)。所有的信件將儲存在用戶的目錄下:(/home/vmail)
#groupadd -g 5000 vmail
#useradd -g vmail -u 5000 vmail -d /home/vmail -m

7.剩下的就是設定 Postfix。下面我們假設 mail 網域為 server.example.com
如果你有 SSL certificate and key,放置路徑為 /etc/pki/dovecot/private/dovecot.pem
#postconf -e 'myhostname = server.example.com'
#postconf -e 'mydestination = localhost, localhost.localdomain'
#postconf -e 'mynetworks = 127.0.0.0/8'
#postconf -e 'inet_interfaces = all'
#postconf -e 'message_size_limit = 30720000'
#postconf -e 'virtual_alias_domains ='
#postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf'
#postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf'
#postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf'
#postconf -e 'virtual_mailbox_base = /home/vmail'
#postconf -e 'virtual_uid_maps = static:5000'
#postconf -e 'virtual_gid_maps = static:5000'
#postconf -e 'smtpd_sasl_type = dovecot'
#postconf -e 'smtpd_sasl_path = private/auth'
#postconf -e 'smtpd_sasl_auth_enable = yes'
#postconf -e 'broken_sasl_auth_clients = yes'
#postconf -e 'smtpd_sasl_authenticated_header = yes'
#postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'
#postconf -e 'smtpd_use_tls = yes'
#postconf -e 'smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem'
#postconf -e 'smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem'
#postconf -e 'virtual_create_maildirsize = yes'
#postconf -e 'virtual_maildir_extended = yes'
#postconf -e 'proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps'
#postconf -e 'virtual_transport = dovecot'
#postconf -e 'dovecot_destination_recipient_limit = 1'

8.編輯 /etc/postfix/master.cf ,在檔案中增加 Dovecot 的設定:
dovecot   unix  -       n       n       -       -       pipe    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}
    
9.在 /etc/postfix/master.cf 中,開啟 submission 和 smtps 二個設定:
#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd
#smtp      inet  n       -       -       -       1       postscreen
#smtpd     pass  -       -       -       -       -       smtpd
#dnsblog   unix  -       -       -       -       0       dnsblog
#tlsproxy  unix  -       -       -       -       0       tlsproxy
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       -       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
 
10.重新啟動 Postfix:
#systemctl enable postfix.service
#systemctl start  postfix.service

---------------------------------------------------

Dovecot 設定

1.備份/etc/dovecot/dovecot.conf 檔案
#mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup

2.將下方的程式碼複製到 /etc/dovecot/dovecot.conf 檔案中:
其中在 37行的 example.com 是MailServer的網域,你的 ssl 憑證檔請放在第 5-6行:
-----------

protocols = imap pop3
log_timestamp = "%Y-%m-%d %H:%M:%S "
mail_location = maildir:/home/vmail/%d/%n/Maildir

ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem

namespace {
    type = private
    separator = .
    prefix = INBOX.
    inbox = yes
}

service auth {
    unix_listener auth-master {
        mode = 0600
        user = vmail
    }

    unix_listener /var/spool/postfix/private/auth {
        mode = 0666
        user = postfix
        group = postfix
    }

user = root
}

service auth-worker {
    user = root
}

protocol lda {
    log_path = /home/vmail/dovecot-deliver.log
    auth_socket_path = /var/run/dovecot/auth-master
    postmaster_address = [email protected]
}

protocol pop3 {
    pop3_uidl_format = %08Xu%08Xv
}

passdb {
    driver = sql
    args = /etc/dovecot/dovecot-sql.conf.ext
}

userdb {
    driver = static
    args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
}

3.在 /etc/dovecot/dovecot-sql.conf.ext 設定檔中,設定 MariaDB 資料庫的格式:
driver = mysql
connect = host=127.0.0.1 dbname=mail user=mail_admin password=mail_admin_password
default_pass_scheme = CRYPT
password_query = SELECT email as user, password FROM users WHERE email='%u';

4.設定 dovecot-sql.conf.ext 的群組和權限:
#chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext
#chmod o= /etc/dovecot/dovecot-sql.conf.ext

5.重新啟動 Dovecot,並設定 Dovecot 在系統啟動時啟動:
#systemctl enable dovecot.service
#systemctl start  dovecot.service

6.查看 /var/log/maillog 日誌檔,如果有類似以下記錄,證明你的 Dovecot 設定正確。
Mar 18 17:10:26 localhost postfix/postfix-script[3274]: starting the Postfix mail system
Mar 18 17:10:26 localhost postfix/master[3276]: daemon started -- version 2.10.1, configuration /etc/postfix
Mar 18 17:12:28 localhost dovecot: master: Dovecot v2.2.10 starting up for imap, pop3 (core dumps disabled)

7.安裝 Telnet 並測試 POP3:
#yum install telnet
#telnet localhost pop3

8.終端應輸出以下結果:
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
+OK Dovecot ready.


設定郵件的別名

1.編輯 /etc/aliases ,確保 postmaster 和 root 的別名:
postmaster: root
root: [email protected]

2.更新別名並重新啟動 Postfix:
#newaliases
#systemctl restart  postfix.service

目前已完成別名的設定。
接下來將測試 Postfix 確保它正常運行。

測試 Postfix

1.測試 Postfix 服務器的 SMTP 埠口:
#telnet localhost 25

2.在終端中,輸入以下命令:
#ehlo localhost

3.你應該在終端中看到下面的輸出:
250-hostname.example.com
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN
250-AUTH=PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

4.在終端中輸入 quit 指令退出 Postfix

下一步,在 MariaDB 資料庫中增加郵件網域和帳號。

增加和測試郵件網域和帳號

在下面的例子中,我們將在 MariaDB 資料庫中新曾郵件網域「example.com」,同時增加一個名為 sales 帳號。

1.登入MariaDB 服務器:
>>mysql -u root -p

2.進入「mail」資料庫,增加一個郵件網域 example.com 和 [email protected] 郵件帳號和密碼:
>>USE mail;
>>INSERT INTO domains (domain) VALUES ('example.com');
>>INSERT INTO users (email, password) VALUES ('[email protected]', ENCRYPT('password'));
>>quit

3.在訪問任何新增的電子郵件帳號前,需要發送一個測試訊息來新增該帳號信箱:
#yum install mailx
#mailx [email protected]
在終端中按 「Ctrl + D」 來結束測試郵件。
這就完成了一個新的網域和郵件帳號的設定。


檢查日誌

測試郵件發送後,請檢查日誌檔確保郵件已發送。

1.查看 /var/log/maillog maillog日誌檔,你應該看見類似記錄:
Mar 18 17:18:47 localhost postfix/cleanup[3427]: B624062FA: message-id=<[email protected]>
Mar 18 17:18:47 localhost postfix/qmgr[3410]: B624062FA: from=<[email protected]>, size=515, nrcpt=1 (queue active)
Mar 18 17:18:47 localhost postfix/pipe[3435]: B624062FA: to=<[email protected]>, relay=dovecot, delay=0.14, delays=0.04/0.01/0/0.09, dsn=2.0.0, $
Mar 18 17:18:47 localhost postfix/qmgr[3410]: B624062FA: removed

2.查看/home/vmail/dovecot-deliver.log Dovecot 日誌檔,應該看見類似記錄:
deliver(<[email protected]>): 2011-01-21 20:03:19 Info: msgid=<<[email protected]>>: saved mail to INBOX
現在你可以測試看看你的 MailServer 的帳號會看到他們的郵件客戶端。

測試 Mailbox

1.進入 /home/vmail/example.com/sales/Maildir 目錄中,測試 [email protected] ,在終端輸入 find 指令:
#find

2.將在終端中看見以下輸出:
.
./dovecot-uidlist
./cur
./new
./new/1285609582.P6115Q0M368794.li172-137
./dovecot.index
./dovecot.index.log
./tmp

3.通過使用郵件客戶端測試 maillbox。
推薦使用 mutt 來測試。
如果沒有安裝 muutt,請用 #yum install mutt 安裝
mutt -f .

可能會提示您新增根信箱。這不是必需的。

4.如果你看見在 inbox 有郵件,證明你 Postfix、Dovecot、MariaDB 設定正確。
可以在 muut 中輸入「q」退出 mutt
 

文章分類