Skip to content

Instantly share code, notes, and snippets.

@cyphercodes
Last active September 5, 2018 17:02
Show Gist options
  • Save cyphercodes/89a959e532d45020e00e6d6cb643b9f7 to your computer and use it in GitHub Desktop.
Save cyphercodes/89a959e532d45020e00e6d6cb643b9f7 to your computer and use it in GitHub Desktop.
Let's Encrypt Certificat & Express with Auto Renewal on Ubuntu 18.04
sudo apt install letsencrypt
const express = require('express'),
    app = express();
    
app.use(express.static(path.join(__dirname, 'static')));
app.get('/', (req, res) => res.send("I'm ALIVE!"));
app.listen(80);
    
certbot --webroot -w ./static -d {{YOUR DOMAIN}}
/etc/letsencrypt/live
function ensureSecure(req, res, next) {
    if (req.secure) {
        return next();
    }
    res.redirect('https://' + req.hostname + req.url);
}

app.use(express.static(path.join(__dirname, 'static')));
app.all('*', ensureSecure);

app.listen(80);
const options = {
    cert: fs.readFileSync('/etc/letsencrypt/live/{{DOMAIN}}/fullchain.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/{{DOMAIN}}/privkey.pem')
};
https.createServer(options, app).listen(443);
crontab -e
0 10 * * * /usr/bin/certbot renew && /usr/bin/node /usr/local/bin/pm2 restart serve-scriptscront

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment