List of content you will read in this article:
VPS is a trustworthy choice when it comes to deploying Node JS applications. It may be somehow tricky and complex to configure and deploy such web apps on a server, but we're going to break down all the facts into simple steps and streamline the process for you.
Based on the security and efficiency of VPS solutions for NodeJS apps, they have become a seamless solution in this case. However, you need to master the process if you want no errors and pitfalls during and after the process is done.
Why Deploy a Node.js App on a VPS?
Node.js is an all-in-one solution for those developers who want to develop and deploy full-stack web and mobile applications.
It comes with integrated features and a bunch of components that aim to simplify the process of building web and mobile apps. Node.js can be integrated with different databases and web development frameworks to deliver unique web apps that are ready to deploy on a VPS server.
Compared to shared hosting, a VPS has way more features in the case of controlling the environment and managing the resources.
You can easily install custom software tools on VPS environments and apply your desired settings. Integrations with other software tools, such as CRM tools and web analytics tools, turn VPS solutions into reliable web environments.
With a VPS, you will get many of the benefits dedicated servers provide. However, VPS plans are more affordable compared to dedicated servers.
Node.js websites are established for a wide range of businesses, including web platforms, online games, and the cryptocurrency industry. Therefore, these websites will experience high traffic volumes, so shared hosting is not a helpful solution.
Online stores can significantly benefit from VPS hosting since it provides great resources and computing hardware. Deploying Node.js apps on a VPS is a good choice as it brings an opportunity to customize web apps and minimize server errors.
As your website grows, you will experience a decrease in your website's loading times. This is due to the large amounts of hardware resources you'll need. With VPS hosting, it's so easy to upgrade your hosting plan and achieve better computing hardware.
Need Affordable VPS Hosting
Starting From $5.99/Monthly
🏅 Fast Delivery
🏅 25+ Data Centers
Prerequisites for Deploying Node.js App on a VPS
Before diving into the main process, you need to install some prerequisites for deploying Node.js apps on a VPS.
Node.js can be downloaded freely from the main website, and NPM should be installed accordingly.
You need your database installed and configured so everything is ready for a successful Node.js app deployment.
Here is a checklist of all the tools you need for this:
- Install node.js and NPM
- Install the desired Database: MySQL or MongoDB
Note that with the Ubuntu OS installed on your VPS, it's easy to get all those packages by making use of the terminal.
Step-by-Step Guide to Deploy a Node.js App on a VPS
If you're using Ubuntu Server, the process of installing the Node.js packages and database systems is as simple as ABC.
Step 1: Setting Up Your VPS
VPS hosting services are mostly working based on Linux so that you'll get optimal security for your web platform.
Configuring your VPS is easy, and you need to update your VPS at the first step by entering the following command in your terminal:
sudo apt-get update
Node.js is a part of the open-source community, and it comes with Ubuntu's official repository by default. So, you only need to enter the following command to get the final stable version of Node.js. Learn more about checking Node version
sudo apt-get install nodejs
In the next step, you need to install the latest version of npm that acts as a package manager, and it's essential to deal with Node.js packages.
sudo apt-get install npm
Step 2: Transferring Your Node.js Application to Your VPS
By using the terminal, you can easily verify that Node.js and NPM are installed correctly.
node -v
npm -v
You can now create a new Node.js application or transfer an existing app to your VPS. It's recommended to transfer the app through a zip file and unzip the file on VPS.
Step 3: Configuring Your VPS to Run Your Node.js Application
We can create a simple Node.js application that is running on port number 3000, as below. By entering the command "node app.js," your app will be started.
mkdir myNode_app
cd myNode_app
npm install express
# app.js
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World !'))
app.listen(3000, () => console.log('Node.js app listening on port 3000.'))
The output for the "node app.js" command on localhost:3000:
Hello World!
After running your Node.js app on the server, if you exit the application, it will stop running, and this is a major problem. We should make sure it is running all the time, and the PM2 package solves this problem.
npm install pm2 -g
Make sure you're inside your Node.js app directory. So, run the following command to start the app using pm2.
pm2 start app.js
To daemonize your app to start the server on all reboots, you can run the following commands:
sudo pm2 startup
sudo pm2 save
You need to connect to your VPS based on the configuration credentials your VPS provider sent to you.
- IP address
- A user (root by default)
- A port
The syntax for connecting to the server in the terminal is as follows:
- ssh root@<ip address>
Nginx is a powerful web hosting software used by a lot of companies worldwide. It is used as a reverse proxy and helps us provide remote connections to our Node.js application. By default, our Node.js application runs as a local app, and Nginx helps us map requests to the port number where our Node app is running.
Open up your terminal and enter the following command to install Nginx:
sudo apt-get install nginx
Now cd to Nginx virtual host configuration directory and create a server block to establish a reverse proxy.
cd /etc/nginx/sites-available
nano node_app.conf
server {
listen 80;
server_name SUBDOMAIN.DOMAIN.TLD;
location / {
proxy_pass https://PRIVATE_IP:3000;
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;
}
}
Now all the traffic that targets the domain SUBDOMAIN.DOMAIN.TLD will be forwarded to https://PRIVATE_IP:3000, where your Node.js app is running.
It's advisable to test the configuration after saving the above lines by entering the following command in the terminal.
sudo nginx -t
If everything is good, you will get the success message.
The next step involves restarting the Nginx service to load the config file you just changed for it.
sudo service nginx restart
Step 4: Starting and Monitoring Your Application
This way, you can access your Node.js application from anywhere. In each step, you can check whether the server is running correctly or not.
To monitor your Node.js application, the pm2 package is just the right solution. It allows developers to monitor if the application is running properly or not.
You can manage multiple apps using PM2, and it provides you with powerful application and error management features.
Common Issues and How to Troubleshoot Them
There might be some issues with a Node.js application, and deployment issues are also inevitable.
When you receive an error from Nginx, it's stored in the log file. You can run the following command to see what problem causes the error.
sudo cat /var/log/nginx/error.log
One of the most common errors during the deployment process is the syntax error in your configuration file. So, make sure you edit the file correctly without any syntax mismatching.
Another issue involves the running of the nginx service, which you can check the status using this command in the terminal. If the service is running, it will respond as "active (running)":
systemctl status nginx
Firewall problems are common when it comes to installing a Nginx server and deploying a Node.js application. You can install the ufw firewall management tool and check the status.
If Nginx is not allowed, you need to set the proper configuration for it.
// install ufw and check the status
sudo apt install ufw
sudo ufw status
// allow Nginx
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow 'Nginx Full'
Conclusion
VPS hosting allows developers to easily and efficiently deploy their Node applications in minutes. There is a defined step-by-step process for this, and you only need to install and configure some Node.js tools to start your web platform. If you want to get rid of Node.js app deployment issues, VPS hosting is a trustworthy and reliable solution.
Hello, everyone, my name is Lisa. I'm a passionate electrical engineering student with a keen interest in technology. I'm fascinated by the intersection of engineering principles and technological advancements, and I'm eager to contribute to the field by applying my knowledge and skills to solve real-world problems.