amit prajapati

Amit Prajapati

hero

Deploy on VPS with Nginx, PM2 and SSL

In this blog, we will confidently guide you through deploying on a VPS server, setting up the environment, and configuring your domain.

Get your VPS Hosting from Hostinger today!

  • Click here to secure India’s best hosting 👉 VPS Hosting
  • Dynamic Coding with Amit Family 💥, take advantage of the discount code DCA10 for an immediate 10% off!
  • After completing your payment, you will be redirected to the homepage. Click on "Start" without hesitation.
  • dashboardhostinger start

Setup

  • Select the VPS location that is closest to your target audience. it will help you to reduce latency and improve the speed of your website. Click to .vps locationlocation

Select Operating System

  • Select your operating system. Hostinger is a greater choice for beginners as well as professionals there is three section Plan OS , OS with Panel and Applications are available.

      Plan OS:

    • Designed specifically for advanced users who are proficient with command-line interfaces and adept at configuring custom setups.
    • Ensure the manual installation of essential applications and services, including Apache, Nginx, MySQL, Node.js, MongoDB, pm2, WordPress, and more.

      OS with Panel:

    • Perfect for users who demand a hassle-free hosting environment with minimal manual setup.
    • A hosting control panel offers a graphical user interface (GUI) that allows users to manage applications, databases, domains, and files.
    • Popular Control Panels cPanel, Plesk, Hostinger’s hPanel, Webmin, VestaCP, or CyberPanel etc.

      OS with Applications:

    • Perfect for the rapid deployment of targeted applications or software stacks.
    • Pre-configured web servers, such as the MERN stack, content management systems (e.g., WordPress), or technology stacks like LAMP and LEMP.
    os

In this blog, I will discuss an OS with Application that are easy to deploy, requiring no installation or setup of applications such as Node.js, MongoDB, Nginx, PM2, etc.

OS with Application

  • Click on "Application" and select the MERN stack ( MongoDB, Express, Reactjs and Nodejs). Built on the Ubuntu 20.04 operating system. or latest.application osubuntu servermern ubuntu

Install addition feature for VPS

  • It is your choice whether to install the malware scanner or not. If you do not want to install it, then checked the box or proceed with the default option.malware scanner

Create Root Password

  • Create a root password. This password is used to log in to your server. Make sure to keep it secure and confidential.create root password

Finish your Server Setup

  • Here you can see the summary of your server. Click on "Finish Setup" to complete the process. it will take a few minutes to set up your server just wait for a while.summarysetting up your vps
  • After the setup is complete, you will receive an email containing your server details. Please be aware that setting up your server may take some time. You will then be redirected to the Hostinger VPS panel page.summary

Onboarding Screen

  • onbordingonbording 2onbording 3

Connect to VPS Server

  • There are two ways to connect to a VPS server. You can use the online browser terminal provided by Hostinger, or you can use the terminal on your PC or laptop. If you choose to use the Hostinger terminal, you will find it at the top right side of the screen, as shown in the image.
  • If you want to use your pc or Laptop terminal then to on "SSH Access" and copy the ssh root@<ip_address> .

    you can also use the online browser
  • Open the terminal on your PC or laptop, paste the ssh root@<ip_address> , and then press Enter.

    open terminal and paste
  • Press Enter. (enter yes)

    press enter
  • Enter you root password.

    enter the root passwordlogin done

Install Package

  • In this setup, While installed the OS , I have selected "OS with applications" which including Node.js, Git, Nginx, PM2, MongoDB etc. To check if all these technologies are installed, use the following commands.
    node version
    mongodb
    git
    nginx
    pm2 version

Clone Repository from Github

  • Two types of Repository are as follows :

      Public Repo:

    • Public repositories are open and accessible to anyone on the internet.
      • Go to Github account.
      • Select the repo. Click on Code . Go to HTTPS section copy the url
      • Go To terminal clone git using following command.
    • Private Repo:

    • Private repositories are accessible only to you, those you share access with, and certain members of your organization.
    • Here is how you can clone your private repositories using a GitHub token.
    • Go to Github account. Go To Settings Developer Settings Personal access tokens Tokens (classic) Right side Click on "Generate new token" Select "Generate new token" Note <enter_name_of_token> Select Exipiration date Select Scopes (permission)
    • Make sure to copy your personal access token now. You won’t be able to see it again!
    • To clone the GitHub repository, you need to format the command as follows using your token:
    • Example :
    • After cloning the repository, you need to install the necessary packages.

Add credentials to the .env file

  • To add credentials to the .env file, you must install the nano package. This tool allows you to edit text directly in the terminal. Before installing, first check if it is already installed on the server.
  • nano
  • If nano is not installed, you must first install it. In Ubuntu, it's necessary to update the system before installing any applications.
  • Create .env file following command.

Folder, pm2 and Nginx setup Here are the most useful Ubuntu commands for daily use.

  • System Updates & Upgrades

    • # Update package lists
    • # Upgrade installed packages
    • # Smart upgrade handling dependencies
    • # Remove unused packages
  • File & Directory Management

    • # List files with details
    • # Change directory
    • # Show current directory
    • # Create a new directory
    • # Delete folder & contents (use with caution)
    • # Copy directory recursively
    • # Rename or move files/folders
    • # Search for files

Folder, pm2 and run on IP

  • Go to root folder, check the list of folder by
    ls cmd
  • By default, the application folder is present. Navigate into this folder using the cd command.
  • Use the git clone command to clone the repository, and include the token since I was using a private repository.
    git clone url
  • After the cloning process is complete, navigate to the folder and install the package.

    install packages
  • After installing the package, set the credentials in the .env file.

    set .env
  • We have now added the credentials to the .env file. Now, let's build this project using the command "npm run build."

    npm run build 1npm run build 2
  • Now that the build is completed, let's run the project using pm2 and check it in the browser. To run the project, we need to use the following command.

  • Example :
    pm2 start with port number
  • Now open you browser. <ip_address>:port.
  • Exaple : 46.12.52.90:3000pm2 output
  • Your project running in ip with port.

Setup Domain and Run with Nginx.

  • Go to your domain provider and add the IP of you VPS Hosting. For Example, I am using godaddy.com. Inside the domain settings, I set up the domain.
  • I have a domain that is already running: amitprajapati.co.in. I will create a new sub-domain with my VPS hosting IP address, which is as follows.test-sub-domain
  • You can use you domain also only you need to setup the ip in @ and ip are as followdomain
  • Next, access your VPS server terminal to configure the domain using your IP address and set up the proxy.
  • Go the path are as follow.
  • Create the file for your domain configuration. I will create the file test.amitprajapati.co.in.conf for my domain name using the nano command.( you can give any name which you wanted)

    sites-availlable

    In side this file you can added the following configuration are as shown in image.

    sitea-available configurat
  • Press CTRL + X yes Enter
  • site available file output
  • Now you need to create the symbolic link in the site-enabled folder in the nginx folder. you need to go to the /etc/nginx/sites-enabled
    site available file output
    symboli linksites-enable-folder
  • You can open the file using the following cmd.
    domain configuration
  • Press CTRL + X to Close
  • Now you need to restart you nginx.
    nginx restart
  • Now you can open you browser. open the domain check the website is running if no error is there.domain webiste running

Install SSL on a VPS Using Certbot

  • ssl is not there
  • sudo update

    Once it's ready, run the following to set up a Python virtual environment

    Next, create a symbolic link so that Certbot can be executed from any path:

    Since the SSL is active for 90 days, it is recommended to set up automatic renewal. You can do so by running the following:
  • 9MAIBcsRKV
Thank You for reading.