DEV Community

Ryan Orino
Ryan Orino

Posted on

How to deploy web app on vps

Deploying web application on Hostinger VPS

  • Preparing the VPS Environment
  • Setting Up the MongoDB Database
  • Deploying the Express and Node.js Backend
  • Deploying the React Frontends
  • Configuring Nginx as a Reverse Proxy
  • Setting Up SSL Certificates

1. Preparing the VPS Environment

Log in to Your VPS in Terminal

 ssh root@your_vps_ip
Enter fullscreen mode Exit fullscreen mode

Update and Upgrade Your System

  sudo apt update
Enter fullscreen mode Exit fullscreen mode
  sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Install Node.js and npm ( if not pre-installed)

  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
Enter fullscreen mode Exit fullscreen mode
  sudo apt-get install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Install Git

  sudo apt install -y git
Enter fullscreen mode Exit fullscreen mode

2. Deploying the Express and Node.js Backend

Clone Your Backend Repository

 mkdir /var/www
Enter fullscreen mode Exit fullscreen mode
 cd /var/www
Enter fullscreen mode Exit fullscreen mode
 git clone https://github.com/yourusername/your-repo.git
Enter fullscreen mode Exit fullscreen mode
 cd your-repo/backend
Enter fullscreen mode Exit fullscreen mode

Install Dependencies

 npm install
Enter fullscreen mode Exit fullscreen mode

Create .env file & configure Environment Variables

 nano .env
Enter fullscreen mode Exit fullscreen mode

add environment variables then save and exit (Ctrl + X, then Y and Enter).

Installing pm2 to Start Backend

 npm install -g pm2
Enter fullscreen mode Exit fullscreen mode
 pm2 start server.js --name project-backend
Enter fullscreen mode Exit fullscreen mode

Start Backend on startup

 pm2 startup
Enter fullscreen mode Exit fullscreen mode
 pm2 save
Enter fullscreen mode Exit fullscreen mode

Allowing backend port in firewall

 sudo ufw status
Enter fullscreen mode Exit fullscreen mode

If firewall is disable then enable it using

 sudo ufw enable
Enter fullscreen mode Exit fullscreen mode
 sudo ufw allow 'OpenSSH'
Enter fullscreen mode Exit fullscreen mode
 sudo ufw allow 4000
Enter fullscreen mode Exit fullscreen mode

3. Deploying the React Frontends

Creating Build of React Applications

 cd path-to-your-first-react-app
Enter fullscreen mode Exit fullscreen mode
 npm install
Enter fullscreen mode Exit fullscreen mode

If you have ".env" file in your project

Create .env file and paste the variables

 nano .env
Enter fullscreen mode Exit fullscreen mode

Create build of project

 npm run build
Enter fullscreen mode Exit fullscreen mode

Repeat for the second or mulitiple React app.

Install Nginx

 sudo apt install -y nginx
Enter fullscreen mode Exit fullscreen mode

adding Nginx in firewall

 sudo ufw status
Enter fullscreen mode Exit fullscreen mode
 sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

Configure Nginx for React Frontends

 nano /etc/nginx/sites-available/yourdomain1.com.conf
Enter fullscreen mode Exit fullscreen mode
 server {
    listen 80;
    server_name yourdomain1.com www.yourdomain1.com;

    location / {
        root /var/www/your-repo/frontend/dist;
        try_files $uri /index.html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Save and exit (Ctrl + X, then Y and Enter).

Create a similar file for the second or multiple React app.

 nano /etc/nginx/sites-available/yourdomain2.com.conf
Enter fullscreen mode Exit fullscreen mode
server {
    listen 80;
    server_name yourdomain2.com www.yourdomain2.com;

    location / {
        root /var/www/react-app-2/dist;
        try_files $uri /index.html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Create symbolic links to enable the sites.

ln -s /etc/nginx/sites-available/yourdomain1.com.conf /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
ln -s /etc/nginx/sites-available/yourdomain2.com.conf /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Test the Nginx configuration for syntax errors.

nginx -t
Enter fullscreen mode Exit fullscreen mode
systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

4. Configuring Nginx as a Reverse Proxy

Update Backend Nginx Configuration

nano /etc/nginx/sites-available/api.yourdomain.com.conf
Enter fullscreen mode Exit fullscreen mode
server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://localhost:4000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Enter fullscreen mode Exit fullscreen mode

Create symbolic links to enable the sites.

ln -s /etc/nginx/sites-available/api.yourdomain.com.conf /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Restart nginx

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Connect Domain Name with Website

Point all your domain & sub-domain on VPS IP address by adding DNS records in your domain manager

Now your website will be live on domain name

5. Setting Up SSL Certificates

Install Certbot

sudo apt install -y certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Obtain SSL Certificates

certbot --nginx -d yourdomain1.com -d www.yourdomain1.com -d yourdomain2.com -d api.yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Verify Auto-Renewal

certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

If you still need help in deployment:

Contact us on email : lightbeam9811@gmail.com

Top comments (0)