const app = require('./server.js');
const http = require('http');
const port = process.env.PORT || 8080;
const server = http.createServer(app);
server.listen(port, () => console.log('Listening on port:', port));
- install Let's Encrypt
- Run CertBot
- change ownership of the files
- Associate filepaths with the cert and private key
- Add the https server code.
- Add an Http redirect to https
https://startupnextdoor.com/how-to-obtain-and-renew-ssl-certs-with-lets-encrypt-on-node-js/
download CertBot: wget https://dl.eff.org/certbot-auto
change permissions: chmod a+x certbot-auto
Verify your server is NOT running.
Verify incoming requests on 443 can get through (Check your port forwarding and security settigns if needed)
./certbot-auto certonly --standalone -d yourdomain.example.com
Follow the prompts
How to change file ownership in Ubuntu
like this: sudo chown -R username:group directory
We will use: sudo chown -R ubuntu /etc/letsencrypt/live
Changes ownership to Ubuntu so node can run under the Ubuntu user and have access to those files.
const app = require('./server.js');
const http = require('http');
const https = require('https');
const port = process.env.PORT || 8080;
const httpsPort = process.env.HTTPS_PORT || 8443;
const fs = require('fs');
const credentials = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('fullchain.pem')
};
const httpsServer = https.createServer(credentials, app);
const httpsRedirect = http.createServer((req, res) => {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url }).end();
});
httpsRedirect.listen(port, () => console.log('http listening on port:', port, 'to redirect to https'));
httpsServer.listen(httpsPort, () => console.log('https listening on port:', httpsPort));