DevOps & Deployment
devopsdeploymentRecipeBeginner
Deploy React (Vite SPA) on Ubuntu VPS with Nginx & SSL
Deploy a production React Single Page Application (Vite) on Ubuntu VPS with Nginx SPA fallback routing (try_files), Gzip compression, and SSL.
Verified: 2026-10-01
Tested on:react 19.x, vite 6.x, nginx 1.26
Platforms:
linuxall
#react#vite#vps#nginx#spa#deployment
Goal
Host a fast, optimized React Single Page Application (SPA) built with Vite on an Ubuntu VPS served statically through Nginx with client-side routing fallback and Let's Encrypt SSL.
Prerequisites
- Ubuntu VPS with Nginx and Git installed
- Domain name pointed to the VPS (e.g.
react.example.com)
```bash
# Install dependencies and build
npm run build
# Outputs static HTML, CSS, and JS bundles to ./dist/
```# Set ownership
sudo chown -R $USER:$USER /var/www/react-app
# Copy files from dist into /var/www/react-app
cp -r dist/* /var/www/react-app/
``````nginx
server {
listen 80;
listen [::]:80;
server_name react.example.com;
root /var/www/react-app;
index index.html;
# Essential: Fallback all client-side routes (e.g. /dashboard, /profile) to index.html
location / {
try_files $uri $uri/ /index.html;
}
# Cache hashed JS/CSS assets aggressively
location ~* \.(?:css|js|woff2?|svg|png|jpg|webp)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# Do NOT cache index.html so users always receive fresh asset hashes
location = /index.html {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate";
}
}
```
Enable and test:
```bash
sudo ln -s /etc/nginx/sites-available/react.example.com.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
```Verification
- Open
https://react.example.comin your browser. - Navigate to subroutes (e.g.
/dashboard) and hit Browser Refresh (Cmd+R/F5) to verifytry_filesprevents 404 errors.
Deploy Next.js App on Ubuntu VPS (Standalone + PM2 + Nginx + SSL)
Step-by-step production deployment guide for Next.js App Router on an Ubuntu VPS using standalone output, PM2 cluster mode, Nginx reverse proxy, and SSL.
Deploy Node.js & Express API on Ubuntu VPS (PM2 + Nginx + SSL)
Deploy a Node.js Express/NestJS REST API server on Ubuntu VPS with PM2 cluster mode, automatic reboot recovery, Nginx reverse proxy, and SSL.