Skip to main content

Specifics of setting up Node.js servers for WebSockets use (unused)

Step 1: Stop Apache

To use the WebSockets server properly, we want to use port 443 to serve requests, and Apache is going to get in the way of that. If your webserver has Apache installed (e.g. as part of a premade image), stop it.

sudo /opt/bitnami/ctlscript.sh stop apache

Step 2: Add the SSL/TLS certificates and configure them

There's a little bit of extra work to do here compared to the normal process for webservers, as we will need the certificates in a second form for WebSockets authentication purposes.

Use the same process to obtain SSL/TLS certificates as that used for the webservers in this article. The private key, privkey.pem, is obtained from the .domain.key file; fullchain.pem is obtained from the .domain.crt file (not the issuer file). 

Switch to root user, and convert them to PEM format using the following:

sudo su
mkdir -p /opt/bitnami/letsencrypt/node-server-certs/
openssl x509 -in /opt/bitnami/letsencrypt/certificates/_.desertedchateau.com.crt -out /opt/bitnami/letsencrypt/node-server-certs/fullchain.pem -outform PEM
openssl ec -in /opt/bitnami/letsencrypt/certificates/_.desertedchateau.com.key -out /opt/bitnami/letsencrypt/node-server-certs/privkey.pem -outform PEM
cp /opt/bitnami/letsencrypt/certificates/_.desertedchateau.com.json /opt/bitnami/letsencrypt/node-server-certs/_.desertedchateau.com.json

If needed, change ownership and permissions:

chown -R root:root /opt/bitnami/letsencrypt/node-server-certs/
chmod -R 755 /opt/bitnami/letsencrypt/node-server-certs/

Step 3: Update the server software

First, update node and npm to the latest stable versions, by installing nvm. Use the latest version of the nvm install script, which you can find on their GitHub repository: https://github.com/nvm-sh/nvm .

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --reinstall-packages-from=current --latest-npm 'lts/*'

Log out of your existing terminal session and log back in, to prevent weird npm warnings about out-of-date versions when you already updated.

Install nodemon (this particular installation requires root privileges, since the -g flag is installing globally). 

sudo npm install -g nodemon

Step 3: Deploy the messaging server code

Upload to the code to the server via WinSCP, into the /opt/bitnami/apache/htdocs directory. 

Install the dependencies for the Node application:

npm update

Step 4: Add the server's private IP to your VPC security group

Add the IP address of the Node server to your VPC security group, allowing port 6379 for Redis access, and if you need it, port 3306 for MariaDB access.

Start the node server:

cd /opt/bitnami/apache/htdocs/
nohup nodemon -I public/index.js > nohup.out 2>&1 &

If you need to kill the server later for some reason (e.g. to force a shutdown or something), you will need to find the Nodemon process and kill it:

ps aux | grep nodemon
kill <processID>