如何使用流行的免费解决方案设置HTTPS让我们加密
如果您在自己的VPS上运行Node.js应用程序,则需要一种用于获取SSL证书的解决方案。
今天,执行此操作的标准是使用让我们加密和Certbot,来自的工具联邦军,又名电子前沿基金会(Electronic Frontier Foundation),这是一家领先的非营利组织,致力于数字世界中的隐私,言论自由和一般公民自由。
这些是我们将要执行的步骤:
安装Certbot
这些说明假设您使用的是Ubuntu,Debian或其他使用apt-get
管理软件包:
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot
您也可以在Mac上安装Certbot,以进行测试(要求自制酒):
brew install certbot
但是,您需要将其链接到真实域名,以使其有用。
使用Certbot生成SSL证书
现在已经安装了Certbot,您可以调用它来生成证书。您必须以root身份运行:
certbot certonly --manual
…或从非root用户调用sudo:
sudo certbot certonly --manual
这是详细的过程:
安装程序将要求您提供网站的域名。
…然后询问您的电子邮件:
➜ sudo certbot certonly --manual
Password: XXXXXXXXXXXXXXXXXX
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]
……并接受服务条款:
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
(A)gree/©ancel: A
…并允许共享您的电子邮件地址:
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
…最后,我们可以输入要使用SSL证书的域:
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): copesflavio.com
…安装程序询问是否可以记录您的IP地址:
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for copesflavio.com
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you’re running certbot in manual mode on a machine that is not
your server, please ensure you’re okay with that.
Are you OK with your IP being logged?
(Y)es/(N)o: y
…最后我们进入验证阶段!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:
TS_oZ2-ji23jrio3j2irj3iroj_U51u1o0x7rrDY2E.1DzOo_voCOsrpddP_2kpoek2opeko2pke-UAPb21sW1c
And make it available on your web server at this URL:
http://copesflavio.com/.well-known/acme-challenge/TS_oZ2-ji23jrio3j2irj3iroj_U51u1o0x7rrDY2E
现在,让我们离开Certbot几分钟。
我们需要通过创建一个名为的文件来验证我们是否拥有该域TS_oZ2-ji23jrio3j2irj3iroj_U51u1o0x7rrDY2E
在里面.well-known/acme-challenge/
文件夹。请注意!每当您执行此过程时,我刚刚粘贴的奇怪字符串都会更改。
您需要创建文件夹和文件,因为默认情况下它们不存在。
在此文件中,您需要放入Certbot打印的内容:
TS_oZ2-ji23jrio3j2irj3iroj_U51u1o0x7rrDY2E.1DzOo_voCOsrpddP_2kpoek2opeko2pke-UAPb21sW1c
至于文件名-每次运行Certbot时,此字符串都是唯一的。
允许Express提供静态文件
为了从Express提供该文件,您需要启用对静态文件的服务。您可以创建一个static
文件夹,然后在其中添加.well-known
子文件夹,然后按以下方式配置Express:
const express = require('express')
const app = express()
//…
app.use(express.static(__dirname + ‘/static’, { dotfiles: ‘allow’ }))
//…
这dotfiles
该选项是强制性的,否则.well-known
,它是一个点文件(以点开头),因此不会显示。这是一种安全措施,因为点文件可以包含敏感信息,并且默认情况下保留更好。
确认网域
现在运行该应用程序,并确保可以从公共Internet访问该文件。返回仍在运行的Certbot,然后按Enter键继续执行脚本。
取得证书
而已!如果一切顺利,Certbot会创建证书和私钥,并在您计算机上的文件夹中提供它们(当然,它会告诉您哪个文件夹)。
现在,只需将路径复制/粘贴到您的应用程序中,即可开始使用它们来满足您的请求:
const fs = require('fs')
const https = require('https')
const app = express()
app.get(’/’, (req, res) => {
res.send(‘Hello HTTPS!’)
})
https
.createServer(
{
key: fs.readFileSync(’/etc/letsencrypt/path/to/key.pem’),
cert: fs.readFileSync(’/etc/letsencrypt/path/to/cert.pem’),
ca: fs.readFileSync(’/etc/letsencrypt/path/to/chain.pem’),
},
app
)
.listen(443, () => {
console.log(‘Listening…’)
})
请注意,我使该服务器在端口443上进行侦听,因此需要使用root权限运行它。
另外,该服务器仅在HTTPS中运行,因为我使用了https.createServer()
。您还可以通过运行以下命令来部署HTTP服务器:
http.createServer(app).listen(80, () => {
console.log('Listening...')
})
https
.createServer(
{
key: fs.readFileSync(’/etc/letsencrypt/path/to/key.pem’),
cert: fs.readFileSync(’/etc/letsencrypt/path/to/cert.pem’),
ca: fs.readFileSync(’/etc/letsencrypt/path/to/chain.pem’),
},
app
)
.listen(443, () => {
console.log(‘Listening…’)
})
设置续订
SSL证书的有效期仅为90天,因此您需要设置一个自动系统来续订它。
如何?使用cron作业。
cron作业是一种在指定时间间隔内运行任务的方法。可以是每周,每分钟,每月等等。
在本例中,我们将按照Certbot文档中的建议每天运行两次更新脚本。
首先,找出certbot
在您的系统上。我用type certbot
在macOS上获取它,就我而言,它在/usr/local/bin/certbot
。
这是我们需要运行的脚本:
certbot renew
这是cron作业条目:
0 */12 * * * root /usr/local/bin/certbot renew >/dev/null 2>&1
上面写着“每天每12个小时运行一次:分别在00:00和12:00”。
使用以下命令将新创建的脚本添加到系统的crontab中:
env EDITOR=pico crontab -e
这将打开pico
编辑器(可以用您喜欢的任何编辑器替换)。只需输入新脚本,保存,便安装了cron作业。
完成此操作后,您可以通过运行以下命令查看活动的cron作业列表:
crontab -l
免费下载我的Express.js手册
更多速成教程:
- Express,流行的Node.js框架
- 使用Express检索GET查询字符串参数
- 使用express-validator验证Express中的输入
- 快递模板
- 使用Express服务静态资产
- 使用Express发送JSON响应
- 快速会议
- 使用Express发送回复
- 使用Express发送文件
- 使用Express-Validator清理Express中的输入
- 在Express中路由
- 具有自签名证书的Express HTTPS服务器
- Express,请求参数
- 使用Express检索POST查询参数
- 使用Express处理重定向
- 快速中间件
- 设置让我们为Express加密
- 在Express中使用HTTP标头
- 在Express中处理表格
- 使用Express处理表单中的文件上传
- 在Express中处理CORS
- 使用Express管理Cookies