本教程来自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信息
本章结束