本文共 1242 字,大约阅读时间需要 4 分钟。
本地ssl证书生成
Note: I ran these commands on macOS. Linux should work in the same way. I don’t guarantee for Windows.
注意:我在macOS上运行了这些命令。 Linux应该以相同的方式工作。 我不保证使用Windows。
In the project root folder, run:
在项目根文件夹中,运行:
openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
Now run:
现在运行:
openssl rsa -in keytmp.pem -out key.pem
You should now have the files cert.pem
and key.pem
in the folder.
现在,您应该在文件夹中包含文件cert.pem
和key.pem
。
With Express/Node.js, you can load the certificate and key using this code:
使用Express / Node.js,您可以使用以下代码加载证书和密钥:
const fs = require('fs')const https = require('https')const app = express()app.get('/', (req, res) => { res.send('Hello HTTPS!')})https.createServer({ key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem')}, app).listen(3000, () => { console.log('Listening...')})
If you’re using create-react-app
, change the start
script in the package.json
file to:
如果您使用的是create-react-app
,请将package.json
文件中的start
脚本更改为:
"start": "export HTTPS=true&&SSL_CRT_FILE=cert.pem&&SSL_KEY_FILE=key.pem react-scripts start",
Look at your framework/library documentation on the instructions on how to pass the certificate and key to the app.
请参阅您的框架/库文档,以获取有关如何将证书和密钥传递给应用程序的说明。
翻译自:
本地ssl证书生成
转载地址:http://qamgb.baihongyu.com/