昨天测试开发微信小程序,才发现微信也要求用https加密数据,想来是由于之前苹果的ats审核政策的缘故吧,微信想在苹果上开放小程序必然也只能要求开发者必须使用https了,于是在服务器上测试安装nginx+https服务。
安装 https 最麻烦的问题应该就是获取证书了,证书感觉种类也挺复杂的,有好几种,单域、泛域、多域。。。还有个种标准乱七八糟的感觉,而且收费很高,还是每年买的。
现在各个云服务商也都有提供各种基础功能的免费证书,但似乎很多只对单域免费,这里的单域是指每个二级域名都算是一个域名,每个二级域名都需要单独配置证书。
我使用的是免费的 let's encrypt 证书,支持苹果ats标准。
<a href="https://letsencrypt.org/">https://letsencrypt.org/</a> let's encrypt 项目旨在让每个网站都能使用 https 加密,该项目获得了 mozilla、思科、akamai、identrust 和 eff 等组织的支持, 由 linux 基金会托管
我用的是阿里云的服务器,操作系统版本:centos7.0
提醒:在一切开始前,需要先注意看看服务器剩余内存,我一开始用的是最低配的测试服务器,剩余可用内存只有70mb左右,结果服务器直接挂了,所以最好先腾出个300mb内存再来。 另外,就是服务器必须绑定域名且是外网可以直接访问的才行,若是内网或虚拟机上是不行的,因为证书安装时需要通过域名访问数据的,否则不能生成证书。
ssl labs 最终配置测试结果,
好了,下面上配置流程:
我第一次安装时使用的 openssl 还是系统默认安装版本 1.0.1e,安装完后在 ssl labs 测试安全性居然是个 f 等级,于是升级到1.1.0c 之后变成了a,不过后来看了很多文章后,发现大家推荐使用的是 1.0.2j 版本,据说是因为 1.1 官方支持是到 2018年,而且很多软件还不能兼容,1.0.2 是支持到2019年的。至于实际用哪个看你的其他需求了。。。
证书安装时会在网站根目录下自动生成 .well-known 目录,必须让外网能够访问该目录下的文件才行,否则会认证失败。 对于 nginx 的版本,我测试时使用过的最低版本是 nginx1.8.1,建议使用当前最新的稳定版 nginx1.10.2
如果之前 nginx.conf 中存在类似下面的规则
需要增加下面配置:注意,因为后面证书还需要自动续期,所以这个配置后面还是需要用到的,不能删除。
1, 需要先安装 epel
<code>yum install epel-release</code>
2, 先修改为使用国内的 pip 源
<code>`</code>mkdir ~/.pip
cat > ~/.pip/pip.conf <[global]
[install]
trusted-host=pypi.doubanio.com
eof
mkdir /usr/local/certbot
cd /usr/local/certbot
chmod +x ./certbot-auto
./certbot-auto -n
yum provides '*/applydeltarpm'
yum install deltarpm
./certbot-auto certonly --email [email protected] --agree-tos --webroot -w /home/wwwroot/yourwebsite -d www.yourdomain.com
./certbot-auto certonly --email [email protected] --agree-tos --webroot -w /home/wwwroot/yourwebsite -d www.yourdomain.com -d m.yourdomain.com
./certbot-auto certonly --email [email protected] --agree-tos --webroot -w /home/wwwroot/yourwebsite -d www.yourdomain.com -d m.yourdomain.com -w /home/wwwroot/myanotherwebsite -d www.aotherdomain.com -d www.aotherdomain.com
ll /etc/letsencrypt/live/
vi nginx.conf
server {
}
nginx -t
nginx -s reload
vi /etc/sysconfig/iptables
-a input -p tcp -m state --state new -m tcp --dport 443 -j accept
crontab -e
30 05 01 /usr/local/certbot/certbot-auto renew --renew-hook "/etc/init.d/nginx reload"
add_header strict-transport-security "max-age=63072000; includesubdomains; preload";
mkdir -p /usr/local/nginx/conf/vhost/ssl
openssl dhparam -out /usr/local/nginx/conf/vhost/ssl/dhparams.pem 2048
ssl_dhparam /usr/local/nginx/conf/vhost/ssl/dhparams.pem;