Ubuntu之郵件服務器(Postfix, Dovecot, MySql)

前提

  • 系統爲 Ubuntu , 版本爲14.04, 其餘版本問題應該不大
  • 系統 hostnamezjc - 詳見設置linux主機名 ----- 此步非必須html

    • $ sudo sunode

    • # echo zjc > /etc/hostnamemysql

    • # exitlinux

  • 系統IP地址爲10.1.6.49sql

  • 一些地址映射 要加入/etc/hosts文件或者DNS數據庫

    • $ sudo apt-get install vimubuntu

    • $ sudo vim /etc/hosts #天然,也能夠用其餘文本編輯器vim

    •  #新添加的項
      10.1.6.49      zjc zjc.com vzjc.com  mail.vzjc.com
  • 修改完畢後最好重啓一下機器centos

    • $ sudo shutdown -r now

安裝依賴包

 
 
 
 
$ sudo apt-get install openssl telnet libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql

安裝mysql

 
 
 
 
$ sudo apt-get install mysql-client mysql-server

首次安裝後會提示輸入mysql密碼,輸入確認並記住,之後還須要用到bash

設置MySQL數據庫

其中的mail_adminpassword-for-mail_admin爲生成的數據庫mail的用戶名和密碼,能夠修改爲本身的,可是一旦修改,後文中出現的地方都要修改。

 
 
 
 
$ mysql -u root -p


Enter password: input your password
mysql> CREATE DATABASE mail;
mysql> USE mail;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mail. TO 'mail_admin'@'localhost' IDENTIFIED BY 'password-for-mail_admin'; mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mail. TO 'mail_admin'@'127.0.0.1' IDENTIFIED BY 'password-for-mail_admin';
mysql> FLUSH PRIVILEGES;
mysql> CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );
mysql> CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );
mysql> CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
mysql> CREATE TABLE transport ( domain varchar(128) NOT NULL default '', transport varchar(128) NOT NULL default '', UNIQUE KEY domain (domain) );
mysql> quit

安裝Postfit及相關

$ sudo apt-get install postfix postfix-mysql postfix-doc mailutils

若是postfit彈出設置界面,那麼

General type of mail configuration 選擇 Internet Site
System Mail Name 選擇 vzjc.com

配置Postfit

虛擬domain配置文件

$ sudo vim /etc/postfix/mysql-virtual_domains.cf

 
 
 
 
user = mail_adminpassword = password-for-mail_admindbname = mailquery = SELECT domain AS virtual FROM domains WHERE domain='%s'hosts = 127.0.0.1

虛擬forwarding配置文件

$ sudo vim /etc/postfix/mysql-virtual_forwardings.cf

 
 
 
 
user = mail_adminpassword = password-for-mail_admindbname = mailquery = SELECT destination FROM forwardings WHERE source='%s'hosts = 127.0.0.1

虛擬mailbox配置文件

$ sudo vim /etc/postfix/mysql-virtual_mailboxes.cf

 
 
 
 
user = mail_adminpassword = password-for-mail_admindbname = mailquery = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'hosts = 127.0.0.1

虛擬email2email配置文件

$ sudo vim /etc/postfix/mysql-virtual_email2email.cf

 
 
 
 
user = mail_adminpassword = password-for-mail_admindbname = mailquery = SELECT email FROM users WHERE email='%s'hosts = 127.0.0.1

設置權限

 
 
 
 
$ sudo chmod o= /etc/postfix/mysql-virtual_*.cf$ sudo chgrp postfix /etc/postfix/mysql-virtual_*.cf

創建用戶

 
 
 
 
$ sudo groupadd -g 5000 vmail$ sudo useradd -c 'VMail' -g vmail -u 5000 vmail -d /home/vmail -m

使用postconf配置

此處有個須要注意的地方mynetworks應該包含本機的全部IP地址,例如我這裏只有一個10.1.6.197,否則會致使發給本身的域的郵件接收不到。若是仍是接收不到,那麼重啓機器看看

 
 
 
 
postconf -e 'myhostname = vzjc.com'postconf -e 'mydestination = vzjc.com, localhost, localhost.localdomain'postconf -e 'mynetworks = 127.0.0.0/8, 10.1.6.197'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_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/postfix/smtpd.cert'postconf -e 'smtpd_tls_key_file = /etc/postfix/smtpd.key'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 'local_transport = dovecot'

爲postfix建立證書

 
 
 
 
$ cd /etc/postfix$ sudo openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 3650 -x509

更改證書權限

$ sudo chmod o= /etc/postfix/smtpd.key

配置saslauthd

 
 
 
 
$ sudo mkdir -p /var/spool/postfix/var/run/saslauthd$ sudo cp -a /etc/default/saslauthd /etc/default/saslauthd.bak

$ sudo vim /etc/default/saslauthd

 
 
 
 
START=yesDESC="SASL Authentication Daemon"NAME="saslauthd"MECHANISMS="pam"MECH_OPTIONS=""THREADS=5OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd -r"

$ sudo vim /etc/pam.d/smtp

 
 
 
 
auth required pam_mysql.so user=mail_admin passwd=password-for-mail_admin host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1account sufficient pam_mysql.so user=mail_admin passwd=password-for-mail_admin host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1

$ sudo vim /etc/postfix/sasl/smtpd.conf

 
 
 
 
pwcheck_method: saslauthdmech_list: plain loginallow_plaintext: trueauxprop_plugin: sqlsql_engine: mysqlsql_hostnames: 127.0.0.1sql_user: mail_adminsql_passwd: password-for-mail_adminsql_database: mailsql_select: select password from users where email = '%u@%r'

設置權限

 
 
 
 
$ sudo chmod o= /etc/pam.d/smtp$ sudo chmod o= /etc/postfix/sasl/smtpd.conf

重啓服務

 
 
 
 
$ sudo adduser postfix sasl$ sudo service postfix restart$ sudo service saslauthd restart

安裝dovecot及相關

安裝dovecot

$ sudo apt-get install dovecot-common dovecot-imapd dovecot-pop3d dovecot-mysql

彈出的選項中選擇生成根證書,而且生成的證書dovecot.pem應該在/etc/dovecot/下面,key在/etc/dovecot/private下面。請確認這一點
hostname 填寫 vzjc.com

配置dovecot

$ sudo vim /etc/postfix/master.cf

取消submission配置的註釋:

 
 
 
 
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=$mua_client_restrictions -o milter_macro_daemon_name=ORIGINATING

文件末尾增長

dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -d ${recipient}

$ sudo cp -a /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak

清空文件/etc/dovecot/dovecot.conf,加入以下

$ sudo vim /etc/dovecot/dovecot.conf

: 1,$d

注意ssl_cert, ssl_key指向的證書和私鑰必須存在
 
 
 
 
log_timestamp = "%Y-%m-%d %H:%M:%S "mail_location = maildir:/home/vmail/%d/%n/Maildirnamespace { inbox = yes location = prefix = INBOX. separator = . type = private}passdb { args = /etc/dovecot/dovecot-sql.conf driver = sql}protocols = imap pop3disable_plaintext_auth = noauth_mechanisms = plain loginservice auth { unix_listener /var/spool/postfix/private/auth { group = postfix mode = 0660 user = postfix } unix_listener auth-master { mode = 0600 user = vmail } user = root}ssl = requiredssl_cert = </etc/dovecot/dovecot.pemssl_key = </etc/dovecot/private/dovecot.pemuserdb { args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes driver = static}protocol lda { auth_socket_path = /var/run/dovecot/auth-master log_path = /home/vmail/dovecot-deliver.log postmaster_address = postmaster@zjc.com}protocol pop3 { pop3_uidl_format = %08Xu%08Xv}

dovecot-sql.conf

$ sudo cp -a /etc/dovecot/dovecot-sql.conf /etc/dovecot/dovecot-sql.conf.bak

$ sudo vim /etc/dovecot/dovecot-sql.conf

 
 
 
 
driver = mysqlconnect = host=127.0.0.1 dbname=mail user=mail_admin password=password-for-mail_admindefault_pass_scheme = CRYPTpassword_query = SELECT email as user, password FROM users WHERE email='%u';

$ sudo service dovecot restart

$ sudo chgrp vmail /etc/dovecot/dovecot.conf

$ sudo chmod g+r /etc/dovecot/dovecot.conf

設置mail aliases - 詳見aliases配置文件 -- 這個是給sendmail程序用的吧,反正我是沒看出來爲啥要配置它

$ sudo vim /etc/aliases

postmaster: root
root:admin@vzjc.com

測試

添加郵件帳戶

$ mysql -u root -p
Enter password: input your password
mysql> USE mail;
mysql> INSERT INTO domains (domain) VALUES ('vzjc.com');
mysql> INSERT INTO users (email, password) VALUES ('test1@vzjc.com', ENCRYPT('password-for-test1'));
mysql> INSERT INTO users (email, password) VALUES ('test2@vzjc.com', ENCRYPT('password-for-test2'));
mysql> INSERT INTO users (email, password) VALUES ('admin@vzjc.com', ENCRYPT('111111'));
mysql> quit

原本覺得用戶名能夠用 test1 形式, 而不用 test1@vzjc.com 形式呢,結果若是不加@時候收發郵件是時靈時不靈的。。。不測試了,用戶名直接加域名得了

### 重啓機器

> 作完上面操做後若是用telnet vzjc.com 587,則顯示不能鏈接,機器重啓後就能夠了 -- 緣由待查

$ sudo shutdown -r now

### 用telnet測試

$ telnet vzjc.com 587

Trying 10.1.6.197...
Connected to vzjc.com.
Escape character is '^]'.
220 vzjc.com ESMTP Postfix (Ubuntu)

ehlo vzjc.com
250-vzjc.com
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

$ telnet vzjc.com smtp

$ telnet vzjc.com 110

Trying 10.1.6.197...
Connected to vzjc.com.
Escape character is '^]'.
+OK Dovecot (Ubuntu) ready.
quit
+OK Logging out
Connection closed by foreign host.

用客戶端測試

能夠用ubuntu的thunderbird或者其餘系統的outlook,foxmail等客戶端測試,若是是其餘機器上,注意一系列端口號必定要被服務器開放
這裏寫圖片描述

FAQ:

解決Postfit不能羣發的問題 解決方案

參考:

https://www.centos.bz/2013/06/ubuntu-12-04-deploy-postfix-dovecot-mysql-cyrus-sasl/
http://nonfu.me/p/2753.html
http://blog.163.com/kssh0487596@126/blog/static/32111129201001911212786/



相關文章
相關標籤/搜索