CentOS7 基于Postfix Dovecot Extmail 搭建郵件服務(wù)器

CentOS7 最小化安裝,基于 LAMP,安裝環(huán)境:CENTOS Mysql Apache Postfix Dovecot.

web 模塊是最先需要確定的,因?yàn)樗鼪Q定了郵箱系統(tǒng)最核心的數(shù)據(jù)結(jié)構(gòu),這里選用國(guó)產(chǎn)的 extmail,其他所有模塊都按照 extmail 的結(jié)構(gòu)進(jìn)行配置修改。
如果在 main.cf 里設(shè)置 virtual_transport = virtual,就表示對(duì)于包含在 virtual_mailbox_domains域名里的郵件,都使用virtual進(jìn)行投遞。

用戶信息存儲(chǔ)方式也有很多可選的,比如 mysql, ldap 等。這里選 mysql,原因是各個(gè)模塊,包括extmail 等對(duì)這個(gè)支持最好最方便最簡(jiǎn)單。在投遞郵件的時(shí)候,需要確定一些數(shù)據(jù),比如收件用戶的Maildir 的路徑,保存郵件用的 uid,gid,郵箱容量等,這些數(shù)據(jù)需要 courier-authlib 來提供。

整個(gè)系統(tǒng)模塊間的作用和關(guān)系是:
postfix 作為 MTA(Mail Transfer Agent), 負(fù)責(zé)創(chuàng)建 smtp 服務(wù)(smtpd)接收本域用戶或其他域名服務(wù)器投遞來的郵件,負(fù)責(zé)向其他服務(wù)器投遞(發(fā)送)郵件,管理郵件隊(duì)列;
courier-authlib 為 maildrop 提供與用戶相關(guān)的信息查詢;
dovecot 提供系統(tǒng)的 POP3 和 IMAP 服務(wù),同時(shí)給 postfix 提供 SMTP 的 SASL 認(rèn)證服務(wù)。

一、 裝備工作

1、防止不必要的麻煩關(guān)閉 Selinux
setenforce 0
2、關(guān)閉防火墻
systemctl stop firewalld
3、添加 MySQL 和 epel 源

yum install -y wget
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm 
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm

二、安裝postfix

1、安裝編譯及其他所需要的包

yum install nginx vim gcc gcc-c++ openssl openssl-devel db4-devel ntpdate mysql mysql-devel mysql-server bzip2 php-mysql cyrus-sasl-md5 perl-GD perl-DBD-MySQL perl-GD perl-CPAN perl-CGI perl-CGI-Session cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel libtool-ltdl-devel telnet mail libicu-devel  -y

2、編譯安裝postfix
i. 卸載系統(tǒng)自帶的postfix,刪除postfix用戶,重新指定uid、gid創(chuàng)建新用戶postfix,postdrop

yum remove postfix -y
userdel postfix
groupdel postdrop
groupadd -g 2525 postfix
useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
groupadd -g 2526 postdrop
useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop

ii. 下載源碼包并解壓編譯

wget http://mirrors.go-parts.com/postfix/source/official/postfix-3.0.1.tar.gz
tar xf postfix-3.0.1.tar.gz
cd postfix-3.0.1
make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 
'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lrt -lm -L/usr/lib64/sasl2 -lsasl2   -lssl -lcrypto'
make && make install

make install 的時(shí)候會(huì)有個(gè)交互式的界面,自定義一些目錄,我們這里只更改第二項(xiàng)臨時(shí)文件目錄,其他的全部默認(rèn)。

Please specify the prefix for installed file names. Specify this ONLY
if you are building ready-to-install packages for distribution to OTHER
machines. See PACKAGE_README for instructions.
install_root: [/] 
Please specify a directory for scratch files while installing Postfix. You
must have write permission in this directory.
tempdir: [/root/postfix-3.0.1] /tmp/extmail
…………………………
…………………………
shlib_directory: [no] 
Please specify the final destination directory for non-executable files
that are shared among multiple Postfix instances, such as postfix-files,
dynamicmaps.cf, as well as the multi-instance template files main.cf.proto
and master.cf.proto.
meta_directory: [/etc/postfix]

3、更改目錄屬主屬組

chown -R postfix:postdrop /var/spool/postfix
chown -R postfix:postdrop /var/lib/postfix/
chown root /var/spool/postfix
chown -R root /var/spool/postfix/pid

4,修改postfix的配置文件

[root@localhost ~]# vim /etc/postfix/main.cf
myhostname = {hostname}        //設(shè)置主機(jī)名
mydomain = {domain}        //指定域名
myorigin = $mydomain        //指明發(fā)件人所在的域名
inet_interfaces =         //all指定postfix系統(tǒng)監(jiān)聽的網(wǎng)絡(luò)接口
mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain        //指定postfix接收郵件時(shí)收件人的域名 [使用虛擬域需要禁用]
mynetworks_style = host        //指定信任網(wǎng)段類型
mynetworks = 192.168.1.0/24, 127.0.0.0/8        //指定信任的客戶端
relay_domains = $mydestination        //指定允許中轉(zhuǎn)郵件的域名
alias_maps = hash:/etc/aliases        //設(shè)置郵件的別名

三、安裝dovecot

1、yum安裝

yum install -y dovecot dovecot-mysql

2、配置dovecot

[root@localhost ~]# cd /etc/dovecot/
[root@localhost dovecot]# vim dovecot.conf        //直接在配置文件最后添加即可
protocols = imap pop3
!include conf.d/*.conf
listen = *
base_dir = /var/run/dovecot/
[root@localhost dovecot]# cd conf.d/
[root@localhost conf.d]# vim 10-auth.conf
disable_plaintext_auth = no
[root@localhost conf.d]# vim 10-mail.conf
mail_location = maildir:~/Maildir
mail_location = maildir:/var/mailbox/%d/%n/Maildir
mail_privileged_group = mail
[root@localhost conf.d]# vim 10-ssl.conf
ssl = no
[root@localhost conf.d]# vim 10-logging.conf 
log_path = /var/log/dovecot.log
info_log_path = /var/log/dovecot.info
log_timestamp = "%Y-%m-%d %H:%M:%S "
[root@localhost conf.d]# cp auth-sql.conf.ext auth-sql.conf
[root@localhost conf.d]# vim auth-sql.conf
passdb {  
driver = sql  # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext  
args = /etc/dovecot/dovecot-sql.conf
}
userdb {  
driver = sql  
args = /etc/dovecot/dovecot-sql.conf
}

3、編輯dovecot通過mysql認(rèn)證的配置文件

[root@localhost conf.d]# vim /etc/dovecot-mysql.conf
driver = mysql
connect = host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = '%u'

四、安裝courier-authlib

1、下載解壓并并編譯

[root@localhost ~]# wget https://sourceforge.net/projects/courier/files/authlib/0.66.2/courier-authlib-0.66.2.tar.bz2
[root@localhost ~]# tar xf courier-authlib-0.66.2.tar.bz2
[root@localhost ~]# cd courier-authlib-0.66.2
[root@localhost ~]# ./configure \
--prefix=/usr/local/courier-authlib \
    --sysconfdir=/etc \
    --without-authpam \
    --without-authshadow \
    --without-authvchkpw \
    --without-authpgsql \
    --with-authmysql \
    --with-mysql-libs=/usr/lib64/mysql \
    --with-mysql-includes=/usr/include/mysql \
    --with-redhat \
    --with-authmysqlrc=/etc/authmysqlrc \
    --with-authdaemonrc=/etc/authdaemonrc \
    --with-mailuser=postfix

這里會(huì)報(bào)錯(cuò),configure: error: The Courier Unicode Library 1.2 appears not to be installed.提示 Courier Unicode Library 沒有安裝,下載 courier-unicode-1.2 并編譯安裝。

[root@localhost ~]# wget https://sourceforge.net/projects/courier/files/courier-unicode/1.2/courier-unicode-1.2.tar.bz2
[root@localhost ~]# tar xf courier-unicode-1.2.tar.bz2 
[root@localhost ~]# cd courier-unicode-1.2
[root@localhost courier-unicode-1.2]# ./configure
[root@localhost courier-unicode-1.2]# make && make install

再次編譯 courier-authlib,最后 make && makeinstall

2、配置 courier-authlib

[root@localhost  courier-authlib-0.66.2]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
[root@localhost  courier-authlib-0.66.2]# cp /etc/authdaemonrc.dist  /etc/authdaemonrc
[root@localhost  courier-authlib-0.66.2]# cp /etc/authmysqlrc.dist  /etc/authmysqlrc

驗(yàn)證方法使用authmysql

[root@localhost  courier-authlib-0.66.2]# vim /etc/authdaemonrc           
authmodulelist="authmysql"
authmodulelistorig="authmysql"
[root@localhost  courier-authlib-0.66.2]# vim /etc/authmysqlrc            //直接添加到配置文件尾部,然后去上面將響應(yīng)系統(tǒng)默認(rèn)的注視掉,或者刪除即可
MYSQL_SERVER            localhost
MYSQL_USERNAME          extmail
MYSQL_PASSWORD          extmail
MYSQL_SOCKET            /var/lib/mysql/mysql.sock
MYSQL_PORT               3306
MYSQL_DATABASE          extmail
MYSQL_USER_TABLE        mailbox
MYSQL_CRYPT_PWFIELD     password
DEFAULT_DOMAIN          test.com
MYSQL_UID_FIELD         '2525'
MYSQL_GID_FIELD         '2525'
MYSQL_LOGIN_FIELD       username
MYSQL_HOME_FIELD        concat('/var/mailbox/',homedir)
MYSQL_NAME_FIELD        name
MYSQL_MAILDIR_FIELD     concat('/var/mailbox/',maildir)

3、 courier-authlib 添加服務(wù)啟動(dòng)腳本及其他

[root@localhost  courier-authlib-0.66.2]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib
[root@localhost  courier-authlib-0.66.2]# chmod +x /etc/init.d/courier-authlib
[root@localhost  courier-authlib-0.66.2]# chkconfig --add courier-authlib
[root@localhost  courier-authlib-0.66.2]# chkconfig courier-authlib on
[root@localhost  courier-authlib-0.66.2]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf
[root@localhost  courier-authlib-0.66.1]# ldconfig
[root@localhost  courier-authlib-0.66.1]# service courier-authlib start
Starting Courier authentication services: authdaemond

4、smtp以及虛擬用戶相關(guān)的設(shè)置

[root@localhost ~]# vim /usr/lib64/sasl2/smtpd.conf        //文件不存在,要自己創(chuàng)建
pwcheck_method: authdaemond
log_level: 3
mech_list: PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
[root@localhost ~]# vim /etc/postfix/main.cf
##postfix支持SMTP##
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = ''
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
broken_sasl_auth_clients=yes
smtpd_client_restrictions = permit_sasl_authenticated
smtpd_sasl_security_options = noanonymous
##postfix支持虛擬用戶##
virtual_mailbox_base = /var/mailbox
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf   //這里的配置文件需在后面extman
里復(fù)制過來
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

五、安裝extmail

下載包

wget http://7xivyw.com1.z0.glb.clouddn.com/extmail-1.2.tar.gz
wget http://7xivyw.com1.z0.glb.clouddn.com/extman-1.2.tar.gz

1、創(chuàng)建目錄并解壓

[root@localhost ~]# mkdir -p /var/www/extsuite
[root@localhost ~]# tar xf extmail-1.2.tar.gz -C /var/www/extsuite/
[root@localhost ~]# mv /var/www/extsuite/extmail-1.2/ /var/www/extsuite/extmail

2、更改extmail的配置文件

[root@localhost ~]# cd /var/www/extsuite/extmail
[root@localhost extmail]# cp webmail.cf.default webmail.cf
[root@localhost extmail]# vim webmail.cf
SYS_SESS_DIR = /tmp/extmail
SYS_UPLOAD_TMPDIR = /tmp/extmail/upload
SYS_USER_LANG = zh_CN
SYS_MIN_PASS_LEN = 8
SYS_MAILDIR_BASE = /var/mailbox
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_DB = extmail
SYS_MYSQL_HOST = localhost
SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock
SYS_MYSQL_TABLE = mailbox
SYS_MYSQL_ATTR_USERNAME = username
SYS_MYSQL_ATTR_DOMAIN = domain
SYS_MYSQL_ATTR_PASSWD = password
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket

3、建立臨時(shí)文件目錄與session目錄

[root@localhost extmail]# mkdir -p /tmp/extmail/upload
[root@localhost extmail]# chown -R postfix.postfix /tmp/extmail/

六、安裝extman

1、解壓extman

[root@localhost ~]# tar xf extman-1.1.tar.gz -C /var/www/extsuite/
[root@localhost ~]# cd /var/www/extsuite/
[root@localhost extsuite]# mv extman-1.1/ extman

2、更改extman配置文件

[root@localhost extsuite]# cd extman/
[root@localhost extman]# cp webman.cf.default webman.cf

3、更改cgi目錄屬主屬組

[root@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/
[root@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/

4、導(dǎo)入數(shù)據(jù)庫(kù)

由于數(shù)據(jù)庫(kù)不能識(shí)別TYPE= MyISAM ,所以這里直接導(dǎo)入會(huì)出錯(cuò),先編輯extmail.sql數(shù)據(jù)庫(kù)文件,將TYPE=MyISAM更改為ENGINE=MyISAM

[root@localhost extman]# vim docs/extmail.sql 
:% s/TYPE/ENGINE/g

導(dǎo)入還會(huì)報(bào)錯(cuò):

[root@localhost extman]# mysql -uroot < docs/extmail.sql 
ERROR 1364 (HY000) at line 31: Field 'ssl_cipher' doesn't have a default value

修改my.cnf配置文件

[root@localhost extman]# vim /etc/my.cnf
# Recommended in standard MySQL setup
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES   //將這行注視掉,重啟mysql,這里需要注意的是,等數(shù)據(jù)庫(kù)導(dǎo)入成功后,這項(xiàng)是不可以去掉注釋的,不然mysql就啟動(dòng)不起來了。

再次導(dǎo)入數(shù)據(jù)庫(kù)

mysql -uroot < docs/extmail.sql
mysql -uroot < docs/init.sql

5、創(chuàng)建數(shù)據(jù)庫(kù)用戶extmail并授予權(quán)限

mysql> GRANT ALL ON extmail.* to extmail@'localhost' identified by 'extmail'; //只允許本地登錄

刷新權(quán)限
FLUSH PRIVILEGES;

6、復(fù)制四-4提到的配置文件

[root@localhost ~]# cd /var/www/extsuite/extman/docs/
[root@localhost docs]# cp mysql_virtual_* /etc/postfix/

為extman創(chuàng)建臨時(shí)目錄

[root@localhost extman]# mkdir /tmp/extman
[root@localhost extman]# chown -R postfix.postfix /tmp/extman/

7、啟動(dòng)postfix、dovecot、saslauthd

七、測(cè)試

1、測(cè)試虛擬用戶

[root@localhost courier-authlib-0.66.2]# /usr/local/courier-authlib/sbin/authtest -s login postmaster@extmail.org extmail
Authentication succeeded.               //顯示這個(gè)表示成功,測(cè)試時(shí)使用的是postmaster@extmail.org,因?yàn)槲覀儗?dǎo)入的數(shù)據(jù)庫(kù)init.sql里面自帶了這個(gè)。
Authenticated: postmaster@extmail.org  (uid 2525, gid 2525)
Home Directory: /var/mailbox/extmail.org/postmaster  //這里需要注意/var/mailbox這個(gè)目錄現(xiàn)在我們還沒有創(chuàng)建,后面web訪問的時(shí)候如果沒有會(huì)報(bào)錯(cuò),所以提前創(chuàng)建。
                    Maildir: /var/mailbox/extmail.org/postmaster/Maildir/
                    Quota: (none)
            Encrypted Password: $1$phz1mRrj$3ok6BjeaoJYWDBsEPZb5C0
                Cleartext Password: extmail
                    Options: (none)

創(chuàng)建 目錄

[root@localhost courier-authlib-0.66.2]# mkdir /var/mailbox
[root@localhost courier-authlib-0.66.2]# chown -R postfix.postfix /var/mailbox/

不創(chuàng)建的話會(huì)報(bào)錯(cuò)


2、測(cè)試smtp發(fā)信

[root@localhost ~]# printf   "postmaster@extmail.org" | openssl base64
cG9zdG1hc3RlckBleHRtYWlsLm9yZw==
[root@localhost ~]#  printf   "extmail" | openssl base64
ZXh0bWFpbA==
[root@localhost ~]# telnet localhost 25
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.daen.com ESMTP Postfix
auth login
334 VXNlcm5hbWU6
cG9zdG1hc3RlckBleHRtYWlsLm9yZw==
334 UGFzc3dvcmQ6
ZXh0bWFpbA==
235 2.7.0 Authentication successful     //成功
quit
221 2.0.0 Bye
Connection closed by foreign host.

3.啟動(dòng)cmdserver

[root@localhost ~]# /var/www/extsuite/extman/daemon/cmdserver -v -d 
loaded ok

不啟動(dòng)會(huì)出現(xiàn) Connection refused

八、配置Apache

1、編輯httpd.conf文件

vim httpd.conf       //直接在末尾添加
NameVirtualHost *.80
Include conf/vhost_extmail.conf

2、編輯 vhost_extmail.conf

# VirtualHost for ExtMail Solution
  <VirtualHost *:80>
  ServerName mail.extmail.org
  DocumentRoot /var/www/extsuite/extmail/html/

  ScriptAlias /extmail/cgi/ /var/www/extsuite/extmail/cgi/
  Alias /extmail /var/www/extsuite/extmail/html/

  ScriptAlias /extman/cgi/ /var/www/extsuite/extman/cgi/
  Alias /extman /var/www/extsuite/extman/html/

  # Suexec config
  SuexecUserGroup vuser vgroup
  </VirtualHost>

3.啟動(dòng) Apache

systemctl start httpd

訪問 http://ip

extman的登錄賬戶為root@extmail.org密碼為extmail123,首次使用需要先添加域,添加之后再修改域,改為可自由注冊(cè),再注冊(cè)用戶就可以登錄發(fā)郵件了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容