`
yhz61010
  • 浏览: 550093 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:11933
社区版块
存档分类
最新评论

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

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

官网介绍:https://letsencrypt.org/

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

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

申请 Let's Encrypt 免费 SSL 证书

首先,先停止 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。注意:Apache 插件目前支持 Debian 系,即 Ubuntu 12.04+ 或者 Debian 7+。

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

注意:如果使用是的国内的 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
官网提及的其它镜像源如下:https://www.pypi-mirrors.org/

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

最终执行完结果如下:
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
成功之后,我们就可以访问自己的网站了:


其它下载方法
也可以不下载源代码,直接从官网下载可执行文件:
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> 中。

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

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

参考网址:
https://github.com/certbot/certbot
https://certbot.eff.org/#ubuntuxenial-apache
https://certbot.eff.org/docs/using.html#where-are-my-certificates
http://www.laozuo.org/7676.html
https://www.zyx.im/no-response-of-installing-python-packages/
  • 大小: 4.9 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics