Home

Hosting

Hetzner server with Ubuntu and Namecheap domain

Hetzner Server Configuration

- Create new project and assign a name, e.g. "Webserver"
- Open project and create a new server
- Choose nearest location
- Choose image, e.g. Ubuntu 22.04
- Choose server type, e.g. shared-cpu cx22
- Paste ssh public key to connect without password
- Assign a server name, e.g. "ubuntu-4gb"

Connect to the server via terminal

ssh root@<ip-address>

Create the app directory

mkdir -p /var/www/app

For multiple apps

mkdir /var/www/app/<domain-name> // e.g. "my-app.me-online.de"

Install node on the server

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

sudo apt install -y nodejs // -y flag for confirming all promts with “yes”

Upload an app to the server

scp -r ./<project> root@<server-ip>:/var/www/app

Or clone repository (ssh key needs to be added before)

apt install git -y

git clone <repo-url> . // clone into the current directory

Install PM2 for background server process

npm install -g pm2

Start the app

pm2 start npm --name <your-app-name> -- run start // start if there is a start script in the package.json

More pm2 options

# Show running processes
pm2 list

# Stop process
pm2 stop < id > # id or namespace not pid!

# Remove from list
pm2 delete < id > # id or namespace not pid!

# Automatically restart app after server reboot
pm2 save # saves the current list of running processes to a dump file
pm2 startup # copy-paste the line it shows you

# Check logs
pm2 logs < id > # id or namespace not pid!

# Restart App
pm2 restart < id > # id or namespace not pid!

Namecheap domain configuration

- Domain List > Manage
- Assure that "Namecheap BasicDNS" is selected under "Nameservers"
- Under "Advanced DNS" add a new record (for main domain, without www) with:
  Type: A, Host: @, Value IPv4: <server-ip>, TTL: automatic
- Add a second record (for www domain) with:
  Type: A, Host: www, Value IPv4: <server-ip>, TTL: automatic

It may take a few minutes until the domain is ready, check with:
ping <domain-name>

Nginx webserver creation

sudo apt update && sudo apt install nginx -y

Check if the server is running

sudo systemctl status nginx // or type the server-ip into the browser url

Create an nginx configuration file

sudo nano /etc/nginx/sites-available/app

Add the following content:

server {
    listen 80;
    server_name _;

    location / {
        proxy_pass http://localhost:3000; # port the app is running on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Link the config and reload server

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/

sudo nginx -t // test if config is valid

sudo systemctl reload nginx // reload server

App should now be accessible under:

http://<domain>

or without domain configuration:

http://<server-ip>

If nginx default page blocks other pages, remove the default page

sudo rm /etc/nginx/sites-enabled/default

Configure https with Certbot (Let’s Encrypt)

Prerequesites:

  1. A domain is already available
  2. The domain points to the server-ip
  3. nginx is running

Installation

sudo apt update

sudo apt install certbot python3-certbot-nginx -y

Adapt nginx configuration for server_name

sudo nano /etc/nginx/sites-available/app

# Replace "server\_name \_;" with domain name
server_name my-app.de www.my-app.de;

Test and reload nginx

sudo nginx -t

sudo systemctl reload nginx

Get the ssl certificate

sudo certbot --nginx -d my-app.de -d www.my-app.de

# if asked enter valid email
# agree to the terms of service
# optional share email with Electronic Frontier Foundation if asked

Connecting via sftp (with WinSCP)

Connection with ssh key

- WinSCP requires a .ppk key file (PuTTY format)

Convert OpenSSH key to PPK

1. Open PuTTYgen (included in WinSCP) - Anmeldung > Werkzeuge > PuTTYgen ausführen

2. Load > open SSH Private Key, (e.g. id_rsa)

3. Save Private Key (save as .ppk)

Configure connection mask

- Übertragungsprotokoll: sftp
- Serveradresse: <ip from server or domain>
- Portnummer: 22
- Benutzername: root
- Password: <leave empty>

# Include previous generated ppk key
- Erweitert > Erweitert > SSH > Authentifizierung
- Datei mit privatem Schlüssel > select .ppk file