skip to content

How I Set Up My Ubuntu Server for Apps

Published: • Updated:
How I Set Up My Ubuntu Server for Apps

Your app works locally. Now let’s put it online.

Install Nginx

Nginx sits in front of your app and handles web traffic.

Terminal window
sudo apt install nginx -y
sudo systemctl enable nginx

Set Up Firewall

Only open what you need:

Terminal window
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Get Free SSL

For that lock icon in browsers:

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

It renews itself. Nice.

Connect Nginx to Your App

Create /etc/nginx/sites-available/myapp:

server {
listen 443 ssl;
server_name yoursite.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
}

Enable it:

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

Keep Your App Running Forever

Without this, your app dies when server restarts.

Create /etc/systemd/system/myapp.service:

[Unit]
Description=My App
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure
[Install]
WantedBy=multi-user.target

Start it:

Terminal window
sudo systemctl enable myapp
sudo systemctl start myapp

When Things Break

Terminal window
sudo systemctl status myapp
sudo journalctl -u myapp -n 30

Done

Nginx handles traffic, systemd keeps app alive, firewall blocks bad stuff, SSL keeps it safe. That’s it.