How To Host A nodejs Application On Ubuntu 20.04

Let’s share on hosting Node.js application on Ubuntu 20.04. Node.js is an open-source, cross-platform JavaScript runtime environment. 

It is designed to build scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it highly efficient.

Hosting Node.js application on Ubuntu 20.04

This platform runs on Linux, macOS, FreeBSD, and Windows. It is a good choice for real-time applications, as it can handle many concurrent connections.

Before hosting the node.js application on Ubuntu 20.04, we need the following:

  • Ubuntu 20.04 server setup
  • Nginx installed and configured with SSL.
  • Domain name pointed at your server’s public IP.

With these, you are ready to set up your environment.

Let’s begin: Hosting Node.js application on Ubuntu 20.04

1. First, you need to install the latest version of node.js. 

Achieved through using this code:

cd ~

curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh

It sets up the PPA environment to add your node.js. Now it’s time to install the node.js with this command:

sudo apt install nodejs

Also, add some essential packages to ensure your build works perfectly. Run this code:

sudo apt install build-essential

Once done now, it’s time to create your first application.

2. Let’s create a node.js application.

We’ll create a simple hello word node.js application. Run the following code to get started:

cd ~

nano hello.js

On the file that appears add this code to complete the process:

const http = require(‘http’);

const hostname = ‘localhost’;

const port = 3000;

const server = http.createServer((req, res) => {

  res.statusCode = 200;

  res.setHeader(‘Content-Type’, ‘text/plain’);

  res.end(‘Hello World!\n’);

});

server.listen(port, hostname, () => {

  console.log(`Server running at http://${hostname}:${port}/`);

});

Save the file and exit the editor. The Node.js application is configured to listen on the designated address (localhost) and port (3000).

Then it responds with “Hello World!” and a 200 HTTP success code. External clients cannot connect to the application as it only listens on localhost.

3. Test your application

Let’s figure out if it works by adding this code:

node hello.js

You should get such feedback:

Server running at http://localhost:3000/

Now open another instance of the terminal and run this:

curl http://localhost:3000

Here is your output:

Hello World!

4. Let’s install node.js manager; PM2

It helps you manage your applications to run in the background. Let’s use npm to install the latest version:

sudo npm install pm2@latest -g

Once done, let’s test it by:

pm2 start hello.js

Now your environment is set. Let’s set up the Nginx server.

5. Setting up Nginx

It ensures other users have access to your application. From the above setup, it runs on a localhost. Use this code:
sudo nano /etc/nginx/sites-available/example.com

By configuring the server, it is set to handle requests at its root. 

If our server is accessed at example.com, typing https://example.com/ in a web browser will send the request to hello.js. 

The hello.js file is listening on port 3000 at the localhost.

Final words; hosting Node.js application on Ubuntu 20.04

Now you see hosting Node.js application on Ubuntu 20.04 is that easy! 

It involves setting up the necessary software, configuring the server, and deploying your application. 

Following the outlined steps, you can successfully host your Node.js application on Ubuntu 20.04. Take advantage of its stability, security, and flexibility.

Was this article helpful?

Related Articles

Leave A Comment?