Self Hosting

Creating DNS Records

purchased domain from Namecheap , and using Cloudfare as DNS server...need to add an A record in the DNS record management part in Cloudfare dashboard...

  • use @ for root
  • put the ip of your server in the IPv$ address section
  • disable proxy status (we're setup in our server later)
  • and set TTL (time to leave) as low as possible to avoid cache
  • finally click save
Markdown Monster icon

Query DNS Records with Dig

from your linux system run

dig softpipertesting.xyz A
dig +trace softpipertesting.xyz A

subdomain, CNAME, TEXT Records

doc link: https://wizardzines.com/zines/dns/ https://messwithdns.net/

SSH to your VPS with a domain name

 ssh root@softpipertesting.xyz

lsof -i :80

This will show the process ID (PID) of the service using port 80.

lsof -i :80

nc -l 80

listen to requests coming to port 80

nc -l 80

we can respond to the requests from netcat

HTTP/1.1 200 OK
Content-type: text/html

<body style = "background:black;color:white">
<h1>hello world</h1>
</body>

Install Caddy web server (alternative of Nginx)

doc link: https://caddyserver.com/docs/install#debian-ubuntu-raspbian

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

check if caddy is running

sudo service caddy status

add https

sudo nano /etc/caddy/Caddyfile

then replace :80 with domain name , say softpipertesting.xyz

restart the caddy service

service caddy restart

redirect ip to the root domain

213.210.21.240:80 {
        redir https://softpipertesting.xyz{uri}
}

add this to the /etc/caddy/Caddyfile file.

Multiple Caddyfile Setup

navigate to /ctc/caddy

then

mkdir sites-available 
mkdir sites-enabled
cp Caddyfile sites-available/Caddyfile
nano sites-available/Caddyfile

keep only , remove the rest

213.210.21.240:80 {
        redir https://softpipertesting.xyz{uri}
}

then

cd sites-available
mv Caddyfile http-redirects

file name is changed to http-redirects

navigate back to /etc/caddy

nano Caddyfile

modify the file to

softpipertesting.xyz {
        # Set this path to your site's directory.
        root * /usr/share/caddy

        # Enable the static file server.
        file_server

        # Another common task is to set up a reverse proxy:
        # reverse_proxy localhost:8080

        # Or serve a PHP site through php-fpm:
        # php_fastcgi localhost:9000
}

import sites-enabled/*

create a symbolic link for http-redirects file from sites-available to sites-enabled

ln -s /etc/caddy/sites-available/http-redirects /etc/caddy/sites-enabled/http-redirects

check if the link is created, from /etc/caddy/sites-enabled

ls -l

it will show , http-redirects -> /etc/caddy/sites-available/http-redirects

restart the caddy service

caddy service restart

redirect www subdomain to root domain

edit the http-redirects file in sites-available folder and add www.softpipertesting.xyz as space separated

213.210.21.240:80 www.softpipertesting.xyz {
        redir https://softpipertesting.xyz{uri}
}

setup /var/www folder

navigate to the var folder and

mkdir www
mkdir softpipertesting.xyz 

time 37 min , see to folder security

create an index file ,

cd softpipertesting.xyz
nano index.html

navigate to the Caddy file in /etc/caddy and change the root

root * /var/www/softpipertesting.xyz

Transfer files to the server with scp

scp index.html root@softpipertesting.xyz:/var/www/softpipertesting.xyz

clone a git repository (public) to server

go to the directory and then clone the repository

git clone https://github.com/Al-ArafatSiddique/testing.git . 

Hide files from Caddy File server

anyone can access https://softpipertesting.xyz/.git/HEAD we need to hide it... go to the Caddyfile...modify it

softpipertesting.xyz {
        # Set this path to your site's directory.
        root * /var/www/softpipertesting.xyz

        # Enable the static file server.
        file_server {
                hide .git
        }

handle errors in Caddyfile

softpipertesting.xyz {
        # Set this path to your site's directory.
        root * /var/www/softpipertesting.xyz

        # Enable the static file server.
        file_server { 
                hide .git 
        } 
        handle_errors {
                respond "{err.status_code} {err.status_text}"
        }

Clone Private git repository with and SSH deploy key

generate and add ssh key to VPS

ssh-keygen -t ed25519 -C "your_email@example.com"

enter a unique filename like

/root/.ssh/id_ed15519_softpipertesting.xyz

adding ssh key to the ssh-agent

exec ssh-agent bash
ssh-add ~/.ssh/id_ed15519_softpipertesting.xyz

view the key

cat id_ed15519_softpipertesting.xyz.pub

add the to the deploy key section in the github repository

install node js

https://github.com/nvm-sh/nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source /root/.bashrc //for root
source ~/.bashrc //for other user
nvm install --lts 
node -v

Create a reverse proxy with caddy

edit the Caddyfile and add the line doc link: https://caddyserver.com/docs/caddyfile/directives/reverse_proxy

reverse_proxy :3000

as our application is running on port 3000

Manage long running processes with pm2

npm i -g pm2
pm2 start --name "softpiper"
pm2 list
pm2 stop "softpiper"
pm3 delete "softpiper"