Set up GitHub SSH connection on VPS

    Terminal commands:
  1. Generate SSH key pair: ssh-keygen -t rsa -b 2048
  2. Navigate to SSH directory: cd ~/.ssh/
  3. View public key: cat id_rsa.pub
  4. Copy the entire public key content (starts with ssh-rsa)
  5. Go to GitHub Settings → SSH and GPG Keys
  6. Click "New SSH Key"
  7. Paste key and save
  8. Test connection: ssh -T git@github.com
Important Notes
  1. Run eval "$(ssh-agent -s)" in each new session
  2. Use SSH URLs for Git operations

Enable Firewall Using UFW

    Terminal Command:
  1. sudo ufw enable
  2. Press y and hit Enter

Allow Applications Through UFW

  1. List available applications: sudo ufw app list (to view available apps)
  2. Allow an application (e.g., OpenSSH): sudo ufw allow OpenSSH (replace "OpenSSH" with your desired app)

Resolve Python Access Issue on VPS

Error Message
Python Access Error Message
    Terminal Command:
  1. sudo chmod +x /usr/bin/python3

Run pgAdmin in Docker

  1. docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=admin@admin.com" -e "PGADMIN_DEFAULT_PASSWORD=admin" -d dpage/pgadmin4
  2. Navigate to http://159.65.145.88:5050 to access pgAdmin.
    pgAdmin Interface
  3. Enter the email and password specified in Point 1.

Connect Local PostgreSQL to pgAdmin4 in Docker

    Retrieve Docker Container IP:
  1. docker ps
    Docker PS Output
  2. docker inspect <container_id> (replace <container_id> with your actual container ID)
  3. Scroll down to the Network section.
    Docker Network Configuration
  4. Add the following line to your pg_hba.conf file:
    host all all 172.17.0.2/32 md5 (ensure you add "/32" for IPv4 or "/128" for IPv6)
    PostgreSQL Configuration
  5. Click on Add Server, enter your server IP address, select a database, and provide the database password. (Using a super user account is recommended)
    Add Server in pgAdmin

Free Up a Port in aaPanel

  1. Navigate to Security > Firewall, add the desired port, and click the Open button.
    Open Block Port

Add a New User to VPS

    Terminal Commands:
  1. adduser user_name (Replace user_name with the desired username. You will be prompted to enter a password and additional information.)
  2. adduser user_name sudo (Grant sudo privileges to the new user)

Access PostgreSQL in Terminal

    Terminal Commands:
  1. su - postgres
  2. psql

Essential PostgreSQL Commands

  1. \l (List all databases)
  2. \q (Quit PostgreSQL)

Set Up Go (Golang)

  1. Create a project directory and navigate into it:
  2. Open the terminal in that directory.
  3. Terminal Commands:
  4. wget https://go.dev/dl/go1.19.4.linux-amd64.tar.gz
  5. tar -xvf golang_download_file_name.tar.gz (Replace golang_download_file_name.tar.gz with the actual file name)
  6. sudo mv go /usr/local (Move Go to the local directory)

Miscellaneous Tips

  1. UFW (Uncomplicated Firewall)
  2. Linux systems automatically create a tmp folder after booting.
  3. Location of the tmp folder: root directory. (If not found, type cd and press Enter to navigate to the tmp folder)
  4. Navigate inside the tmp folder.

PostgreSQL Installation and Setup

  1. sudo apt update && sudo apt -y full-upgrade
  2. sudo reboot
  3. sudo apt update
  4. sudo apt install curl gpg gnupg2 software-properties-common apt-transport-https lsb-release ca-certificates
  5. curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
  6. echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
  7. sudo apt update
  8. sudo apt install postgresql-13 postgresql-client-13

Configure PostgreSQL for Remote Connections

  1. Edit PostgreSQL configuration: sudo nano /etc/postgresql/13/main/postgresql.conf (replace "13" with your PostgreSQL version)
  2. In postgresql.conf, set listen_addresses to your VPS IP address. For example:
                                    listen_addresses = '192.168.10.11'
                                
  3. Save and close the file.
  4. Edit pg_hba.conf: sudo nano /etc/postgresql/13/main/pg_hba.conf
  5. Change the host entry from:
                                    # Accept from anywhere
                                    host all all 0.0.0.0/0 md5
                                
    To:
                                    # Accept from trusted subnet
                                    host all all 10.10.10.0/24 md5
                                
  6. Save and close pg_hba.conf.
  7. sudo systemctl restart postgresql

GitHub CI/CD Packages for VPS Deployment

CI/CD Boilerplate

CI/CD Workflow
  1. Variables formatted as ${{ variable_name }} can be stored in the repository's Settings under Secrets and Variables in the Actions section.
  2. Create Variables
  3. In the Actions section of Secrets and Variables, click on New Repository Secret, then enter a Name and Secret.
    Example: Name: HOST Secret: localhost
  4. Access your secret key using ${{ secrets.HOST }}

Check Running Service Status

  1. systemctl status <service_name> (e.g., nginx, mysql)
  2. systemctl restart <service_name>
  3. systemctl stop <service_name>

Angular Configuration for Nginx

  1.                         location / {
                                # First attempt to serve request as file, then
                                # as directory, then redirect to index(angular) if no file found.
                                try_files $uri $uri/ /index.html;
                            }
                           

Resolve "node: Permission Denied" Error

  1. sudo chmod 777 -R Your_Dir/ (Replace Your_Dir with your directory)

Deploy a Static Website

  1. sudo apt install nginx
  2. sudo ufw app list (to check if Nginx HTTP and Nginx HTTPS exist)
  3. sudo ufw enable
  4. sudo ufw allow 'Nginx HTTP'
  5. sudo ufw allow 'Nginx HTTPS'
  6. systemctl status nginx (to check Nginx status)
  7. sudo mkdir -p /var/www/html/your_folder
  8. sudo chown -R $USER:$USER /var/www/your_folder
  9. sudo chmod -R 755 /var/www/your_folder
  10. sudo nano /etc/nginx/sites-available/your_domain
  11. Configure Nginx by replacing your_folder with your directory and your_domain with your domain name. Ignore sections managed by Certbot.
                                server {
                                    root /var/www/your_folder;
                                    index index.html index.htm index.nginx-debian.html;
                                    
                                    server_name your_domain;
                                    
                                    location / {
                                        try_files $uri $uri/ /index.html;
                                    }
                                    
                                    listen [::]:443 ssl ipv6only=on; # managed by Certbot
                                    listen 443 ssl; # managed by Certbot
                                    ssl_certificate /etc/letsencrypt/live/path/fullchain.pem; # managed by Certbot
                                    ssl_certificate_key /etc/letsencrypt/live/path/privkey.pem; # managed by Certbot
                                    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
                                    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
                                }
                                server {
                                    if ($host = your_domain) {
                                        return 301 https://$host$request_uri;
                                    } # managed by Certbot
                                    
                                    listen 80;
                                    listen [::]:80;
                                    
                                    server_name your_domain;
                                    return 404; # managed by Certbot
                                }
                            
  12. sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
  13. sudo nginx -t

Deploy a Node.js Application

  1. Run your Node.js backend.
  2. Update Nginx configuration as shown below, replacing http://localhost:3000/ with your application's URL.
  3.                             server {
                                    server_name nodejs.your_domain.com;
                                    
                                    location / {
                                        proxy_set_header X-Real-IP $remote_addr;
                                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                                        proxy_set_header Host $host;
                                        proxy_set_header X-NginX-Proxy true;
                                        proxy_pass http://localhost:3000/;
                                        proxy_redirect http://localhost:3000/ https://$server_name/;
                                    }
                                    
                                    listen 443 ssl; # managed by Certbot
                                    ssl_certificate /etc/letsencrypt/live/path/fullchain.pem; # managed by Certbot
                                    ssl_certificate_key /etc/letsencrypt/live/path/privkey.pem; # managed by Certbot
                                    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
                                    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
                                }
                                server {
                                    if ($host = your_domain) {
                                        return 301 https://$host$request_uri;
                                    } # managed by Certbot
                                    
                                    listen 80;
                                    server_name your_domain;
                                    return 404; # managed by Certbot
                                }
                            
  4. Ensure your Node.js app is running on port 3000 or update the proxy settings accordingly.

Add Free SSH with Certbot

  1. sudo apt install snapd
  2. sudo snap install --classic certbot
  3. Run certbot in your terminal:
  4. You will see a list of hosted websites. Select the desired site and generate an R3 SSL certificate.