Hostomy Blog

How to Host Your NodeJS App on Ubuntu: A Beginner-Friendly Guide for Easy Deployment!

Dec 04, 2023

By Rahul Mukati

Rahul Mukati
How to Host Your NodeJS App on Ubuntu: A Beginner-Friendly Guide for Easy Deployment!

NodeJS is rapidly gaining popularity among developers for building web pages and APIs. In this comprehensive guide, we'll walk you through deploying a basic NodeJS app on a server and connecting a domain from scratch—no panels involved.

Prerequisites:

  • Ubuntu Server (20.04 / 22.04)
  • A domain name that is already pointing to your server IP, in this guide, we have used example.com to demonstrate!

Step 1: Install NodeJS, PM2, and Nginx

NodeJS is the go-to choice for many developers due to its versatility and efficiency. Let's dive into the deployment process with a clean slate.

Install NodeJS:

First, we need to install NodeJS. Open your terminal and execute the following commands:

sudo apt update -y
sudo apt install nodejs npm -y

This ensures that NodeJS and npm (Node Package Manager) are installed on your Ubuntu Server.

Install PM2:

PM2 is a process manager for Node.js applications. It ensures that your NodeJS app runs continuously, even after a server restart. Install PM2 globally using npm:

sudo npm install -g pm2

Install Nginx:

Nginx will act as a reverse proxy, handling incoming requests and directing them to our NodeJS app. Install Nginx with the following commands:

sudo apt install nginx -y

Once the installation is complete, start Nginx and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Congratulations! You've now set up the foundation for hosting your NodeJS app. In the next steps, we'll guide you through deploying your app and connecting a domain without relying on any control panels.

Step 2: Upload Your NodeJS App and Start on a Port

Now that we have the essential tools installed, let's proceed with uploading your NodeJS app to the server using FTP and configuring it to run on a specific port.

Upload Your NodeJS App:

  1. Install FTP Client: If you haven't installed an FTP client on your local machine, you can use FileZilla or any other preferred FTP client.
  2. Connect to Your Server: Using your server IP, username, and password, connect to your Server!
  3. Create an app directory: You will need a directory where you will be uploading your app files, in this case, we created /home/nodejsapp
  4. Upload app files: Now you can upload your app files to: /home/nodejsapp (ignore node_modules folder)

Installing dependencies:

Now that you have uploaded your app, you will need to install the node packages/dependencies your app uses.

cd /home/nodejsapp
npm install

Hurray! Our app is ready to start!

Start using PM2

Now, as your app is ready to get started, we will use PM2, which will keep running your Nodejs app in the background even if you close the terminal! For that, firstly you will have to find the main file of the app, in many cases, it's app.js or server.js.

Now using the below command you can start the app:

pm2 start app.js

Once started, the app should be live at http://SERVER_IP:APP_PORT

Now that we have started our app at a port, we still have one part left, which is connecting our app to a domain! Let's proceed!

Configure Nginx as a Reverse Proxy:

Remember we installed Nginx? Nginx is a web server, we will be using it to map our app which is currently running on a port to our domain.

Create a new vHost:

sudo nano /etc/nginx/sites-enabled/example.com

Replace example.com with your actual domain name.

Add Nginx configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
    }
}

Replace example.com with your actual domain name and localhost:3000 with your actual app port.

Test Nginx configuration:

Now that we have completed our Nginx configuration we can test if everything is fine with Nginx and there are no errors. For that run the below command:

nginx -t

Reload Nginx:

service nginx reload

Your NodeJS app is now uploaded, running with PM2, and accessible through Nginx. Make sure to configure your domain's DNS settings to point to your server's IP address. Congratulations, your NodeJS app is live! Visit your domain in a web browser to see your application in action.