Skip to content

Instantly share code, notes, and snippets.

@mahkassem
Created June 13, 2022 19:59
Show Gist options
  • Save mahkassem/c7d3617a5ebd30c7303467669ad80cc2 to your computer and use it in GitHub Desktop.
Save mahkassem/c7d3617a5ebd30c7303467669ad80cc2 to your computer and use it in GitHub Desktop.
Linux Server Setup & Deployment

Linux Server Setup & Deployment

Deploy NodeJS application and html website with PostgresSQL and Nginx as a server

Environment

  • OS: Linux20.04 LTS
  • Server: Nginx 1.20
  • NodeJS: nvm 0.39
  • Database: PostgreSQL 12.11

Steps

1. Create rsa private/public key pair πŸ—.

ssh-keygen -t rsa

Obtain public key:

cat ~/.ssh/id_rsa.pub

P.S. This public key to be put in authorized_keys in our remote server

2. Install PostgreSQL

sudo apt update
sudo apt upgrade

Install postgresql & postgresql-contrib packages.

sudo apt install postgresql postgresql-contrib

Start postgres service

sudo systemctl start postgresql.service
Enable remote

Edit the postgresql.conf

listen_addresses = '*'

Edit the pg_hba.conf file and add the following entry at the very end of file:

host    all             all              0.0.0.0/0                       md5
host    all             all              ::/0                            md5

Source

3. Install nvm & node 16

Download & run nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Configure nvm bash path

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Install node 16

nvm install 16

4. Install & configure Nginx

  • Install:
sudo apt install nginx-full
  • Domain Configuration:
cd /etc/nginx/sites-available
nano example.com

Inside that file we will insert the following:

server {
        server_name domain.com www.domain.com;

        root /var/www/website;

        index index.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

5. Configure and enable firewall πŸ”₯

Add inbound rules: Ports: 22, 22/tcp, 2222, 2222/tcp, Nginx Full, 5432, OpenSSH

sudo ufw allow [port/app]
sudo ufw enable
sudo ufw reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment