Deploy simple Node.js website or app on VPS (Hetzner)
The article is a work in progress 🚧
1. Generate SSH keys
Generating SSH key is recommended and it will be used to log in to your VPS automatically without password.
- Open Terminal (PowerShell on Windows).
- Type
ssh-keygen -t ed25519and press ENTER. - It will say, “Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519):”, press
ENTER. - When it says
Enter passphrase (empty for no passphrase):press ENTER. - When it says,
Enter same passphrase again:press ENTER. - You will need this file when creating VPS server:
Your public key has been saved in...id_ed25519.pub. - The file named
id_ed25519without.pubis your private key and should be always kept private!
2. Get a VPS server
- Sign up Hetzner account.
- Log in to Hetzner Cloud.
- Click + New Project, give it a name and go to this new project.
- Click Add Server.
- Choose Location (the closest will be default).
- Choose Ubuntu image.
- Choose cheapest Shared vCPU for now.
- In Networking section, keep Public IPv4, but deselect Public IPv6.
- In SSH Keys section upload your public SSH key, the
id_ed25519.pubfile from previous step. - Create volume (e.g. 10GB, least possible).
- Click Create & Buy now to launch your cloud server.
Log in to server
- Copy Public IP, this is your server address.
- Open your command line interface, I use Terminal on Mac.
- Type
ssh root@PUBLIC_IP. Instead of PUBLIC_IP, paste the IP address. - You might see a message saying something similar to:
The authenticity of host '12.524.412.245 (12.524.412.245)' can't be established.
ED25519 key fingerprint is SHA256:ANlLAjSsImPBhBIZA1+R28PcBzA/ufgy67rf8og.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?type yes and press ENTER.
Initial setup
- Update and upgrade system, type
sudo apt update && sudo apt upgrade -y - Install Nginx server type
sudo apt install nginx - Start and enable Nginx, type:
sudo systemctl start nginx && sudo systemctl enable nginx - Check Nginx status:
sudo systemctl status nginx - Allow firewall traffic
sudo ufw allow 'Nginx Full'
Configure domain name
- Create a new configuration file for your domain:
sudo nano /etc/nginx/sites-available/mydomain.com - Add the following configuration
server {
listen 80;
server_name mydomain.com www.mydomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}- Enable the configuration by creating a symlink:
sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/-
Test Nginx for syntax errors:
sudo nginx -t -
If successful, reload Nginx:
sudo systemctl reload nginx
Obtain an SSL (HTTPS) Certificate
To secure your domain with HTTPS, you can use Certbot to get a free SSL certificate:
- Install Certbot:
sudo apt install certbot python3-certbot-nginx - Obtain and configure the SSL certificate for your domain:
sudo certbot --nginx -d mydomain.com -d www.mydomain.com - Restart everything:
sudo systemctl restart nginx