给你的网站穿上外衣- HTTPS 免费部署指南

2016-09-27 21:05:41 +08:00
 rapospectre

前言

随着国内各大网站纷纷开启全站 HTTPS 时代, HTTPS 已不再是支付等敏感操作过程的专属,开启 HTTPS 对于个人网站或者小型网站也不再遥不可及。 今天博主就以自己的网站 www.rapospectre.com 为例叙述一下为自己网站点亮 HTTPS 小绿锁的过程。

HTTP 和 HTTPS

HTTPS ( Hypertext Transfer Protocol over Secure Socket Layer ),是以安全为目标的 HTTP 通道,简单讲是 HTTP 的安全版。即 HTTP 下加入 SSL 层, HTTPS 的安全基础是 SSL ,因此加密的详细内容就需要 SSL 。 它是一个 URI scheme ( 抽象标识符体系 ),句法类同 http :体系。用于安全的 HTTP 数据传输。 https:URL 表明它使用了 HTTP ,但 HTTPS 存在不同于 HTTP 的默认端口及一个加密 /身份验证层(在 HTTP 与 TCP 之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。

HTTP 超文本传输协议 ( HTTP-Hypertext transfer protocol ) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。

从概念里可以看到,要开启 HTTPS 至关重要的一点就是 ssl 层的身份验证,而身份验证需要用到 ssl 证书,以前少有免费 ssl 证书,所以小站基本不会选择 https ,而现在网上提供个人免费 ssl 证书的机构越来越多,这使得免费升级站点为 https 成为可能。

1. 申请 SSL 证书

网上已经有不少机构提供个人免费 ssl 证书,有效期几个月到几年不等,博主使用的是 StartSSL, 申请成功后有效期 3 年,到期后可免费续租。 具体申请过程不复杂,注册后根据提示验证网站 + 生成证书即可,如果不清楚可以 Google 一下

要注意 StartSSL 验证网站拥有者时是给域名所有者的邮箱发验证邮件,如果域名开启了隐私保护请暂时关闭。

然后在自己服务器中生成 SSL 证书的 csr ,记住生成输入的秘密,之后要用到:

openssl req -new -sha256 -key rapospectre.com_secure.key -out rapospectre.com.csr

假设以上文件生成在 /var/tmp 文件夹下

在 StartSSL 填写 csr 文件内容,生成 SSL 证书并下载, 生成成果后如图:

点击 Retrieve 下载证书,解压缩后包含各种服务器的 crt ,博主使用 nginx 做反代,所以选择 nginxserver 解压缩后得到 www.rapospectre.com_bundle.crt 将此文件上传到服务器,假设传到 /var/tmp/ 文件夹

2. 配置服务器

以 nginx 为例,打开 /etc/nginx/nginx.conf,加入配置:

 server {
        listen       443 ssl;
		ssl_certificate /var/tmp/www.rapospectre.com_bundle.crt;
		ssl_certificate_key /var/tmp/rapospectre.com_secure.key;
		ssl_prefer_server_ciphers on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #选择特定的加密方式, 避免已知的漏洞
        ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';
		#让浏览器记住直接访问 https 的网址, 不再去 http 重定向。
		add_header Strict-Transport-Security 'max-age=31536000; preload';
        add_header X-Frame-Options DENY;
		ssl_session_cache   shared:SSL:10m;
		ssl_session_timeout 10m;
		keepalive_timeout 70;
		ssl_dhparam /var/tmp/dhparam2048.pem;
        #禁止服务器自动解析资源类型
        add_header X-Content-Type-Options nosniff;
        #防 XSS 攻擊
        add_header X-Xss-Protection 1;
        server_name  www.rapospectre.com rapospectre.com;

在之前的 80 端口进行重定向配置:

server {
	listen 80;
	server_name rapospectre.com www.rapospectre.com;
	return 301 https://www.rapospectre.com$request_uri;
}

3. HTTP 替换

将网站所有以 http 方式获取的资源全部改为 https 方式或自动方式获取, eg :

<script src="http://xx.cdn.com/jquery.js"></script>
改为
<script src="https://xx.cdn.com/jquery.js"></script>
或
<script src="//xx.cdn.com/jquery.js"></script>

重启服务器,提示输入之前生成 csr 的密码,输入密码,重启成功,访问 https://www.rapospectre.com 可以看到 HTTPS 已经正常工作!

顺手来一发 SSLLABS测试, wtf 只有 F ?

看图发现因为

This server is vulnerable to the OpenSSL Padding Oracle vulunerability ( CVE-2016-2107 )

原来是 OpenSSL 漏洞的锅,升级 OpenSSL 到 1.0.2h 版 ( 后续版本应该也可以,博主一开始升级到了最新的 1.1.0a 结果服务器挂了 ) 即可修复漏洞:

Fix OpenSSL Padding Oracle vulnerability (CVE-2016-2107) - Ubuntu 14.04

# Based on http://fearby.com/article/update-openssl-on-a-digital-ocean-vm/

$ apt-get update
$ apt-get dist-upgrade

$ wget ftp://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2h.tar.gz
$ tar -xvzf openssl-1.0.2h.tar.gz
$ cd openssl-1.0.2h
$ ./config --prefix=/usr/
$ make depend
$ sudo make install
$ openssl version
# OpenSSL 1.0.2h  3 May 2016

# now restart your nginx or other server
$ nginx -s reload

3. HTTP2

开启 http2 , nginx 在 1.9.5 以后的版本才开始支持 http2 ,之前一直使用的是 spdy 而 ubuntu 自带的 nginx 是 1.4.6 的古董, 所以需要重新编译安装新版的 nginx ,博主选择了安装最新的 nginx 1.11.4:

  1. 下载 nginx 到 /var/tmp/nginx:
wget http://nginx.org/download/nginx-1.11.4.tar.gz
  1. 解压 nginx-1.11.4.tar.gz 文件
tar zxvf nginx-1.11.4.tar.gz
  1. 进入 ngixn-1.11.4 文件夹
cd nginx-1.2.5
  1. 查看 nginx 原来的配置
nginx -V

上面的命令将输出类似如下信息:

--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

我们在后面加上 http2 模块与 上一步中 openssl 源码( 是源码路径不是安装 )路径:

--with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

注意,如果以上信息内包含 --with-spdy_module 请去除, nginx 1.9.5 之后已弃用 spdy

  1. 执行 configure 命令,后面跟上原来 nginx 的配置
./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

configure 时可能遇到的几个错误:

  1. --with-http_xslt_module 时提示 the HTTP XSLT module requires the libxml2/libxslt libraries
apt-get install libxml2 libxml2-dev libxslt-dev
  1. --with-http_image_filter_module 时提示 the HTTP image filter module requires the GD library.
apt-get install libgd2-xpm-dev
  1. --with-http_geoip_module 时提示 the GeoIP module requires the GeoIP library.
apt-get install geoip-database libgeoip-dev
  1. ./configure: error: the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev

再次执行 configure 命令, 然后make && make install。 编译好以后 objs 目录下多出一个 nginx 文件,用它替换旧的 nginx 文件:

mv /usr/sbin/nginx /usr/sbin/nginx-backup
cp objs/nginx /usr/sbin/nginx

执行 /usr/sbin/nginx -t 命令检查配置文件返回下面的信息:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

表示 nginx 升级成功,修改 nginx 配置,加入 http2 支持:

listen       443 ssl http2 fastopen=3 reuseport;

重启 nginx 访问正常后再测一发:

搞定,个人网站加入 HTTPS 并且 SSLABS 评分 A+ 。 快来试试吧~

( 博主网站图片上传到七牛,而七牛免费似乎账户不支持 https 链接,所以有些文章比如说这篇会提示网页内有不安全的内容 )

原文地址: https://www.rapospectre.com/blog/https-deploy-guide

作者:rapospectre

10572 次点击
所在节点    程序员
39 条回复
EchoChan
2016-09-27 21:10:17 +08:00
赞!
另外,微博图床支持 https 。
isCyan
2016-09-27 21:16:00 +08:00
lfzyx
2016-09-27 21:16:28 +08:00
还敢用 startssl 的证书? http://www.solidot.org/story?sid=49821

建议使用开源的 https://letsencrypt.org
yoa1q7y
2016-09-27 21:25:41 +08:00
selinaspy
2016-09-27 21:27:58 +08:00
startssl 要被 firefox 不信任了,可以撸免费的野卡证书~
vivagonna
2016-09-27 21:30:40 +08:00
噗~ startssl 和 wosign 马上要被火狐干了,还是换一家吧,不然小绿锁没有,不信任警告倒是有了 lol
vivagonna
2016-09-27 21:31:31 +08:00
@selinaspy singlehop 的免费野卡?
lightening
2016-09-27 21:49:55 +08:00
@neilpanghttps://github.com/Neilpang/acme.sh 申请 Let's Encrypt 证书很好用。

如果用 Docker ,也可以用我的 https://github.com/SteveLTN/https-portal
vivagonna
2016-09-27 22:01:51 +08:00
用 Let's Encrypt 的 v 友有没有遇到这个问题: cron 定期 renew 证书总是失败,手动 renew 没问题,也检查过不是权限问题,命令没写错,一直没搞明白为什么
isbase
2016-09-27 22:16:02 +08:00
@vivagonna 取消默认的 cron 自己写脚本
neilp
2016-09-27 22:17:34 +08:00
vivagonna
2016-09-27 22:22:10 +08:00
@neilp 谢谢,回来试试
vivagonna
2016-09-27 22:22:54 +08:00
@isbase 谢谢
eoo
2016-09-27 22:37:10 +08:00
@vivagonna 我就用沃通了 火狐爱干啥就干啥,不缺他这一款浏览器。
bdbai
2016-09-27 22:41:01 +08:00
@eoo 等其它浏览器厂商跟进,你就倒霉了。
phithon
2016-09-27 22:48:25 +08:00
newbieo0O
2016-09-27 22:56:29 +08:00
@eoo 赶紧换到 let's encrypt
eoo
2016-09-27 23:04:42 +08:00
@bdbai 现在火狐都还没开始干呢? 等其他跟进 那都好几年的事情了,所以说还是安心用着先,不急。
HmyBmny
2016-09-27 23:06:22 +08:00
前提是你要有服务器啊,没有服务器的这个方法就不实用了。 CloudFlare 提供免费的, GitHub Pages 就能使用,参考我的博客 https://www.hmybmny.com/ , 代码在这里 https://github.com/HmyBmny/hmybmny.github.io
Adven
2016-09-27 23:21:44 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/309382

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX