in

Install Node.js, NGINX and PM2 on Ubuntu

If you are looking to build a web application instead of just a website, one of the more popular development platforms is node.js. Node.js is an open-source, cross-platform JavaScript runtime environment. You can find out more at the main Node.js website.

This tutorial will take you through two ways of creating a working node.js environment: one using a marketplace distribution we’ve created for you and the other using a base Ubuntu OS build, where we will go through the steps required to install and configure all of the relevant software.

Step 1: Register with Nucleus Web Services

If you haven’t already, the first step is to register with Nucleus Web Services (NWS). Go to https://nws.nucleus.ca/signup and provide your details to create an account. You will need to provide payment details.

You might also want to sign up for our free Nucleus unified communications platform, with free business phone number, team chat, video calling, shared text messaging and more.

Once you’ve registered you will land on the main NWS Dashboard. This is your main navigation panel and offers an at-a-glance summary of your services and costs. At the moment, yours will probably not contain much information.

The main Nucleus Web Services Dashboard

Step 3: Create a new instance

NWS offers many different services (or applications) that can be deployed to an instance on our platform. An instance is just a virtual server that runs your service which can be configured to use whatever resources (memory and storage) that you need. For our node.js build we only need one of the smallest instances that we offer.

To create your node.js instance we need to use the + Add Instance button in the main dashboard area. We’re going to use the Quick Deploy option, so select that.

Getting started with Quick Deploy

Now you need to answer a few questions. In most cases you’ll be able to use the default answer, but I’ll annotate each step here with the recommended answer based on whether you are deploying the marketplace build for node.js or the core Ubuntu OS build and installing each of the required software applications yourself.

  1. Choose your instance location: Any
  2. Choose your platform: Linux

For the Ubuntu build

  1. Choose your distribution: Ubuntu 22.04 LTS Edition (from OS Only)

For the Marketplace build

  1. Choose your distribution: Node JS (from Marketplace)
  1. Choose your instance plan: 1 GB Memory, 1 vCPU, 10GB storage is fine to start with
  2. Choose your network: default-network (the default) with Create Instance with Public IP ticked
  3. Setup Cloud Firewall (Security Policy): Yes – you can use the Linux LAMP Security group to get started
  4. Add SSH Key Pair: No. This will ensure that a password is set for the root account, although I do recommend that you go back and add a key pair later.
  5. Identity your instance: hello-world (or whatever you like!)

Step 4: Deploying

If everything has been set up properly, you should now be able to click on the Deploy button at the bottom of the screen. (If you can’t, it’s likely that you missed one of the steps above – go back and check that you’ve selected something at each stage of the process).

Selecting Deploy will take you to the Compute Instance page where all of your instances are listed. You will see that the instance reports in the State column that it is currently deploying. Deployment usually takes a couple of minutes. Once the deployment process is complete the State column will change to running. If the state doesn’t change you can still proceed with the next step after two minutes (sometimes, the state doesn’t update immediately).

For the Ubuntu build

For the Ubuntu build, we are just getting started. Now we have to install all of the software that is pre-installed with the marketplace build. The only real advantage to this is allowing complete customisation of the build.

For the Marketplace build

If you have gone with the marketplace build, you will now have a server with all of the software you need to run node.js, including node.js, nginx, pm2 and more.

You can now login to your newly deployed server from the terminal using the login details below – replacing <IPADDRESS> with the four part number from your instance detail page:

ssh ubuntu@<IPADDRESS>
Compute Instance detail page, which includes your instance’s IP address and password

You will need to supply the password available on your instance’s detail page. You can reveal it by clicking on the eye show button (see below).

Once the terminal has connected to your server you’ll see a welcome message.

For the Ubuntu build

For the Ubuntu build, we are just getting started. Now we have to install all of the software that is pre-installed with the marketplace build. The only real advantage to this is allowing complete customisation of the build.

See the detailed instructions below about how to install node.js, nginx and pm2.

For the Marketplace build

If you have gone with the marketplace build, you will now have a server with all of the software you need to run node.js, including:

  • nginx
  • node.js
  • pm2

and more.

Node.js shell user login credentials are stored under /root/.shell_user_passwords

The node.js home directory is /var/www/html/

You can modify the demo node script at /var/www/html/hello.js.

You can then see the results live by using pm2, a process manager that schedules your code to run at boot time. Node.js pm2 files are stored under /home/nodejs/

The pm2 application runs as the nodejs user, so changes to pm2 need to be run as the nodejs user:

sudo -u nodejs pm2 restart hello

The configuration files for the nginx web server that site between the node application and the internet can be found at /etc/nginx/sites-available/default.

For more information about about node.js, nginx and pm2 work together, see the detailed build instructions below.

Install nginx

Ubuntu build only

Node applications typically run on ports like 3000 and although it’s possible to reconfigure node to run on one of the ports typically reserved for websites (80, 443) it is strongly recommended that your node application actually sit behind a traditional web server that is used to handling web requests. In this tutorial we are using nginx, although other web servers like apache could equally well be used.

At the terminal type the following:

sudo apt update
sudo apt install nginx

Agree to the default prompts and once the installation process has finished nginx will be installed. You can check everything is running with the following command:

sudo systemctl status nginx

You can also check this by navigating to the IP address of your server (shown on the instance detail page – see above).

If nginx is not shown as running, you can start it with the following command:

sudo systemctl start nginx

Once nginx is running, navigate to the root directory for web sites:

cd /var/www

The one file for the default website is available in the html folder listed here. Go into the html directory and try editing some of the content of that file – for example, by using the pico file editor (other editors are available):

sudo pico index.nginx-debian.html

Those changes should be reflected in the page you can see when viewing the site using the server’s IP address.

Now that we know that nginx is installed and working properly, we can move on to the next step in this process.

Install node.js

Ubuntu build only

To install node we are going to use NVM (node version manager), which allows us to install the latest version of node (and to move between different versions of node – which is the sort of thing that you’ll either be grateful for one day or forget about completely).

To install nvm we pull the latest version from GitHub:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

(Or just follow the instructions here: GitHub – nvm-sh/nvm: Node Version Manager – POSIX-compliant bash script to manage multiple active node.js versions)

In all likelihood you will need to log out of your terminal and reconnect before proceeding – to ensure that NVM is probably set up for use.

After logging back in, you should be able to run:

nvm -v

This will return the version of nvm that you are running.

Now to install node you just need to run the following command (which will install the latest version of node):

nvm install node

You can check that node was installed properly by running the following command:

node -v

This should return the version of node that you are running.

Now let’s get a simple node application running. Type the following commands to move to the var directory and create a new app directory.

cd /var/
sudo mkdir app

Now create a new file in the newly created app directory called index.js with the following text:

var http = require(‘http’);

var server = http.createServer(function(request, response) {

    response.writeHead(200, {“Content-Type”: “text/plain”});
    response.end(“Hello World!”);

});

var port = 3000;
server.listen(port);

console.log(“Server running at http://localhost:%d”, port);

Once the file has been saved we can try running it with node.

Let’s run the node application as a background process:

node index.js &

Then we can check that it works by running a curl command to make a local web request to the port that the node.js server is running on:

curl http://localhost:3000

This should return “Hello World”. If it doesn’t, make sure that you’ve typed everything correctly and check the error message that is returned to work out what you need to do to get things running.

You can quit the background process at any point by running the following commands:

fg
ctrl-c to quit

Link node and nginx via reverse proxy

Ubuntu build only

The nginx web server handles requests by acting as what is known as a reverse proxy, forwarding on requests to the node instance.

From wikipedia: a reverse proxy is an application that sits in front of back-end applications and forwards client requests to those applications. Reverse proxies help increase scalability, performance, resilience and security. The resources returned to the client appear as if they originated from the web server itself

To set up a reverse proxy in nginx, we need to add a new configuration file. Navigate to the nginx config files:

cd /etc/nginx
cd sites-available

Create a new nginx configuration file called node-default in an editor:

sudo pico node-default

Paste the following into the newly created file:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
	location / {
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-NginX-Proxy true;
		proxy_pass http://localhost:3000/;
		proxy_ssl_session_reuse off;
		proxy_set_header Host $http_host;
		proxy_cache_bypass $http_upgrade;
		proxy_redirect off;
	}
} 

Now we’re going to enable this new configuration and disable the old one. Type the following:

cd /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/node-default node-default
sudo rm default
sudo systemctl restart nginx

So now if we go back to our node app we can see if this works:

cd /var/app
node index.js &

Now, you should be able to get a ‘Hello world’ response from both the node application itself:

curl http://localhost:3000

And the nginx webserver:

curl http://localhost/

Those should both return the same content. You can disable the node application again with:

fg 
ctrl-c to quit

But running a node application from the command line like this isn’t recommended. What we need is a tool for managing the node application – primarily to make sure that it continues to run in the background when we’ve logged out, after the app has crashed, or when the server has rebooted.

For that, we will use PM2.

Installing PM2

Ubuntu build only

PM2 is a daemon process manager that will help you manage and keep your application online. It’s very simple to install.

npm install pm2@latest -g

Now that we have pm2 installed we can go back to our hello world app and launch it using pm2 instead of calling node directly.

pm2 start /var/app/index.js --name hello-world
pm2 save

Here we’ve set a name for the running node application so that it’s easy to identify in the PM2 list of processes (although in our case here it will be the only process running).

Now nginx should be serving up the hello-world node app again, which you can check by calling localhost (or our IP address) without the port number:

curl http://localhost

By default, PM2 will restart your node app if it should crash, or if the server is rebooted. You can test that now by rebooting your server:

sudo reboot now

Wait a couple of minutes for the server to restart and then log back in and check the PM2 list of running processes:

pm2 list

And you should see that the hello-world application is still running.

Translate »