Pheww!!
You have worked hard and put all the effort into creating a dream Node.Js app for your project. After numerous hours of coding and debugging, you have finally come up with an end-product. Now the time has to launch your product in the market where anyone can see and use it. Yuppie!!
Excited, Aint’ you?
In this guide, I will explain the step-by-step process of deploying your first Node.Js application to the server using web application development services.
So, let’s start now!
Well, using cloud-based technologies, you can deploy an app in different ways. If you hire experienced NodeJs developers, they may sure many services like Microsoft Azure, Google Cloud, or AWS to host your web app. Also, you and your team can use PaaS services like Heroku for quick and easy deployment.
Moreover, deploying your Node.Js application to a bare Linux server on Cloud is difficult to find the right configuration and stack of technologies that stand up to your expectations.
There are many Virtual Private Server – VPS you may come across from different reliable platforms. But, here, we are going to use Hostinger as it’s cost-effective and easy to configure with different add-ons.
Here, I am using Ubuntu’s latest version – 20.04.
So, follow the required steps to deploy your Node.Js app to a bare server.
Follow the above steps to access your server’s IP or domain name on PORT 80 through your Node.Js app. Also, as a default port, 80 is considered for serving HTTP.
Now, you are required to have an SSH to your server while using Hostinger. You will be able to see the server’s IP address on the right-hand side. You can keep SSH as root.
ssh root@42.35.40.01
From Hostinger Cpanel, you can change the password of SSH.
Its installation process is the same as other Debian (Ubuntu) packages throughout the apt.
sudo apt update
sudo apt install mysql-server
When you run the configuration script, you have to make sure that you have accepted all the required permission from the server. Now configure the MySQL Deamon server. It will give tight security in a production-ready environment.
sudo mysql_secure_installation
After giving a command to the server, you can change some of the security options of MySQL.
However, you have to configure the Validate Password Plugin in the first window. This way, you can check your MySQL password’s strength.
For the second window, you have to set MySQL root user password.
Once you are done with the window, you will have to press Y and Enter for accepting the defaults for all the subsequent questions.
Now, no anonymous users can get through it. Moreover, it will also disallow remote root logins, erase the test database, and integrate these new rules. MySQL will adapt to the changes immediately.
Don’t forget to check whether the MySQL server is running or not with the given command.
systemctl status mysql
If you see an Active or running status, this means your server has been successfully started without any issue.
Now, connect the server with the MySQL client.
mysql -u root
Whoaa!
Now, we have successfully configured the Database Server without any errors.
mysql quit
Here, we use Nginx reverse proxy to receive multiple requests from clients and serve it to different servers. We will use the Nodejs server here.
As per my experience, Nginx is an amazing and reliable HTTP Server. It’s commonly used to serve static HTML Files and reverse proxy.
It provides internal access to a server or those resources that are behind a firewall.
Here, in our case, we will run the NodeJs app on port 3000 behind a firewall. Only through localhost, we can access it. Now, bind Nginx reverse proxy to port 80 – available for the public. Afterward, it can serve the localhost Nodejs server.
Now is the time to install Nginx.
sudo apt update
sudo apt install nginx
Now, apache2 is installed with Hostinger. It is fully configured to server HTTP on PORT 80. Hence, we have to disable apache2 from our server.
sudo apt-get remove apache2*
After using the command, all the apache2 packages and sub-packages will not be available.
Now configure your Nginx server with a simple Test webpage.
But before proceeding further, we have to install a firewall to secure our server.
So, let’s install ufw (uncomplicated firewall).
sudo apt install ufw
To access your server in the future, you have to ensure that all the SSH connections pass through the firewall only.
ufw allow ssh
You have to write a given code to check which network application is available and has access through the firewall.
ufw app list
> Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Since we need HTTP through ufw, let’s allow Nginx.
Allow the Nginx HTTPS or the Nginx Full.
sudo ufw allow 'Nginx HTTP'
> Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
If you can see allow in the action box of OpenSSH and Nginx HTTP, you are on the right path.
As we can assume that, it also supports IPV6 Protocol.
With a bellow written code, check the Nginx status.
sudo systemctl status nginx
Keep the Nginx status active. If it has not started automatically, then do it manually.
sudo systemctl start nginx
After completing this, we have to set up Nginx Server Block. From our given servers or multiple independent domains, you can serve subdomains.
Now, let’s create the main IP address or domain serving page root.
sudo mkdir -p /var/www/servername.com/html
Make sure the permission of web root should be correct with the given code.
sudo chmod -R 755 /var/www/servername.com
Here, -R (recursive) will define all servername.com sub-directories having the same permission as the parent directory.
Then, write a given code. This code will assign the ownership of the directory having the existing user named $USER environment variable:
sudo chown -R $USER:$USER /var/www/example.com/html
Now, let’s make a simple index.html webpage for testing purposes. Then integrate the proxy of the Nginx server to our localhost running the NodeJs app.
nano /var/www/servername.com/html/index.html
Write the below-given HTML code inside.
<html>
<head>
<title>Welcome to servername.com!</title>
</head>
<body>
<h1>
Success! The servername.com server block is working successfully!
</h1>
<b>Meow Meow!</b>
</body>
</html>
Now close the Nano text editor after saving.
Moreover, we should add configuration at /etc/nginx/sites-available for Nginx to serve this content.
sudo nano /etc/nginx/sites-available/servername.com
You should keep the config file the same as your domain name to be easy for you to maintain.
For a simple Nginx configuration, here is the code.
server {
listen 80;
listen [::]:80;
root /var/www/servername.com/html;
index index.html index.htm index.nginx-debian.html;
#Here, you can put your domain name for ex: www.servername.com
server_name 42.35.40.01;
location / {
try_files $uri $uri/ =404;
}
}
/etc/nginx/sites-available is only available for storing your configuration. If you have to make it active, create a link to the config to the sites-enabled directory to see the effect on the server.
sudo ln -s /etc/nginx/sites-available/servername.com /etc/nginx/sites-enabled/
If you want to check errors in the new add config, run the below code.
nginx -t
Now, restart the Nginx for the new config for the modification.
sudo systemctl restart nginx
Now, try to browse the address of the server’s IP or domain name. You will be able to view the test page of the sample webpage served back to you. So, the Nginx server is running successfully behind the ufw firewall.
Here, we will use NVM, which you can download from GitHub. Generally, you can install Node from the apt package manager, but it will not let you install the latest version.
sudo apt install wget
wget -qO-https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
It will consider the install.sh script and execute it with bash to install NVM.
To apply environment changes, you need to run the .profile or .bashrc from your home folder. And this completely depends upon your requirements and configuration.
source ~/.bashrc
source ~/.profile
Now run nvm command. It will run like a charm.
nvm -v
0.34
Now install Node and NPM using NVM with the following code.
nvm install 11.0
Then Node and NPM commands will be available across the command line.
node -v
11.0
PM2 is a NodeJs process manager. It tracks your running Node process to make a debug easier. Also, it shows the log files of running apps.
Let’s install it.
npm install pm2 -g
It’s very easy to install, like the PM2 process.
Now, there is a requirement of the Node.Js app’s code on the server. So, we will use Git Version Control to push and then clone the repository.
git add .
git commit -m "Initial Commit"
git remote add origin "https://github.com/username/your_repo"
git push origin master
Now clone the repository with the given code.
mkdir repos
cd repos
git clone https://github.com/username/your_repo
Suppose the framework is based on a simple Node.Js or Express server with app.js, then you need to use PM2 to start the process.
cd /repos/your_repo
Use PM2 now.
pm2 start server app.js
Here, we will use the app.js instance with PM2. We named it “server” so that we can easily identify our application.
Once you are done with this process, you will get a table showing the running/stopped apps and CPU usage.
The Nginx configuration is already added with a simple index.html page.
Now bind it to a localhost address, representing the address of the existing running NodeJs app.
This is where an Nginx takes its place for a reverse proxy.
Now open the servername.com config and do essential changes to work as a reverse proxy.
server
{
listen 80;
server_name 42.35.40.01;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect http://localhost:3000/ https://$server_name/;
}
}
Once you implement the above code, all your questions from Nginx will be redirected to http://localhost:3000. And that’s the place where you will find the NodeJs server running.
Now change the host and port of your existing NodeJs app.
The provided custom headers will be sent with the proxied request to our NodeJs servers - $remote_addr and $host. These represent the real IP address of those users who raised the requests initially.
Finally, everything is covered, and the setup is complete.
You can access your server IP address or domain name on the browser where the NodeJs server handles the request. Isn’t that pretty cool? Haah!!
Anyway, it’s a very cool thing to use a reverse proxy and port 3000 (NodeJs server port). However, this secures your web server or application.
Bang on!
You have deployed a NodeJs application on the server.
Furthermore, if you are planning to develop a feature-rich NodeJs application, you can always have an experienced NodeJs development company by your side. That would help you lead the world with your ideas and their executions.