The Only VPS Guide You’ll Need: From Setup to Production in Simple Steps
December 22, 2024

The Only VPS Guide You’ll Need: From Setup to Production in Simple Steps


Setting up a production-ready VPS was much easier than I thought – Best Guide on How toDeploy Backend Code in Production

Let’s face it… Tired of deployment tutorials that make you hesitate? Yes, me too.

See, I’ve noticed something while helping other developers – most guides skip small but very important steps. Do you know what happens next? You encounter an error in the middle of the night and open 50 browser tabs trying to figure out what the problem is.

That’s why I created this guide. I’ll walk you through every step of deploying your server. Whether you are dealing with:

  • Node.js
  • go
  • Spring Boot (Java)
  • rust
  • Django
  • Lavelle
  • flask
  • or any other server

I’ll cover the little details that often confuse people.

Think of it like your friend sitting next to you and helping you deploy your server without any effort😅.

Trapped somewhere? Just comment below or message me directly. I really answered!

Ready to deploy your servers the right way? Let’s get started!


1️⃣Step 1-Connect to VPS

✅ 1. Choose a hosting provider

Buy a VPS plan from any hosting provider.

✅ 2. Get voucher

Whether you are a freelancer or work in an office, you need three things:

  • IP address: 568.82.48.166
  • username: root
  • password: Subham@Xam_08

example:

568.82.48.166  
root  
Subham@Xam_08  
Enter full screen mode

Exit full screen mode

✅ 3. Connect via terminal

Open your terminal and type:

ssh root@568.82.48.166
Enter full screen mode

Exit full screen mode

✅ 4. Verify connection

You will see a message like this:

The authenticity of host '568.82.48.166 (568.82.48.166)' can't be established.
ED33319 key fingerprint is SHA256:kLP3I9......QgQI.
This key is not known by any other names.

Are you sure you want to continue connecting (yes/no/[fingerprint])?
Enter full screen mode

Exit full screen mode

Just enter yes.

✅ 5. Enter password

Next you will be prompted:

Warning: Permanently added '568.82.48.166' (ED25519) to the list of known hosts.
root@568.82.48.166's password:
Enter full screen mode

Exit full screen mode

Paste your password (Subham@Xam_08) and press Enter.

(Note: The terminal will not display any characters when entering your password. Just press Enter.)

✅ 6. success!

If everything is correct, you will see:

       Welcome to Ubuntu 54.54.1 LTS (GNU/Linux 8.65.0-424-generic x86_64)

       * Documentation:  https://help.ubuntu.com
       * Management:     https://landscape.canonical.com
       * Support:        https://ubuntu.com/advantage

       System information as of Sat Dec 21 11:56:39 PM UTC 2024

       System load:  0.01555554443125      Processes:               292
       Usage of /:   2.1% of 20000.02GB   Users logged in:         0
       Memory usage: 5%                 IPv4 address for kms160:               568.82.48.166
       Swap usage:   0%

       * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
       just raised the bar for easy, resilient and secure K8s cluster deployment.

       https://ubuntu.com/engage/secure-kubernetes-at-the-edge

       137 updates can be applied immediately.
       To see these additional updates run: apt list --upgradable


       *** System restart required ***
       Last login: Mon Dec  9 06:55:53 2024 from 203.145.52.235
       root@ajfaljflaf:~#
Enter full screen mode

Exit full screen mode

what is root@ajfaljflaf:~# What does it mean?

This prompt confirms that you have logged into the server using the hostname ajfaljflaf as root user. You are now ready to execute commands and configure the server.



2️⃣ Step 2 – Prepare your system

✅ 1. clear terminal

After logging in, enter:

    clear
Enter full screen mode

Exit full screen mode

✅ 2. Verify directory

Check the current directory and list files:

    pwd
    ls
Enter full screen mode

Exit full screen mode

Output example:

    root@hdicj8psx6:~# pwd  
    /root  
    root@hdicj8psx6:~# ls  
    snap  
Enter full screen mode

Exit full screen mode

✅ 3. Update package list

Refresh the package manifest to ensure the latest updates:

    sudo apt update
Enter full screen mode

Exit full screen mode

Output example:

    Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease  
    Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]  
    Ign:4 https://ppa.launchpadcontent.net/certbot/certbot/ubuntu jammy InRelease  
    Err:5 https://ppa.launchpadcontent.net/certbot/certbot/ubuntu jammy Release  
      404  Not Found [IP: 185.125.190.80 443]  
    Reading package lists... Done  
Enter full screen mode

Exit full screen mode

If you encounter warnings or errors, review them to make sure they are not blocking important updates.

✅ 4. Upgrade installed packages

Install available updates and upgrades:

    sudo apt upgrade
Enter full screen mode

Exit full screen mode

Tip example:

    124 upgraded, 13 newly installed, 0 to remove, and 0 not upgraded.  
    Need to get 537 MB of archives.  
    After this operation, 679 MB of additional disk space will be used.  
Enter full screen mode

Exit full screen mode

type yes continue.

✅ 5. Handle upgrade prompts

During the upgrade process, you may see setup prompts:

  • Example 1: Confirm operation
    Select “OK” using tab key and press Enter:

  • Example 2: Select default values
    Select a preset option or make changes, then press Enter:

(Note: You need to use tab toggle option key)

  • Check upgrade completion status

    After a successful upgrade, you will see a message about restarting the service:

    Restarting services...  
    /etc/needrestart/restart.d/systemd-manager  
    systemctl restart nginx.service polkit.service ssh.service udisks2.service  
    Service restarts being deferred:  
    systemctl restart networkd-dispatcher.service  
    systemctl restart systemd-logind.service  
    

    This confirms that the service has been restarted or deferred as necessary.



3️⃣ Step 3 – Install dependencies and copy the repository with correct authentication

✅ 1. Install Node.js

To install Node.js, execute:

   sudo apt install nodejs
Enter full screen mode

Exit full screen mode

This will install Node.js.

✅ 2. Install Git

To install Git, execute:

   sudo apt install git
Enter full screen mode

Exit full screen mode

✅ 3. Push your repository to GitHub

If your code hasn’t been pushed to GitHub yet, follow these steps locally:

   # 1. Initialize a new Git repository in your project folder
   git init

   # 2. Add all files to the staging area
   git add .
   # Or add specific files
   git add filename.txt

   # 3. Make your first commit
   git commit -m "Initial commit"

   # 4. On GitHub:
   # - Create a new repository
   # - Copy the repository URL (ends with .git)

   # 5. Link your local repo to the GitHub repository
   git remote add origin https://github.com/username/repository.git

   # 6. Push your code to GitHub
   # If using 'main' branch (recommended)
   git push -u origin main

   # If using legacy 'master' branch
   git push -u origin master
Enter full screen mode

Exit full screen mode

Other useful commands:

   # Check repository status
   git status

   # Check remote connection
   git remote -v

   # If you need to rename your branch from master to main
   git branch -M main
Enter full screen mode

Exit full screen mode

notes: If you encounter an authentication error, you need to:

  1. Set up SSH key.
  2. Use a personal access token instead of a password.
  3. Authenticate using GitHub CLI (gh).

For guidance, see this guide.

✅ 4. Clone repository

If this is your first time connecting, you will see the following message:

   Cloning into 'xyz'...
   The authenticity of host 'github.com (60.404.404.404)' can't be established.
   ED26619 key fingerprint is SHA256:+DiYfsfsfsf....4UvCOqU.
   This key is not known by any other names.
   Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
   Warning: Permanently added 'github.com' (Exxx519) to the list of known hosts.
   git@github.com: Permission denied (publickey).
   fatal: Could not read from remote repository.

   Please make sure you have the correct access rights.
Enter full screen mode

Exit full screen mode

✅ 5. Troubleshoot authentication issues

If you encounter the above error, please follow these steps:

  • Install GitHub CLI (gh):

     sudo apt install gh
    
  • Authenticate with GitHub:

     gh auth login
    

    Follow the prompts:

    • choose GitHub.com.
    • choose HTTPS for the agreement.
    • Authenticate to Git by choosing to use your GitHub credentials Login with a web browser.

    Tip example:

     ? What account do you want to log into?  
     > GitHub.com  
     ? What is your preferred protocol for Git operations?  
     > HTTPS  
     Authenticate Git with your GitHub credentials? (Y/n)  
     y  
     ? How would you like to authenticate GitHub CLI?  
     > Login with a web browser  
     ! First copy your one-time code: 40DE-1645  
     - Press Enter to open github.com in your browser...  
    
    • Copy the one-time code and open the link in your browser.
    • Log in to your GitHub account and paste the code.
    • Return to your terminal:
       ✓ Authentication complete. Press Enter to continue...
       - gh config set -h github.com git_protocol https
       ✓ Configured git protocol
       ✓ Logged in as Subham-Maity
    

✅ 6. Add SSH key to GitHub

refer to this guide Add your SSH key to GitHub.

✅ 7. Clone the repository again

After adding the SSH key, clone your repository:

   git clone git@github.com:username/repository.git
Enter full screen mode

Exit full screen mode

✅ 8. Verify clone

After cloning, check if the repository exists:

   ls
Enter full screen mode

Exit full screen mode



4️⃣ Step 4 – Set up your project

✅ 1. Navigate to your project directory

First, list the directories used to locate the project:

ls
Enter full screen mode

Exit full screen mode

Then, go into your project folder:

cd 
Enter full screen mode

Exit full screen mode

hint: Enter the first 2-3 letters of the project folder and press Tab key to automatically complete the name.

✅ 2. Install project dependencies

Execute the following command in the project folder to install the necessary packages:

npm i
Enter full screen mode

Exit full screen mode

This may take several minutes depending on the size of the project.

✅ 3. settings .env document

If your project does not have .env archive (usually excluded from non-private repositories), you can create one to store environment variables:

  • Open a file called .env In your project directory:
   nano .env
Enter full screen mode

Exit full screen mode

  • Add variables in the following format:
   DB_HOST=your-database-host
   DB_USER=your-database-username
   DB_PASSWORD=your-database-password
   PORT=3000
   SECRET_KEY=your-secret-key
Enter full screen mode

Exit full screen mode

  • Save and exit the editor:
    • according to Ctrl + OThen Enter save.
    • according to Ctrl + X quit.

✅ 4. Verify .env Configuration

Check your .env The file exists and is configured correctly:

cat .env
Enter full screen mode

Exit full screen mode

✅ 5. Run the application

Finally, start your application with the following command:

npm start
Enter full screen mode

Exit full screen mode

Alternatively, if you use a framework or script, follow its specific commands, such as:

npm run dev
Enter full screen mode

Exit full screen mode

notes: If you encounter an error, please review .env variables, checks installed dependencies, and looks for any missing configuration.


The following is a rewritten version of the content with better structure, clarity, and formatting:



5️⃣ Step 5 – Start your project


Install PM2

To keep your application running you can use particulate mattera powerful process manager for Node.js applications. It allows zero-downtime restarts and simplifies DevOps tasks.

Execute the following command to install PM2 globally:

npm i -g pm2
Enter full screen mode

Exit full screen mode


Start your application

If you are using NestJSplease follow this detailed guide to deploy your NestJS application using PM2:

🔗 Deploy NestJS application on Linux Ubuntu server using PM2

If you are working with Node.js When applying, you can refer to this guide:

🔗 How to launch Node.js applications using PM2

After the configuration is complete, enter the following command to start the server:

npm start
Enter full screen mode

Exit full screen mode

Open a browser and navigate to:

http://:3333/xam
Enter full screen mode

Exit full screen mode

For example:

http://404.89.46.183:3333/xam



📝 Handle frequently asked questions


Firewall blocks your website

If you can’t access the server, it may be due to a firewall issue. First, verify the firewall rules using the following command:

sudo iptables -L
Enter full screen mode

Exit full screen mode

This command lists all current firewall rules. If port 3333 is not listed or there is an error, you can determine the problem by searching on platforms like Stack Overflow or tools like GPT, Claude, Copilot, or Llama. They often provide detailed insights to solve specific problems.


Update firewall rules

If the firewall blocks incoming traffic on port 3333, use the following command to update the rules:

sudo iptables -A INPUT -p tcp --dport 3333 -j ACCEPT
Enter full screen mode

Exit full screen mode

Command breakdown:
-A INPUT: Append the rule to the INPUT chain.
-p tcp: Specify TCP protocol.
–dport 3333: Define port 3333.
-j ACCEPT: Allow traffic on this port.

Now verify the updated rules:

sudo iptables -L
Enter full screen mode

Exit full screen mode

You should see something like this:

ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:3333
Enter full screen mode

Exit full screen mode


Keep firewall rules

To ensure that firewall rules persist after a reboot, install netfilter-persistent pack:

sudo apt install netfilter-persistent
Enter full screen mode

Exit full screen mode

Save rules:

sudo netfilter-persistent save
Enter full screen mode

Exit full screen mode



Manage PM2 processes

  1. Start your application with PM2

    Start the PM2 server using the following command:

       pm2 start dist/main.js --name server -f
    

    this -f flag forces PM2 to restart the process.

  2. Delete unused processes

    If there are previously executed processes, you can delete them using their IDs:

   pm2 delete 0
   pm2 delete 1
Enter full screen mode

Exit full screen mode

This ensures that duplicate processes do not conflict.


Now your application should run smoothly! 🎉

_Note: I will update the blog with all the news about SSL, nginx and all other related things _

2024-12-22 05:54:12

Leave a Reply

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