Hosting

Setting up a VPS for your first web app

Avi, the Avenux Techspire mascot, illustrating: Setting up a VPS for your first web app

The first time you log in to a VPS, all you get is a blinking cursor. No panel, no buttons. That is exactly what makes it a good teacher. Here is the order I follow on a fresh Ubuntu server before putting any app on it. I will use a small Node.js app as the example, but the steps are nearly the same for Python or anything else.

1. Log in and update

Your provider gives you an IP address and a root password or SSH key. From your terminal:

ssh root@203.0.113.10

Then update the package list and installed packages:

apt update && apt upgrade -y

If it asks about restarting services or keeping config files, the default choice is usually fine on a fresh server.

2. Create a normal user

Working as root all the time is risky. One wrong command and there is nothing stopping it. Create your own user and give it sudo rights:

adduser deploy
usermod -aG sudo deploy

From now on you will log in as deploy and use sudo when you need admin rights.

3. Set up SSH keys

Passwords can be guessed. SSH keys are much safer. On your own laptop (not the server), create a key if you do not have one:

ssh-keygen -t ed25519

Then copy it to the server:

ssh-copy-id deploy@203.0.113.10

Open a new terminal and confirm you can log in as deploy without a password. Only after that works, edit the SSH config on the server:

sudo nano /etc/ssh/sshd_config

Set these two lines:

PermitRootLogin no
PasswordAuthentication no

Some Ubuntu images also have files in /etc/ssh/sshd_config.d/ that override these settings, so check there too. Then restart SSH:

sudo systemctl restart ssh

Keep your current session open while you test a new login. If something is wrong, you can still fix it instead of locking yourself out.

4. Turn on the firewall

Ubuntu ships with UFW, a friendly front end for the firewall. Allow SSH first, then enable it:

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

Get the order wrong and you can cut off your own SSH connection, so always allow OpenSSH before enabling.

5. Install your runtime

For Node.js, the version in Ubuntu’s default repository is often behind. Many people install the current LTS from the NodeSource repository or use nvm. Follow the official instructions for whichever you choose, then confirm:

node -v
npm -v

Put your code on the server, usually with git:

cd ~
git clone https://github.com/yourname/your-app.git
cd your-app
npm install --omit=dev

Keep secrets like database passwords and API keys in a .env file on the server. Never commit that file to git.

6. Keep the app running with systemd

If you run node server.js and close the terminal, the app dies. systemd can run it as a service, restart it if it crashes and start it on boot. Create a service file:

sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Node app
After=network.target

[Service]
User=deploy
WorkingDirectory=/home/deploy/your-app
EnvironmentFile=/home/deploy/your-app/.env
ExecStart=/usr/bin/node server.js
Restart=always

[Install]
WantedBy=multi-user.target

Check the path to node with which node, especially if you used nvm, and put that full path in ExecStart. Then:

sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo systemctl status myapp

Logs are available with journalctl -u myapp -f. PM2 is another popular option for Node, and works fine too. systemd has the advantage of already being there.

7. Put Nginx in front

Your app probably listens on a port like 3000. You do not want visitors typing :3000, and you want a proper web server handling HTTPS. Install Nginx and open the web ports:

sudo apt install nginx -y
sudo ufw allow 'Nginx Full'

Create a site config:

sudo nano /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1: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 it, test the config and reload:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Point your domain’s A record at the server IP, and once DNS updates, your app should load on port 80.

8. Add HTTPS

With the domain working, use Certbot to get a free Let’s Encrypt certificate. It edits the Nginx config for you and sets up automatic renewal. I have written a separate post on that, so I will not repeat it here.

What to do next

  • Turn on automatic security updates with the unattended-upgrades package.
  • Set up backups for your database and any uploaded files.
  • Write down every step you did. Next time you set up a server, you will thank yourself.

The first VPS setup might take you an evening. The third one takes half an hour. That is the whole point: once you have done it by hand, tools and platforms stop feeling like magic.