I have a digital ocean droplet running Ubuntu 16.04. I followed this guide to use HTTPS with my NodeJS server.
In short
I used certbot to create an SSL certificate, which meant that at this directory /etc/letsencrypt/live/yourdomain.com/
, 3 files were created:
- privkey.pem
- cert.pem
- chain.pem
So in my server code, I have to fetch these files, which I do:
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
The Problem
When I tried to run my server using the command node server
, or using pm2 start server
I got this error message:
{ Error: EACCES: permission denied, open '/etc/letsencrypt/live/yourdomain.com/privkey.pem'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.<anonymous> (/home/myuser/mywebsite/lib/server-configurations.js:13:21)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '/etc/letsencrypt/live/yourdomain.com/privkey.pem' }
BUT
When I tried launching it using sudo node server
it did work without any problems.
Small Notice
I know I can change the permissions on the files but I would rather not do that as I have read multiple times that it is better not to change the permissions on these files.
And most importantly...
Thank you for your help :)