How do I deploy a web app to a Linux server?
Basic deployment steps
Start with a fresh Linux server (e.g., Ubuntu 22.04). Update packages, create a non-root user, and configure SSH keys. Install the runtime your app needs: for a Node.js app, install Node.js via NodeSource or nvm; for Python, use `apt install python3-pip` and a virtual environment.
Copy your application code to the server, typically via `git clone` or `rsync`. Install dependencies (e.g., `npm install` or `pip install -r requirements.txt`). Build the app if needed (`npm run build`).
Set up a process manager to keep your app running. For Node.js, PM2 is popular (`pm2 start app.js`). For any app, you can create a systemd service file. Finally, install Nginx and configure it as a reverse proxy to forward requests to your app's port.
- Provision server and secure it
- Install runtime and dependencies
- Copy code and build
- Start app with systemd or PM2
- Configure Nginx reverse proxy
- Set up SSL with Let's Encrypt
Example Nginx config
A minimal Nginx server block for a Node.js app on port 3000 looks like: `server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; } }`. Save it in `/etc/nginx/sites-available/` and symlink to `sites-enabled`.
Test with `sudo nginx -t` and reload with `sudo systemctl reload nginx`. For HTTPS, use Certbot to obtain a free Let's Encrypt certificate.
Common mistakes
- Running the app as root; always use a dedicated non-root user.
- Forgetting to open firewall ports (80 and 443) for web traffic.
- Not setting up a process manager, so the app stops when the SSH session ends.
