domingo, 25 de diciembre de 2016

https web server with ubuntu, letsencript and nodejs

sudo apt-get install letsencrypt
letsencrypt certonly --standalone -d example.com

Note: this will generate the following files in folder /etc/letsencrypt/live/example.com: cert.pem chain.pem fullchain.pem privkey.pem

Now you are ready to implement and execute your nodejs https server. Run the following program and then open https://example.com and it should be working without any browser warning, ready for production.

const https = require('https');
const fs = require('fs');

// cert.pem  chain.pem  fullchain.pem  privkey.pem

const options = {
  key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(443);

Or using http-server command line utility:

http-server . -p 443 --ssl --cert "/etc/letsencrypt/live/example.com/cert.pem" --key "/etc/letsencrypt/live/example.com/privkey.pem"