Skip to content

About

This repository provides a simple, step-by-step guide to install, configure, and secure Nginx on an Ubuntu VPS. It includes essential server hardening (UFW firewall), domain setup, HTTPS with Let’s Encrypt, and common Nginx configs such as static hosting and reverse proxy for Node.js apps.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

VPS SETUP

STEP 1: SSH Login

ssh root@your_ip_address

Then enter your password.

STEP 2: Update Packages

sudo apt update
sudo apt upgrade

STEP 3: Enable and Configure UFW (Firewall)

sudo ufw enable
sudo ufw allow 22

Check allow port status:

sudo ufw status

STEP 4: Install NVM & Node.js

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc

Verify installation and install Node.js:

nvm -v
nvm install --lts
# nvm install 18.17.1 (for a specific version)
node -v
npm -v

Note: If the version is not showing, please close the terminal and try again, or run source ~/.bashrc.

STEP 5: GitHub Connection

ls -al ~/.ssh
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
eval "$(ssh-agent -s)"
cat ~/.ssh/id_rsa.pub

Then copy the SSH key and paste it into your SSH and GPG keys on GitHub. Click "New SSH key", write the project name, and paste the key.

ssh -T git@github.com

STEP 6: Setup Nginx

sudo ufw allow 80/tcp
sudo ufw allow 3000
sudo ufw allow 443/tcp
sudo ufw reload
sudo apt install nginx -y
sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo ufw allow 'Nginx Full'
sudo ufw enable

STEP 7: Database Installation (MongoDB & MySQL)

Option A: Install MongoDB

MongoDB Installation Import the public key and create the list file for MongoDB 7.0 (Ubuntu 22.04):

sudo apt-get install gnupg curl -y
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

Update and install:

sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

Option B: Install MySQL

Update and install MySQL Server:

sudo apt update
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

Secure the MySQL installation:

sudo mysql_secure_installation

STEP 8: Configure Nginx

You can configure Nginx using one of two methods. Both methods support Next.js, Backend, and WebSockets.

Option 1: Using sites-available and sites-enabled (Ubuntu/Debian Standard)

Create the configuration file:

sudo nano /etc/nginx/sites-available/yourdomain.com

Option 2: Using conf.d Directory (Direct Method)

Create a .conf file directly:

sudo nano /etc/nginx/conf.d/yourdomain.conf

Add Your Server Configurations

Whichever option you chose above, add the following server blocks to the file as needed:

# 1. Next.js Frontend
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    
    location / {
        proxy_pass http://localhost:3000; # Next.js runs on port 3000
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
} 

# 2. Backend Server
server {
    listen 80;
    server_name api.myfinancialtrading.com; # Replace with your domain

    location / {
        proxy_pass http://localhost:3000; # Update port if different
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# 3. WebSocket (Optional)
server {
    listen 80;
    server_name ws.myfinancialtrading.com; # Replace with your domain
    client_max_body_size 1000M;

    location / {
        proxy_pass http://localhost:5008;
        
        # WebSocket-specific headers
        proxy_http_version 1.1;                 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection "Upgrade";  
    
        # Additional proxy headers
        proxy_set_header Host $host;                    
        proxy_set_header X-Forwarded-Host $host;      
        proxy_set_header X-Forwarded-Proto $scheme;     
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    
        # Disable caching for WebSocket connections
        proxy_cache_bypass $http_upgrade; 
    }
}

Enable and Reload

If you used Option 1, you must enable the site by creating a symlink:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

(If you used Option 2, skip the symlink step.)

For both options, test and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

STEP 9: Install Project & Setup

git clone <git repository using ssh>
cd <project-folder>
npm install
npm run build
npm run start

Install and configure PM2:

npm install -g pm2
pm2 --version
pm2 start npm --name "project-frontend" -- start # For Next.js frontend
pm2 start dist/server.js --name "bigmama-backend" # For backend
pm2 list
pm2 startup
pm2 save

Useful PM2 commands:

pm2 restart all         # Restart all apps
pm2 stop nextjs-app     # Stop a specific app
pm2 delete nextjs-app   # Delete a specific app

STEP 10: Create MongoDB Replica Set (If using MongoDB)

sudo ufw allow 27017 
sudo nano /etc/mongod.conf 

Add/modify the replication section:

replication:
  replSetName: "rs0"

Restart MongoDB and initialize the replica set:

sudo systemctl restart mongod
mongosh

Inside the MongoDB shell:

rs.initiate()

For your .env file, use the following connection string:

DATABASE_URL="mongodb://127.0.0.1:27017/kumba?replicaSet=rs0"

STEP 11: Install SSL

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

About

This repository provides a simple, step-by-step guide to install, configure, and secure Nginx on an Ubuntu VPS. It includes essential server hardening (UFW firewall), domain setup, HTTPS with Let’s Encrypt, and common Nginx configs such as static hosting and reverse proxy for Node.js apps.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors