Remote servers & SSH
Real servers live in a data centre or the cloud, so you manage them remotely. This chapter follows what the Traversy "Linux Server Setup" video does step by step: create a server, log in securely with keys, lock it down, and put a web app behind Nginx with HTTPS.
How SSH works
SSH (Secure Shell) gives you a terminal on another machine over an encrypted connection. The server runs an SSH daemon (sshd) listening on port 22; your laptop runs the client, ssh. Everything you type and everything the server prints travels encrypted, so nobody in between can read it.
$ ssh [email protected] # user@server-address
$ ssh -i ~/.ssh/work_key demee@server # use a specific key
$ exit # leave the remote shell
Key pairs instead of passwords
Passwords can be guessed, and servers on the internet get thousands of guessing attempts a day. SSH keys fix that. ssh-keygen creates a pair of matching keys:
- the private key (
~/.ssh/id_ed25519) stays on your laptop and is never shared; - the public key (
~/.ssh/id_ed25519.pub) is copied to every server you want to log in to, into~/.ssh/authorized_keys.
When you connect, the server sends a challenge that only the matching private key can answer. Your private key never leaves your machine, so there is nothing to guess or steal from the network.
$ ssh-keygen -t ed25519 -C "demee laptop" # make the pair (Enter for defaults)
$ ssh-copy-id [email protected] # install the public key on the server
$ ssh [email protected] # now logs in without a password
GitHub uses exactly the same idea: you paste your public key into GitHub's settings so git push can authenticate you.
First things to do on a new server
- Update it:
sudo apt update && sudo apt upgrade. - Create a normal user and give it sudo, so you stop working as root:
adduser demee,usermod -aG sudo demee, then copy your public key into that user's~/.ssh/authorized_keys. - Turn off password logins and root login in
/etc/ssh/sshd_config: setPasswordAuthentication noandPermitRootLogin no, thensudo systemctl restart ssh. Test a new key login in a second terminal before closing the first one, so you can't lock yourself out. - Turn on a firewall that only lets in what you need:
$ sudo ufw allow OpenSSH # keep SSH open first!
$ sudo ufw allow 80 # HTTP
$ sudo ufw allow 443 # HTTPS
$ sudo ufw enable
$ sudo ufw status
Copying files to and from servers
$ scp app.tar.gz demee@server:/srv/ # copy up
$ scp demee@server:/var/log/app.log . # copy down
$ rsync -avz site/ demee@server:/var/www/ # sync a folder, only sending changes
Serving an app: Nginx as a reverse proxy
Your app (say a Node server on port 5000) shouldn't face the internet directly. Instead Nginx listens on ports 80/443 and forwards requests to the app. That is a reverse proxy: visitors only ever talk to Nginx, which also handles HTTPS, compression and serving static files efficiently.
# /etc/nginx/sites-available/default
server {
listen 80;
server_name demee.cc;
location / {
proxy_pass http://localhost:5000; # hand the request to the app
}
}
$ sudo nginx -t # check the config for typos
$ sudo systemctl reload nginx # apply it
Finally point your domain's DNS A record at the server's IP, and get a free HTTPS certificate from Let's Encrypt with sudo certbot --nginx -d demee.cc. Certbot edits the Nginx config for you and renews the certificate automatically.