Deploying a Node App on Server

Deploying a Node App on Server

Deploying a Node App – Set up Project

Outlined below are the fundamental steps for Deploying a Node App on an Ubuntu server using Nginx and PM2.

Now that the server is configured, let’s proceed with installing our application. For Deploying a Node App on the server we need a copy of our app to this server, here we’ll utilize Git to get our app.

Typically, the Ubuntu OS provided by VPS service providers comes with Git pre-installed. To verify if Git is installed, use the following command

#Check if Git is already installed
git --version

#The expected output (the version number may vary)
git version 2.34.1

If it’s not installed, you can use the command below to install it:





#Install git
sudo apt-get install git

#Check the installed version(the version number may vary)
git version 2.34.1

Set up Node.js.

Deploying a Node App is a little more complex than Git because there are several different versions of Node that are used in production environments. Hence, it’s essential to ensure that apt-get is updated with the correct version for our application prior to installation.

We’ve previously provided a guide on how to install Node.js on Ubuntu. Please refer to that post for instructions on installing Node.js. Here is the tutorial.

Once it’s complete, we can verify that node is available by running the below command





node -v

#OUTPUT
v18.18.2

Test the app

To ensure your app is installed and functioning properly, navigate into the newly created folder and initiate it. To start the app use the below command





#Go to your app folder
cd ~/apps/your_directory/

#Install node modules 
npm i

#Start your server
node server.js

Note: In the example app, the main file is located at your_directory/server.js. Depending on your app’s setup, you may need to modify your start command accordingly.

The example app is set to listen at http://localhost:3001 . To test if it’s functioning, you can open a new Terminal session, log into your server, and make a curl request to the app.

#Change the port according to your app
curl http://localhost:3001

Deploying a Node App Using a Process Manager

After Deploying a Node App, while manually starting the app is technically sufficient for deployment, in the event of a server restart, this would necessitate manually initiating the app once more.

In production environments, it’s desirable to minimize, if not entirely remove, manual steps for deploying the app. Therefore, we’ll employ a process manager named PM2 to oversee the execution of our app. This provides additional advantages such as convenient access to logs, and straightforward control over starting, stopping, and restarting the app.

Additionally, PM2 enables us to automate the app’s startup upon server restarts, reducing one less task to manage.

Install PM2

Unlike the previously installed tools, pm2 is a Node package. It is installed using the npm command, which serves as the default package manager for Node.js.

#Deploying a Node App with pm2
sudo npm install -g pm2

The use of -g ensures that pm2 is installed globally, which is essential for its proper functionality.

Start your app using PM2

With PM2 installed, we can now start the app like this:

#Make sure you're in your project folder
cd ~/apps/your_directory/ 

#Then use the below command to start your app with PM2
pm2 start server.js --name myNodeApp

#Now you can check is your app is running or not 
curl http://localhost:3001

# OUTPUT
{"status":true,"message":"output"}

Start your app automatically when the server restarts

We’re nearly finished with getting the app up and running, just one more step remains. To ensure that PM2 restarts our app after a server reboot, we need to perform a two-step process. Let’s begin by executing the command pm2 startup systemd.

To automatically start your app when the server restarts using PM2, follow the below command

pm2 startup

You will receive a message with a command to run. Copy and paste the provided command into the terminal and press Enter. This command sets up PM2 to start on boot.

After executing the command, you should see a message indicating that PM2 has been successfully configured to start on boot. Now, your app managed by PM2 will automatically start when the server restarts.

If you want to learn more about pm2 – Click Here

Connect your APP to a domain

Step 1 – Point Your Domain to the App

After Deploying a Node App, it is time to link your Node.js application to a domain, adhere to these steps:

  1. Navigate to the “DNS” section within your domain provider’s settings.
  2. Create a DNS record dedicated to your Node.js application. This is typically an ‘A’ record pointing to your server’s IP address.

Please remember that DNS changes may take a while to propagate globally. Allow some time for these alterations to take full effect worldwide.

Step 2 – Setup Nginx

Nginx is a web server that will act as a reverse proxy to forward requests to your Node.js application. If it’s not already installed, you’ll need to do this first.

sudo apt update
sudo apt install nginx

Step 3 – Setup Config for your domain

Each domain or site you host on your server needs its own Nginx configuration file. This file contains the settings specific to that site. Use the below command to create an nginx config file for your domain name

sudo nano /etc/nginx/sites-available/domain_name

it will open a default editor copy the below code to this file, remember to change the port and domain name.





server {
    listen 80;
    server_name your_domain_name;

    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable the Nginx Configuration





sudo ln -s /etc/nginx/sites-available/directory_name /etc/nginx/sites-enabled/

Step 4 – Check Nginx configuration and reload the Nginx





sudo nginx -t

#OUTPUT
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To apply the changes in your Nginx configuration restart the Nginx.





sudo systemctl reload nginx

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *