如何为https Web服务器创建.pem文件 [英] How to create .pem files for https web server

查看:905
本文介绍了如何为https Web服务器创建.pem文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js中使用Express框架来创建Web服务器。我希望传输基于SSL。

I'm using the Express framework in Node.js to create a web server. I want the transport is based on SSL.

创建https Web服务器的代码如下所示。

The code to create the https web server is as below.

var app = express.createServer({
  key: fs.readFileSync('./conf/key.pem'),
  cert: fs.readFileSync('./conf/cert.pem')
});
module.exports = app;

问题:如何创建快递所需的key.pem和cert.pem?

Question: How to create the key.pem and cert.pem required by express?

推荐答案

您需要的两个文件是PEM编码的SSL证书和私钥。 PEM编码的证书和密钥是Base64编码的文本,其开头/结尾分隔符看起来像 ----- BEGIN RSA PRIVATE KEY ----- 或类似。

The two files you need are a PEM encoded SSL certificate and private key. PEM encoded certs and keys are Base64 encoded text with start/end delimiters that look like -----BEGIN RSA PRIVATE KEY----- or similar.

要创建SSL证书,首先需要生成私钥和证书签名请求,或CSR(也包含您的公钥)。您可以在各种情况下执行此操作方法,但这是在OpenSSL中的方式。

To create an SSL certificate you first need to generate a private key and a certificate signing request, or CSR (which also contains your public key).You can do this in a variety of ways, but here's how in OpenSSL.

openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem

这将导致您进入交互式提示以生成2048位RSA私钥以及包含您在提示中选择输入的所有信息的CSR。 (注意:您可以在公共名称中放置您将用于访问您网站的域名。)完成此操作后,您通常会将此CSR提交给受信任的证书权限,一旦他们验证了您的请求,您将收到证书。

This will cause you to enter an interactive prompt to generate a 2048-bit RSA private key and a CSR that has all the information you choose to enter at the prompts. (Note: Common Name is where you'll want to put the domain name you'll be using to access your site.) Once you've done this you would normally submit this CSR to a trusted certificate authority and once they've validated your request you would receive a certificate.

如果您不关心您的证书是否可信(通常用于开发目的)您可以只创建一个自签名证书。要做到这一点,我们可以使用几乎相同的行,但我们将传递两个额外的参数。

If you don't care about your certificate being trusted (usually the case for development purposes) you can just create a self-signed certificate. To do this, we can use almost the same line, but we'll pass two extra parameters.

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

这将为您提供证书(有效期为10年)和密钥对,您可以在您发布的代码段中使用。

This will give you a cert (valid for 10 years) and key pair that you can use in the code snippet you posted.

这篇关于如何为https Web服务器创建.pem文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆