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! Getting Started Well, using cloud-based technologies, you can deploy an app in different ways. If you , 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. hire experienced NodeJs developers 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. Here, we will use MySQL (As per your requirements of the app) DBMS Setup: For re-visiting HTTP Requests to our internal Node.Js Server, we’ll use . Setup a Reverse Proxy: Nginx Install Node & NPM. Install PM2 Node.Js Process Manager. From version control push/clone your code. Configure reverse proxy and run a local 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. Install MySQL Database Its installation process is the same as other Debian (Ubuntu) packages throughout the apt. sudo apt install mysql-server sudo apt update 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 Nginx Reverse Proxy 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 install nginx sudo apt update 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. > Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH ufw app list Opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic) Nginx Full: Opens only port 80 Nginx HTTP: Opens only port 443 Nginx HTTPS: Since we need HTTP through ufw, let’s allow Nginx. Allow the Nginx HTTPS or the Nginx Full. > Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6) sudo ufw allow 'Nginx HTTP' 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. /etc/nginx/ Nginx Configuration address: : /var/www/ Nginx Main Server Root address 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> <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 > 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. index index.html index.htm index.nginx-debian.html; location / { } } server { listen 80 ; listen [::]: 80 ; root / var /www/servername.com/html; #Here, you can put your domain name for ex: www.servername.com server_name 42.35 .40 .01 ; try_files $uri $uri/ = 404 ; 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. /etc/nginx/sites-available 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. Install Node Here, we will use , which you can download from . Generally, you can install Node from the apt package manager, but it will not let you install the latest version. NVM GitHub sudo apt install wget wget -qO-https: //raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash It will consider the script and execute it with bash to install NVM. install.sh 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 ~/.profile source ~/.bashrc Now run command. It will run like a charm. nvm nvm -v 0.34 Now install Node and using NVM with the following code. NPM nvm install 11.0 Then Node and NPM commands will be available across the command line. node -v 11.0 Install PM2 and Cloning Repo 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. PM2 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 push origin master git add . git commit -m "Initial Commit" git remote add origin "https://github.com/username/your_repo" Now clone the repository with the given code. cd repos mkdir 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. Configure Reverse Proxy 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 config and do essential changes to work as a reverse proxy. servername.com { 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; } } server listen 80 ; server_name 42.35 .40 .01 ; 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 . And that’s the place where you will find the NodeJs server running. http://localhost:3000 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 by your side. That would help you lead the world with your ideas and their executions. experienced NodeJs development company