291 words
1 minute
Server Setup - From Zero to Production

Server Setup - From Zero to Production#

Setting up a server for the first time can seem scary, but it’s actually pretty straightforward.

Pick a VPS Provider#

ProviderStarting PriceWhy I Like It
DigitalOcean$4/monthEasy, great tutorials
Vultr$2.50/monthCheap, many locations
Linode$5/monthGood performance
Hetzner€3.79/monthSuper cheap, EU

Step 1: First Things First#

Connect to Your Server#

Terminal window
ssh root@your-server-ip

Update Everything#

Terminal window
apt update && apt upgrade -y

Create a User (Don’t Use Root!)#

Terminal window
adduser lukkid
usermod -aG sudo lukkid
su - lukkid

Step 2: Lock It Down#

SSH Keys (Way Safer Than Passwords)#

Terminal window
# On your local machine
ssh-keygen -t ed25519 -C "lukkid@server"
ssh-copy-id lukkid@your-server-ip

Disable Password Login#

Terminal window
sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin no
Terminal window
sudo systemctl restart sshd

Firewall Setup#

Terminal window
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

Step 3: Install the Good Stuff#

Node.js#

Terminal window
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts

Python#

Terminal window
sudo apt install python3 python3-pip python3-venv

Nginx#

Terminal window
sudo apt install nginx
sudo systemctl enable nginx

Step 4: Deploy Your App#

Get Your Code#

Terminal window
git clone https://github.com/0x90Vold/my-project.git
cd my-project
npm install
npm run build

Keep It Running with PM2#

Terminal window
npm install -g pm2
pm2 start npm --name "my-app" -- start
pm2 save
pm2 startup

Step 5: Nginx Config#

Terminal window
sudo nano /etc/nginx/sites-available/my-app
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
}
Terminal window
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 6: Free SSL with Certbot#

Terminal window
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com

Done! Free HTTPS forever.

Useful Commands to Remember#

Check Logs#

Terminal window
sudo tail -f /var/log/nginx/error.log
pm2 logs
journalctl -f

Monitor Stuff#

Terminal window
df -h # disk space
free -h # memory
htop # processes
pm2 monit # your apps

Update Your App#

Terminal window
cd ~/my-project
git pull
npm install
npm run build
pm2 restart my-app

My Server Checklist#

  • Create non-root user
  • Setup SSH keys
  • Disable password auth
  • Enable firewall
  • Install Node/Python
  • Setup Nginx
  • Get SSL certificate
  • Setup PM2

From Termux on my phone to managing real production servers.

You can do it too!

Server Setup - From Zero to Production
https://blog.lukkid.dev/posts/server-setup/
Author
LUKKID
Published at
2024-05-25
License
CC BY-NC-SA 4.0