[原创] Let's Encrypt 免费开启 HTTPS 之旅

8 篇文章 0 订阅
3 篇文章 0 订阅
Let's Encrypt 这个来头超大的,免费推广 HTTPS 的项目,实在是利国利民的大好事。现在就让我们开启自己的 HTTPS 之旅吧。

官网介绍:[url]https://letsencrypt.org/[/url]

[color=red][b]前提[/b][/color]:需要有域名解析到自己的服务器,否则无法申请到证书。虽然网上有说最好使用海外的 DNS, 但是实测 DNSPod 是可以的。

下面来具体说一下 Ubuntu 下 Apache 如何使用 Let's Encrypt 开启 HTTPS。

[b]申请 Let's Encrypt 免费 SSL 证书[/b]

首先,先停止 Apache 服务:
systemctl stop apache2
然后执行如下命令:
git clone https://github.com/certbot/certbot
cd certbot
./certbot-auto --apache --email <your email> -d m3q.xyz -d www.m3q.xyz
执行之后,程序会自动检测系统并下载必要软件。这个命令使用了 Apache 插件,为 m3q.xyz 和 www.m3q.xyz 这两个地址自动开启并配置了 HTTPS。[color=red][b]注意[/b][/color]:Apache 插件目前支持 Debian 系,即 Ubuntu 12.04+ 或者 Debian 7+。

其它插件说明:
[url]http://letsencrypt.readthedocs.io/en/latest/using.html#getting-certificates-and-choosing-plugins[/url]

[color=red][b]注意[/b][/color]:如果使用是的国内的 VPS 或云服务器的话,在自动下载必要软件时,可能会一直卡在 “Installing Python packages...” 这个地方,原因大家应该懂的, Python 源被和谐了。因此可以替换掉 PyPI 源(可以使用 豆瓣 或 阿里云 的镜像源,推荐使用豆瓣镜像源,因为这个源数据更加新一些)。新建或编辑如下文件: ~/.pip/pip.conf(注意,如果文件夹不存在的话,要先创建文件夹再创建文件),并添加如下内容:

# 豆瓣源
[global]
index-url=https://pypi.doubanio.com/simple/

[install]
trusted-host=pypi.doubanio.com
# 阿里云源
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
官网提及的其它镜像源如下:[url]https://www.pypi-mirrors.org/[/url]

即使使用了镜像源,速度也可能挺慢的,耐心等一下吧,至少不会出现无法下载的情况。

最终执行完结果如下:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/m3q.xyz/fullchain.pem. Your cert will expire
on 2017-06-22. To obtain a new or tweaked version of this
certificate in the future, simply run certbot-auto again with the
"certonly" option. To non-interactively renew *all* of your
certificates, run "certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
成功之后,我们就可以访问自己的网站了:
[img]http://dl2.iteye.com/upload/attachment/0123/8987/f9f19fb3-fbbc-3733-b295-96afdfac55b8.png[/img]

[b]其它下载方法[/b]:
也可以不下载源代码,直接从官网下载可执行文件:
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

如果不希望使用插件,而仅仅是生成证书,自己进行 SSL 配置的话,可以使用如下命令:
./certbot-auto certonly --standalone --email <your email> -d m3q.xyz -d www.m3q.xyz
生成的证书位置如下:/etc/letsencrypt/live/m3q.xyz/
其中有四个证书文件:
cert.pem - Apache 服务器端证书(若 Apache < 2.4.8 对应 SSLCertificateFile)
chain.pem - Apache 根证书和中继证书(若 Apache < 2.4.8 对应 SSLCertificateChainFile)
fullchain.pem - Nginx 所需要 ssl_certificate 文件(若 Apache >= 2.4.8 对应 SSLCertificateFile)
privkey.pem - 安全证书 KEY 文件(私钥)

修改 Apache SSL 配置文件:
vim /etc/apache2/sites-available/default-ssl.conf
按如下要求修改配置:
SSLCertificateFile /etc/letsencrypt/live/50d.win/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/50d.win/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/50d.win/chain.pem
保存之后,重启 Apache 即可:
systemctl start apache2
现在,我们的网站已经可以同时使用 HTTP 和 HTTPS 访问了,如果我们希望仅使用 HTTPS 访问网站的话,我们可以将 HTTP 跳转到 HTTPS。

添加转发规则(需要启用 rewrite 模块):
创建或编辑 .htaccess 文件,并追加如下内容:
RewriteEngine on
#RewriteCond %{HTTPS} off
#RewriteCond %{SERVER_PORT} 80
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
也可以将上述内容写到 <VirtualHost> 中。

[b]续期[/b]
虽然 Let's Encrypt 证书的有效期只有 90 天,不过我们可以手动或自动的进行续期。
下面这个命令是测试是否可以“续期”,并不会保存证书:
./certbot-auto renew --dry-run --agree-tos
若上述命令执行成功,可以继续执行如下命令进行续期操作:
./certbot-auto renew --quiet
可以将自动续期的命令加到 cron 或 systemd 中,实现自动续期功能。

[color=red][b]提示[/b][/color]:官网建议每天执行两次续期命令,并且选择一个随机时间执行续期任务,以防止任何未知原因导致无法正常续期。不要担心每天执行续期命令会有什么负面影响,因为只有当证书真正需要续期时,续期命令才会真正起作用。

参考网址:
[url]https://github.com/certbot/certbot[/url]
[url]https://certbot.eff.org/#ubuntuxenial-apache[/url]
[url]https://certbot.eff.org/docs/using.html#where-are-my-certificates[/url]
[url]http://www.laozuo.org/7676.html[/url]
[url]https://www.zyx.im/no-response-of-installing-python-packages/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值