关于javascript:Node-Express-LetsEncrypt-生成一个免费的SSL证书并在5分钟内运行一个HTTPS服务器

5次阅读

共计 2445 个字符,预计需要花费 7 分钟才能阅读完成。

你到底是来干什么的?

首先,你须要这几样货色。

  • Amazon Linux 服务器,有 root 权限(通过 SSH)。
  • NodeJS:https://nodejs.org/en/
  • Express:npm install express
  • Certbot

步骤 1:安装包

EPEL(Extra Packages for Enterprise Linux)资源库中蕴含了咱们装置 Certbot 所须要的所有包,所以咱们先设置一下。

yum -y install epel-release 

接下来,咱们将装置两个让 Let’s Encrypt 运行所需的软件包:certbot 和 apache 连接器。

yum -y install certbot python-certbot-apache

第二,你将用 certbot 生成一个 SSL 证书。

`$ certbot certonly --manual`。

这张图是用碳生成的,我十分喜爱这个工具(感激都灵学生)

输出你的域名,不包含协定局部。例如 例如:yourdomain.com或甚至muchdomain.verysite.

输出 Y 而后ENTER.

留神两点。

  • a-string :你当初要创立的文件名。只有创立就能够了,前面的目录咱们会解决好的。
  • a-challenge: 关上你方才创立的文件,把这个挑战字符串放进去。别的什么都不要,只有这个挑战字符串。

** 当初,不要持续了。你须要用 Node 和 Express 运行一个 web 服务器。

在某个中央关上你的终端

  • 用你想要的名字创立一个目录,例如:服务器。
  • 在这个目录下,创立一个 JS 文件,用来运行你的服务器。
  • 在这个目录下,创立两个目录:.well-known,并在这个目录下创立:acme-challenge
  • 在目录:acme-challenge中放入你之前创立的文件:a-string

这是你应该有的。

`server
—-.well-known
——–acme-challenge
————a-string
—server.js`

重要:其实文件名并不是 个字符串,是一个很长的字母数字字符串。为了平安起见,我不能给你看我的。同样的,一个挑战 也是如此 ……

就快实现了!

应用你最喜爱的代码编辑器并复制粘贴此代码。

    // Dependencies
    const express = require('express');
    // Configure & Run the http server
    const app = express();
    app.use(express.static(__dirname, { dotfiles: 'allow'} ));
    app.listen(80, () => {console.log('HTTP server running on port 80');
    });

为了验证一切正常,请关上浏览器并导航到。*http://yourdomain.com/.well-known/acme-challenge/a-string

你的浏览器应该下载你的挑战文件。如果不是这样,请从头开始复原所有。不要碰你的 shell,从目录和文件创建重新启动。

如果一切正常,回到你的 shell,输出ENTER.

万岁,最初一步,你就实现了!

** 复制粘贴以下代码,你将有一个全新的 HTTPS 服务器运行。

    // Dependencies
    const fs = require('fs');
    const http = require('http');
    const https = require('https');
    const express = require('express');
    const app = express();
    // Certificate
    const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
    const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
    const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
    const credentials = {
        key: privateKey,
        cert: certificate,
        ca: ca
    };
    app.use((req, res) => {res.send('Hello there !');
    });
    // Starting both http & https servers
    const httpServer = http.createServer(app);
    const httpsServer = https.createServer(credentials, app);
    httpServer.listen(80, () => {console.log('HTTP Server running on port 80');
    });
    httpsServer.listen(443, () => {console.log('HTTPS Server running on port 443');
    });
    

导航到: https://yourdomain.com, 你应该会看到 “Hello there !”.


好了,你曾经到了本教程的最初。

最初一句话:* 你可能会遇到谬误,请从头开始重启教程。

  • 你可能会遇到谬误,请从头开始从新开始教程,总之别忘了用你的理论域名批改 yourdomain.coma-stringa-challenge也一样。
  • 如果没有任何成果,请让我赔罪。StackOverflow 将是你最好的敌人。
  • 本教程的目标是让你应用手动办法,这样你就能够管制简直所有的货色。在我的案例中,这是惟一无效的解决方案。
    • *

“ 红色杯子里的卡布奇诺,木桌上的红色泡沫艺术 “ 由 wu yi 在 Unsplash 上公布。
应用 www.DeepL.com/Translator 翻译(免费版)

正文完
 0