Setup server from scratch to production, Node - atabegruslan/Notes GitHub Wiki
Setup Node server from scratch to production
- Key tutorial https://www.youtube.com/playlist?list=PLdHg5T0SNpN38gy5xZ0PVEaDdZXlPkgP9 (Very Good)
Setup Server
Get Server
Digital Ocean Droplet is used
Centos is used
Generate and add your local SSH public key into server's authorized_keys
file
To clear up any confusion between authorized_keys
and known_hosts
, imagine this setup:
Your localhost -> your server -> Github
You add your localhost's SSH key into your server's authorized_keys
, so that it will allows you to SSH in.
When you give your server's SSH key to Github, in order to pull from Github via SSH, then an entry got Github will be added into your server's known_hosts
. https://stackoverflow.com/questions/33243393/what-is-actually-in-known-hosts
Difference between SSH vs Deploy keys: https://stackoverflow.com/questions/39659302/difference-between-account-ssh-key-vs-deployment-ssh-key/39659393#39659393
dnf
is Centos's package manager
dnf update
dnf instal curl vim git wget '@Development tools' nmap net-tools epel-release
epel-release is extra packages for enterprise linux
Install snapcraft
(another package manager) easier to install Node and Certbot with this
dnf install snapd
dnf install dnf-automatic # this installs OS updates
systemctl enable --now dnf-automatic.timer # enables auto OS updates
Users
SSH in
ssh root@{ip-address}
Create new user
adduser {username} # Create new user
usermod -aG wheel {username} # Add that user to group 'wheel'
Some theory
By default, centos have a user called centos
, and a root
user.
By default, centos have a sudo-access group called wheel
Look into /etc/sudoers
:
Look into /etc/sudoers/90-cloud-init-users
: Already exists a user 'centos', represented in this file, in which 'centos' is specified.
What is visudo
: https://www.unixtutorial.org/commands/visudo
visudo
: safely updating the/etc/sudoers
file
Give new user sudo permissions
Create a new file, for the new user: visudo /etc/sudoers/{username}
.
Then give it sudo priviledge and password-less SSH entry. Recall:
visudo -cf /etc/sudoers # to verify the file you just created
Setup SSH for new user
Create directory and file: /home/{username}/.ssh/authorized_keys
.ssh
folder should have permissions 700
authorized_keys
file should have permissions 600
Append your local SSH public key into server's {username}'s authorized_keys
file
Delete old default user 'centos'
userdel -r centos
root
Remove password-less SSH entry from vim sshd_config
Set PasswordAuthentication
from yes
to no
Set PermitRootLogin
from yes
to no
Change terminal
Up to now, the terminal you are using is zsh
, fishshell
is better.
sudo dnf install fish
Install Node.js
sudo snap install node --classic --channel=14
PM2 Example
https://gist.github.com/atabegruslan/10c8a71a184adc0091e153b5cd7446bd
DNS
In this tutorial, GoDaddy is used.
Need to A and CNAME records.
What are proxies
- https://www.youtube.com/watch?v=4NB0NDtOwIQ
- https://www.pomerium.com/blog/proxy-vs-reverse-proxy
- https://www.youtube.com/watch?v=ozhe__GdWC8
- https://www.marktechpost.com/2023/08/31/best-proxy-servers-2023
Reverse Proxy
Tutorial's Nginx reverse-proxy setup
Another example of a simple Nginx reverse-proxy setup
https://phoenixnap.com/kb/nginx-reverse-proxy
Other tutorials:
- https://kinsta.com/blog/reverse-proxy
- https://www.freecodecamp.org/news/docker-nginx-letsencrypt-easy-secure-reverse-proxy-40165ba3aee2
- https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Docker-Nginx-reverse-proxy-setup-example
- https://www.highgo.ca/2023/06/06/using-nginx-as-a-postgresql-reverse-proxy-and-load-balancer
Load Balancing
https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer
For reference - Reverse proxy & Load balancing in HAProxy
- https://webhostinggeeks.com/howto/how-to-configure-and-use-haproxy-as-a-reverse-proxy
- https://www.haproxy.com/blog/layer-4-and-layer-7-proxy-mode
- https://www.youtube.com/watch?v=aKMLgFVxZYk&list=PLpXfHEl2fzl6A8U5X0amZiYewxNQ01ERK&index=2
Load balancing methods
- https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#choosing-a-load-balancing-method
- https://youtu.be/dBmxNsS3BGE?si=ASSdIDtdsj2yf9sw
Load balancer uses
Load balancer choices
- https://www.g2.com/categories/load-balancing
- https://www.nomios.com/news-blog/best-load-balancers-2024
- https://www.softwaretestinghelp.com/sofware-load-balancers
- https://logz.io/blog/best-open-source-load-balancers
- https://www.baeldung.com/zuul-load-balancing
- https://www.designgurus.io/course-play/grokking-system-design-fundamentals/doc/641da6bad2bb4146954f1130
- https://www.howtoforge.com/how-to-set-up-load-balancing-mysql-server-with-proxysql-on-debian-11
- https://www.techgeeknext.com/spring-cloud/eureka-load-balancer
- https://www.eginnovations.com/documentation/F5-BIG-IP-Load-Balancers/Introduction-to-F5-BIG-IP-Load-Balancer-Monitoring.htm#:~:text=An%20F5%20BIG%2DIP%20load,servers%20in%20the%20server%20pools.
SSL certificate
Certbot is used in this tutorial
Update server whenever Github updates
In this tutorial, this Webhook Server is used: https://github.com/adnanh/webhook
Github gets a new commit -> Github's Webhook calls server's Webhook Server -> Webhook Server's hooks.json
-> redeploy.sh
pulls in the new code from Github.
Firewall
firewalld
is used in this tutorial
Fail2Ban
If someone fails to eg login often enough, then he'll be banned for a set amount of time.
Deployment strategies
API Gateways
Uses:
- Authentication / authorization
- SSL cert rotation (obtaining new cert every N days) / SSL termination
- DDoS protection
- Throttling
- Routing
- Security policy enforcement
- Static content
- Something like auto-sending you discounts, sales and offers
- Cache
- Load Balancing
- Circuit breaking
- A/B Testing
- Protocol tranlation
- Service discovery
- Admin dashboard
- Monitoring, logging, analytics, billing