架設郵件服務器之LEMP環境搭建

本教程來自http://blog.pztop.com/2016/04/20/Email-Server-With-Postfix-Dovecot-MailScanner-1/php

本教程僅僅作翻譯和註釋,版權歸原做者全部。html

----------------------------------正文-----------------------------mysql

郵件服務器至關複雜,並不單單是收發郵件便可,同時也須要作好其餘協同功能和安全防禦,恰好最近幫公司搭建一臺郵件服務器,把整個流程整理了下來。linux

固然,也有其餘簡單、快速的方法能夠搭建郵件服務器,好比iRedMail,但我更喜歡一步步從頭作起,這樣能夠了解如何把不一樣的組件搭配,以及他們是如何協同工做的。就像開車,多知道一些引擎和變速器的知識,能夠幫助瞭解咱們哪裏出了問題並改進。nginx

功能和組件git

首先我把重要的須要的功能列出來github

  • 支持域名和收件箱
  • 支持TARTTLS方式的SMTP
  • 支持SSL/TLS加密的MAP/POP3 
  • 有一個安全的網頁郵箱地址
  • 有郵箱帳戶管理後臺界面
  • 有ailScanner管理後臺界面

下面是所需的組件web

  • LEMP環境(CentOS 7 + Nginx + MariaDB + PHP),也就是LNMP
  • Postfix
  • Dovecot (使用Sieve filter)
  • MailScanner (使用ClamAV and Spamassassin)
  • MailWatch (Web UI for MailScanner)
  • RoundCube webmail
  • Postfix Admin (postfix後臺管理界面,能夠新增域名和郵箱帳戶)
  • Fail2ban and iptables (防火牆)
  • OpenSSL and Let’s Encripts SSL certificate(SSL支持)
  • OpenDKIM and SPF(增長郵件評分)

域名解析sql

搭建郵件服務器以前,先把域名解析設置好vim

mydomain.com.                  600     IN      A       服務器IP
smtp.mydomain.com. 3600 IN A 服務器IP
mydomain.com. 3599 IN MX 10 smtp.mydomain.com.
postfixadmin.mydomain.com. 1697 IN CNAME smtp.mydomain.com.
mailwatch.mydomain.com. 1697 IN CNAME smtp.mydomain.com.
roundcube.mydomain.com. 1697 IN CNAME smtp.mydomain.com.

有條件的VPS能夠作好反向DNS

以centos7.2 64位系統爲例進行安裝,我會設置全部郵件保存到/home/vmail,因此請保證VPS有足夠空間。

取消SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

安裝必要命令

yum -y install net-tools nano wget man bind-utils git mailx telnet

防火牆

centos7默認有Firewalld做爲防火牆,但我更喜歡iptables,因此卸載原來的,安裝iptables。

systemctl stop firewalld
systemctl mask firewalld
systemctl disable firewalld

安裝iptables

yum -y install iptables-services
systemctl enable iptables
systemctl start iptables

  安裝fail2ban

yum install -y epel-release
yum install -y fail2ban jwhois

建立fail2ban配置文件

cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local

編輯此文件以下

[sshd]
enabled = true
...

設置fail2ban啓動和開機啓動

systemctl start fail2ban.service
systemctl enable fail2ban.service

 如今SSH端口22已經被fail2ban保護,若是想打開新端口,只須要設置fail2ban便可。

備註:由於有不少黑客掃描22端口,因此配置iptables和fail2ban更改端口。

設置時區

timedatectl set-timezone America/Vancouver

安裝Chrony

yum install -y chrony
systemctl enable chronyd
systemctl start chronyd

檢查狀態

timedatectl
chronyc tracking
chronyc sources 
chronyc sourcestats

升級並重啓系統

yum -y update && reboot

重啓後檢查狀態

# sestatus
SELinux status:                 disabled

安裝LEMP

安裝MariaDB

yum install -y mariadb-server
systemctl enable mariadb.service
systemctl start mariadb.service

安裝完畢後執行下面命令設置密碼,後面所有Y而後回車

mysql_secure_installation

安裝Nginx

配置安裝源

vim /etc/yum.repos.d/nginx.repo

複製如下內容

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

安裝

yum install nginx

配置文件(worker_processes爲CPU核心,根據本身的VPS設置數量)

vim /etc/nginx/nginx.conf
worker_processes 4;
...
gzip on;
server_tokens off;
...

啓動Nginx並設置爲開機啓動

systemctl enable nginx.service
systemctl start nginx.service

檢查80端口狀態

# netstat -ntlp | grep :80
tcp        0      0 0.0.0.0:80       0.0.0.0:*        LISTEN      9901/nginx: master

打開80和443端口,訪問http://smtp.mydomain.com就能夠看到Nginx默認頁面。

安裝PHP5

PHP和Nginx的運行依賴PHP-FPM,因此安裝如下擴展包。

yum install php-fpm php-cli php-mysql php-gd php-mcrypt php-intl php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-mbstring php-snmp php-soap php-imap

編輯幾個配置文件

vim /etc/php.ini
date.timezone = America/Vancouver
cgi.fix_pathinfo = 0
vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
user = nginx
group = nginx

啓動和設置開機啓動

systemctl enable php-fpm.service
systemctl start php-fpm.service

安裝Let's Encrypt免費SSL證書

證書的安裝請參考另一篇文章,DNS最好在國外,並關閉80和443端口,不然會出錯。

yum install git bc
git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
systemctl stop nginx
cd /opt/letsencrypt
./letsencrypt-auto certonly --standalone

安裝過程當中輸入本身的郵箱,點擊a(gree),複製你的域名mydomain.com, www.mydomain.com,smtp.mydomain.com, mailwatch.mydomain.com, postfixadmin.mydomain.com roundcube.mydomain.com

若是看到如下信息表明安裝成功

Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/mydomain.com/fullchain.pem. Your cert will
expire on 2017-05-13. To obtain a new version of the certificate in
the future, simply run Let’s Encrypt again.

免費證書默認90有效期,到期須要從新更新,命令以下(更新時記得關閉Nginx)

/root/.local/share/letsencrypt/bin/letsencrypt renew --agree-tos

須要建立2048位加密(默認1024位)

openssl dhparam -out /etc/nginx/dhparams.pem 2048

建立配置文件以便postfixadmin.mydomain.com能夠打開

vi /etc/nginx/conf.d/postfixadmin.conf

複製以下內容

server {

  listen 80;
  server_name postfixadmin.mydomain.com;
  return 301 https://$server_name$request_uri; # enforce https

}

server {

   listen          443 ssl;
   server_name     postfixadmin.mydomain.com;
   root            /var/www/html/postfixadmin;
   index           index.php;
   charset         utf-8;
   access_log      /var/log/nginx/pa-access.log;
   error_log       /var/log/nginx/pa-error.log;

   ## SSL settings
   ssl_certificate           /etc/letsencrypt/live/mydomain.com/fullchain.pem;
   ssl_certificate_key           /etc/letsencrypt/live/mydomain.com/privkey.pem;
   ssl_protocols             TLSv1.2 TLSv1.1 TLSv1;
   ssl_prefer_server_ciphers on;
   ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
   ssl_dhparam               /etc/nginx/dhparams.pem;   
   ssl_session_cache         shared:SSL:10m;
   ssl_session_timeout       10m;
   ssl_ecdh_curve            secp521r1;

   add_header Strict-Transport-Security max-age=31536000;

   location / {
      try_files $uri $uri/ index.php;
   }

   location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
        fastcgi_pass  unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
   }

}

建立一個測試網頁

mkdir /var/www/html/postfixadmin
echo "<?php phpinfo(); ?>" > /var/www/html/postfixadmin/info.php

打開網址http://postfixadmin.mydomain.com/info.php便可看到php信息

本章結束

相關文章
相關標籤/搜索