架设邮件服务器之LEMP环境搭建

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

本教程仅仅做翻译和注释,版权归原作者所有。

----------------------------------正文-----------------------------

邮件服务器相当复杂,并不仅仅是收发邮件即可,同时也需要做好其他协同功能和安全防护,刚好最近帮公司搭建一台邮件服务器,把整个流程整理了下来。

当然,也有其他简单、快速的方法可以搭建邮件服务器,比如iRedMail,但我更喜欢一步步从头做起,这样可以了解如何把不同的组件搭配,以及他们是如何协同工作的。就像开车,多知道一些引擎和变速器的知识,可以帮助了解我们哪里出了问题并改进。

功能和组件

首先我把重要的需要的功能列出来

  • 支持域名和收件箱
  • 支持TARTTLS方式的SMTP
  • 支持SSL/TLS加密的MAP/POP3 
  • 有一个安全的网页邮箱地址
  • 有邮箱账户管理后台界面
  • 有ailScanner管理后台界面

下面是所需的组件

  • 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(增加邮件评分)

域名解析

搭建邮件服务器之前,先把域名解析设置好

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信息

本章结束

 

posted @ 2017-05-13 11:51  滚滚长江都是水  阅读(318)  评论(0编辑  收藏  举报