centos7.3搭建內網postfix 郵件系統


需求:內網郵件系統,LAMP+Postfix+Dovecot+Postfixadmin

環境:centos7.3 lnmp

一.部署 Bind DNS 服務

1. 卸載系統自帶的sendmail服務

Shell > yum -y remove sendmail

2. 修改服務器主機名稱

Shell > hostnamectl set-hostname mail

$hostname

$mail


3. 安裝DNS服務

3.1 yum 安裝

Shell > yum -y install bind

3.2 配置DNS服務

Shell > vim /etc/named.conf

name.png

修改圖中紅框的兩項值爲any,表示監聽所有地址,允許所有人訪問

  Shell > vim /etc/named.rfc1912.zones

zone11.png

進入到/var/named 目錄 分別複製named.localhost 爲xx.com.zone,  複製named.loopback 爲 xx.com.local 然後進行編輯

Shell > cp -rf named.localhost hy.com.zone

 

Shell > cp -rf named.loopback 2.168.192.local

 

Shell > vim hy.com.zone

hy.png 

Shell > vim 2.168.192.local

 hy1.png

3.3 啓動並驗證DNS服務

Shell > systemctl start named

Shell > systemctl enable named

使用netstat工具查看端口、nslookup工具進行驗證

 238.png 

 nslookup.png

二.postfix 安裝配置

yum 安裝postfix

Shell > yum -y install postfix

到postfix的配置文件main.cf中修改配置

Shell > vim /etc/postfix/main.cf

myhostname = mail.hy.com

mydomain = hy.com

myorigin = $myhostname

inet_interfaces = all

mydestination = $myhostname, localhost.$mydomain, localhost

Shell > systemctl start postfix

Shell > systemctl enable postfix

三.Dovecot 安裝配置

yum安裝dovecot

Shell > yum -y install dovecot dovecot-devel dovecot-mysql pam-devel

Shell > dovecot --version

創建dovecot工作的用戶及用戶組

Shell > groupadd -g 2000 vmail

Shell > useradd -g vmail -u 2000 -s /sbin/nologin -M vmail

dovecot的配置文件都在/etc/dovecot/目錄下。

Shell > vim /etc/dovecot/dovecot.conf

protocols = imap pop3 lmtp

listen = *

login_trusted_networks =192.168.2.0/24

!include conf.d/*.conf

Shell > vim /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = no

auth_mechanisms = plain login

!include auth-system.conf.ext

Shell > vim /etc/dovecot/conf.d/10-ssl.conf

ssl = no

啓用dovecot的日誌,日誌路徑可以自定義

Shell > vim /etc/dovecot/conf.d/10-logging.confspacer.gif 

 logging.png

啓動dovecot服務

Shell > systemctl start dovecot

四.postfix虛擬用戶配置

1、 修改main.cf 文件

Shell > vim /etc/postfix/main.cf

#啓用SMTP認證

smtpd_sasl_type = dovecot

smtpd_sasl_path = /var/spool/postfix/private/auth

smtpd_sasl_application_name = smtpd

smtpd_sasl_auth_enable = yes

smtpd_sasl_local_domain = $myhostname

broken_sasl_auth_clients = yes

smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination,reject_unknown_sender_domain

smtpd_sasl_security_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

smtpd_client_restrictions = permit_sasl_authenticated

smtpd_sasl_security_options = noanonymous

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

#啓用虛擬用戶

virtual_mailbox_base = /home/vmail/

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

virtual_alias_maps =

   proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf,

   proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf,

   proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

virtual_mailbox_maps =

   proxy:mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf,

   proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf

virtual_uid_maps = static:2000

virtual_gid_maps = static:2000

virtual_transport = dovecot

dovecot_destination_recipient_limit = 1

2. 修改master.cf 文件

Shell > vim /etc/postfix/master.cf

dovecot   unix  -       n       n       -       -       pipe

flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

特別注意:flages前面有兩個空格,不然會報錯,不要入坑-_-.

2.1 數據庫連接相關文件

連接數據庫相關文件有7個,在創建配置文件之前,我們要在/etc/postfix/目錄下建立sql目錄用來存放這些配置如下:

Shell > mkdir /etc/postfix/sql

Shell > vim /etc/postfix/sql/mysql_virtual_alias_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

 

Shell > vim /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'

Shell > vim /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

 

Shell > vim /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'

 

 

Shell > vim /etc/postfix/sql/mysql_virtual_domains_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'

 

Shell > vim /etc/postfix/sql/mysql_virtual_mailbox_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'

 

Shell > vim /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u','@',alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'

 

Shell > vim /etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf

user = postfix

password = postfix

hosts = localhost

dbname = postfix

query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'

 

五.Dovecot 虛擬用戶配置

1. 修改dovecot.conf文件

Shell > vim /etc/dovecot/dovecot.conf

protocols = imap pop3

listen = *

mail_location = maildir:/home/vmail/%d/%n   (曾經掉坑過)

!include conf.d/*.conf

passdb {

  driver = sql

  args = /etc/dovecot/dovecot-sql.conf.ext

}

userdb {

  driver = static

  args = uid=5000 gid=5000 home=/home/vmail/%d/%n

}

#調試日誌

auth_debug_passwords=yes

mail_debug=yes

auth_verbose=yes

auth_verbose_passwords=plain

 

2. 修改10-auth.conf文件

Shell > vim /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = no

auth_mechanisms = plain login cram-md5

!include auth-system.conf.ext

3. 修改10-ssl.conf文件

Shell > vim /etc/dovecot/conf.d/10-ssl.conf

 

ssl = no

4. 修改10-mail.conf文件

Shell > vim /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/home/vmail/%d/%n/Maildir   

mbox_write_locks = fcntl

 

5. 修改10-logging.conf文件

Shell > vim /etc/dovecot/conf.d/10-logging.conf

info_log_path = /var/log/dovecot_info.log

debug_log_path = /var/log/dovecot_debug.log

 

6. 修改10-master.conf文件

Shell > vim /etc/dovecot/conf.d/10-master.conf

service imap-login {

  inet_listener imap {

    port = 143

  }

}

service pop3-login {

  inet_listener pop3 {

    port = 110

  }

}

service auth {

   unix_listener auth-userdb {

mode = 0600

    user = vmail

    group = vmail

  }

  # Postfix smtp-auth

unix_listener /var/spool/postfix/private/auth {

    mode = 0666

    user = postfix

  }

}

7. 添加dovecot-sql.conf.ext文件

dovecot-sql.conf.ext 可能默認不在/etc/dovecot/

用find 命令查找一下此文件在哪,然後把此文件複製到/etc/dovecot/

Shell > find / -name dovecot-sql.conf.ext

sql1.png

Shell > vim /etc/dovecot/dovecot-sql.conf.ext

driver = mysql

connect = host=localhost dbname=postfix user=postfix password=postfix

default_pass_scheme = MD5-CRYPT

password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1'

user_query = SELECT maildir, 2000 AS uid, 2000 AS gid, CONCAT('dict:storage=',floor(quota/1000),' proxy::quota') as quota FROM mailbox WHERE username = '%u' AND active='1'

 

六.postfixadmin配置

1. 下載postfixadmin源碼包

Shell > wget  \

https://nchc.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-3.2/postfixadmin-3.2.tar.gz

2. 把解壓出來的postfixadmin-3.2 重命名下

Shell > mv postfixadmin-3.2 postfixadmin

然後放在nginx 網站根目錄下

3. 創建config.local.php配置文件

Shell > vim config.local.php

 <?php

    $CONF['database_type'] = 'mysqli';

    $CONF['database_user'] = 'postfix';

    $CONF['database_host'] = '127.0.0.1';

    $CONF['database_password'] = 'postfix';

    $CONF['database_name'] = 'postfix';

    $CONF['configured'] = true;

?>

4. 修改config.inc.php配置文件

Shell > vim config.inc.php

修改 $CONF['configured'] = false; 爲 $CONF['configured'] = true;

修改 $CONF['default_language'] = 'en'; 爲 $CONF['default_language'] = 'cn';                                                 修改成默認爲中文

修改 $CONF['database_type'] = 'mysqli'; 爲 $CONF['database_type'] = 'mysql';                                              

修改 $CONF['database_host'] = 'localhost'; 爲 $CONF['database_host'] = '127.0.0.1';                  

修改 $CONF['database_password'] = 'postfixadmin'; 爲 $CONF['database_password'] = 'postfix';  

修改 $CONF['admin_email'] = ''; 爲 $CONF['admin_email'] = '[email protected]';                                         

如下圖所示:

sql2.png

sql3.png 

sql4.png

5. 配置文件改完,然後再瀏覽器中打開:http://ip/postfixadmin/public/setup.php 或者  http://域名/postfixadmin/public/setup.php

有報錯, 根據它的報錯內容,在postfixadmin目錄下新建一個templates_c目錄,並授權  //我的postfixadmin 目錄在/usr/share/nginx/html 下,

Shell > mkdir templates_c

Shell > chmod -R 755 templates_c  # 或者777權限

6. 刷新頁面後,如下圖,有一個Error和Warning

注意:第一個提示說連不上數據庫,這時候我們要檢查config.local.php 和config.inc.php配置文件數據配置、數據賬號、數據庫名,密碼、數據庫地址等是否正確,可能是其中一個配置出錯,纔會有這樣的報錯。

 

第二個提示需要安裝php的imap擴展:

imap.png 

 

Shell > yum -y install php56w-imap-5.6.40-1.w7.x86_64

 

在刷新,錯誤沒了,

我們開始設置密碼

admin.png 

這時候,我們要把紅色方框中的 $CONF['setup_password'] = '264d4931e17c6ea7d7cead0123354f94:67d1764819b702ee631d6c73bfff2c169fd7c717'; 複製到 config.inc.php中。

Shell > vim config.inc.php

inc.png 

 

再刷新頁面,添加管理員郵箱,如圖

admin.png 

最後,刪除 setup.php,用 http://ip/postfixadmin/public/index.php 登陸管理頁面,如圖:

adminlogin.png 

 

參考了網上的鏈接如下:

https://blog.51cto.com/4746316/2314749

https://www.ilanni.com/?p=10963

 


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