実際のところ
前提環境
- AWS LightSail
certbot for apache
まずはcertbotを導入。
$ sudo apt install certbot
Lightsailのnode.js版はデフォではapacheが動いています。
なのでapache用の拡張ソフトを導入します。
$ sudo apt install python3-certbot-apache
設定更新のため、Apacheを止めます。
$ sudo apachectl stop
この状態で、certbotを起動。
$ sudo certbot --apache -d YOUR.DOMAIN
apacheを再起動
$ sudo apachectl restart
Let's Encryptの自動更新設定
上記の手順通りなら、/etc/cron.d/certbotに以下のようなタスクが追加されています。
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
もし無い場合、以下の様に追記するとよいそうです(こちらは何故かPython)
echo "0 0,12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/cron.d/certbot
express
偉大なるGPT-4様の指導により
const fs = require('fs'); const https = require('https'); const express = require('express'); const app = express(); // SSL証明書のファイルパスを指定 const privateKey = fs.readFileSync('/path/to/your/privkey.pem', 'utf8'); const certificate = fs.readFileSync('/path/to/your/cert.pem', 'utf8'); const ca = fs.readFileSync('/path/to/your/chain.pem', 'utf8'); // HTTPSサーバーの設定 const credentials = { key: privateKey, cert: certificate, ca: ca }; app.get('/', (req, res) => { res.send('Hello, HTTPS World!'); }); // HTTPSサーバーを起動 const httpsServer = https.createServer(credentials, app); const PORT = process.env.PORT || 3000; httpsServer.listen(PORT, () => { console.log(`HTTPS server is running on port ${PORT}`); });
で
$node app.js
とやってあげれば、無事にhttps接続ができるようになっています。
参考もと
- GPT-4大明神の偉大な教え
- Express 4.x - API リファレンス