0

How to run node.js applications securely on a web server using letsencrypt certs that automatically renew?

We did follow several tutorials on creating certificates and included the appropriate paths to the .pem .cert and .key files which worked for a while until the certificate did not renew.

Our server setup is debian 12 using ispconfig hosting panel.

Eventually, we figured the best solution might be to use the LE certificate files created in the ssl folder of our website.

Check this test code and see if you can improve it or have other suggestions:



var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();

var options = {
  key: fs.readFileSync('/var/www/yourwebsite.tld/ssl/yourwebsite.tld-le.key'),
  cert: fs.readFileSync('/var/www/yourwebsite.tld/ssl/yourwebsite.tld-le.crt')
};
// var options = {}

app.get('/', function(req, res){
  res.send('Hello World!');
});

http.createServer(app).listen(8880, function(err){
    if (err) console.log("Error in server setup")
    console.log("http Server listening on Port", 8880);
})
https.createServer(options, app).listen(8843, function(err){
    if (err) console.log("Error in server setup")
    console.log("https  Server listening on Port", 8843);
})

pat
  • 1
  • 1
  • Is there a reason you won't use Nginx for this? – Rachid Boudjelida Aug 28 '23 at 12:07
  • And whats your problem? Does it work or not? Do you get any error or what is your question here? – Marc Aug 28 '23 at 16:23
  • There is no longer a problem. It works great so far. I should have posted a question and then an answer. Because so far, the above is the best solution I have come across. – pat Aug 29 '23 at 06:00

0 Answers0