Deploy FastAPI and React on Hostinger VPS with Namecheap DNS

by Fahim Mahmud Chisti

To deploy FastAPI and React on a Hostinger VPS, you run the API under systemd and build the React app into static files. Nginx serves those files and forwards /api requests to the backend. Namecheap DNS points your domain at the server, Let’s Encrypt adds HTTPS, and Nginx sends cache headers a CDN can use.

Quick answer: Run FastAPI on 127.0.0.1:8000 as a systemd service, build React with npm run build, and use one Nginx server block that serves dist at / and proxies /api/ to port 8000. Point your Namecheap A record at the VPS and run Certbot for HTTPS.

  1. Create a non-root deploy user and open only SSH, 80 and 443 in UFW.
  2. Install Python with venv, Node.js LTS and Nginx.
  3. Install FastAPI, Gunicorn and the Uvicorn worker in a virtualenv, then add a systemd unit.
  4. Build the React app so the files land in frontend/dist.
  5. Add the Nginx server block, point DNS at the VPS, and run certbot --nginx.
  6. Add long cache headers for /assets/ and no-cache for index.html.
In this guide
  1. Before You Start
  2. Step 1: Create a Deploy User and Lock Down the VPS
  3. Step 2: Install Python, Node.js, and Nginx
  4. Step 3: Set Up the FastAPI Backend as a systemd Service
  5. Step 4: Build the React Frontend
  6. Step 5: Point Namecheap DNS to Your VPS
  7. Step 6: Set Up the Nginx Reverse Proxy and SSL
  8. Step 7: Configure CDN Caching and Static Headers
  9. Step 8: Common Gotchas and Troubleshooting
  10. Frequently Asked Questions
  11. Next Steps
Deploy FastAPI and React on Hostinger VPS with Namecheap DNS

Before You Start

  • A Hostinger VPS with a fresh Ubuntu 22.04 or 24.04 image and its root login, ideally with an SSH key added when you created it.
  • A domain on Namecheap and access to its Advanced DNS tab.
  • Your FastAPI and React code in a Git repository, or the small test API in this guide.
  • About an hour, plus DNS propagation. Step 6 removes Nginx’s default site, so on a server that already hosts sites, back up /etc/nginx first.

Step 1: Create a Deploy User and Lock Down the VPS

Spin up a fresh Ubuntu 22.04 or 24.04 VPS instance on Hostinger and grab its public IP from hPanel. From your local terminal, SSH in as root, update your packages, and create a non-root user straight away so you’re not running builds as root:

apt update && apt upgrade -y
adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

The rsync line copies root’s SSH key to the new user. It only works if you added a key when you created the VPS; otherwise run ssh-copy-id deploy@YOUR_SERVER_IP from your own computer later.

Still as root, lock down UFW so only SSH, HTTP, and HTTPS traffic can reach the box. Never leave raw backend ports like 8000 exposed to the public internet.

ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

(The Nginx Full UFW profile doesn’t exist until Nginx is installed in the next step, hence the port numbers.) ufw status should list OpenSSH, 80 and 443 as allowed.

Switch over to the new deploy user before installing runtimes:

su - deploy

Step 2: Install Python, Node.js, and Nginx

The backend needs Python 3.10+ with venv (Python’s built-in virtual environments), while the React frontend needs Node.js to compile your production assets. This pulls Python and Nginx from Ubuntu’s repos and Node 24 LTS from NodeSource (Node.js 20 is end-of-life). Read the downloaded NodeSource script before running it as root.

sudo apt install -y python3-pip python3-venv nginx git curl
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs

Check that both runtimes installed cleanly:

python3 --version
node -v
npm -v

Each command should print a version number: Python 3.10 or newer, and Node v24. Deploying a Node.js backend instead of FastAPI? The same server layout with PM2 is in the guide to deploy Node.js on a Hostinger VPS with Nginx.

Step 3: Set Up the FastAPI Backend as a systemd Service

Keep your codebase organized under /var/www/app. Create the directories and hand ownership to deploy:

sudo mkdir -p /var/www/app
sudo chown -R deploy:deploy /var/www/app
cd /var/www/app
mkdir backend frontend

If your code is on GitHub, clone the backend and frontend into these two folders; you will need an SSH key for GitHub on the VPS. Then go into /var/www/app/backend, create a virtual environment, and install your dependencies:

cd /var/www/app/backend
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install fastapi "uvicorn[standard]" pydantic gunicorn uvicorn-worker

To test the setup before using your own code, save this as /var/www/app/backend/main.py, replacing example.com with your domain:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Production API")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://example.com", "https://www.example.com"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
@app.get("/api/health")
def health_check():
    return {"status": "healthy", "environment": "production"}
@app.get("/api/data")
def get_sample_data():
    return {"items": ["PostgreSQL", "Redis", "Nginx", "FastAPI"]}

Test run it on localhost port 8000, with the virtualenv still active:

uvicorn main:app --host 127.0.0.1 --port 8000

In a second SSH session, curl http://127.0.0.1:8000/api/health should return {"status":"healthy","environment":"production"}. Stop the test with Ctrl+C once it works. Next, write a systemd unit file so Gunicorn restarts automatically on crashes or reboots:

[Unit]
Description=FastAPI Uvicorn Production Daemon
After=network.target
[Service]
User=deploy
Group=deploy
WorkingDirectory=/var/www/app/backend
Environment="PATH=/var/www/app/backend/venv/bin"
ExecStart=/var/www/app/backend/venv/bin/gunicorn main:app -w 4 -k uvicorn_worker.UvicornWorker -b 127.0.0.1:8000
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target

The worker class comes from the uvicorn-worker package installed above; the older uvicorn.workers.UvicornWorker path is deprecated in Uvicorn. Save that file to /etc/systemd/system/fastapi.service (for example with sudo nano), reload systemd, and start the service:

sudo systemctl daemon-reload
sudo systemctl start fastapi
sudo systemctl enable fastapi
sudo systemctl status fastapi

The status output should show active (running). Press q to leave it.

Step 4: Build the React Frontend

Go to your frontend directory and put your React project there, for example a Vite + React app cloned from your repository:

cd /var/www/app/frontend

Make sure your frontend API calls use relative paths (like fetch(‘/api/data’)) instead of hardcoded http://localhost:8000 URLs. Nginx routes them to the backend for you.

Build the static bundle into the dist folder:

npm install
npm run build

Your static files land in /var/www/app/frontend/dist, and the build should finish without errors and list the files it wrote. Make sure Nginx’s worker user can read the files:

sudo chmod -R 755 /var/www/app/frontend/dist

Step 5: Point Namecheap DNS to Your VPS

Head over to your Namecheap Dashboard, click Manage next to your domain, and open the Advanced DNS tab. Remove any parking records, but leave MX and TXT records for email in place.

  • Add an A Record: Host @, Value = your Hostinger VPS IP address, TTL = Automatic.
  • Add a CNAME Record: Host www, Value = your root domain (e.g., example.com.).

From your own computer, dig +short example.com should return the VPS IP before you request a certificate in the next step.

Step 6: Set Up the Nginx Reverse Proxy and SSL

Nginx has two jobs here: serving the static React files for normal requests (/) and forwarding any /api/ calls to Gunicorn on 127.0.0.1:8000.

Create your site config at /etc/nginx/sites-available/app.conf:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/app/frontend/dist;
    index index.html;
    location / {
        try_files $uri $uri/ /index.html;
    }
    location /api/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        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;
    }
}

Symlink it to sites-enabled, remove the default site, test the syntax, and restart Nginx:

sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx

nginx -t should report that the syntax is ok. Open http://YOUR_SERVER_IP in a browser to see the React app, and http://YOUR_SERVER_IP/api/health to see the JSON from FastAPI.

Once DNS points at the server, get a free SSL certificate from Let’s Encrypt with Certbot:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Certbot adds an HTTPS server block and a redirect, and https://example.com should load with a padlock. If Certbot fails on domain challenge verification, read through our guide to fix Certbot ACME HTTP-01 challenge failed errors on Nginx.

Step 7: Configure CDN Caching and Static Headers

When you put a CDN in front of your React build, you want long cache headers on hashed assets in /assets/ and a revalidate-every-time policy on index.html, so users get new releases immediately.

Add these two blocks inside the server block of /etc/nginx/sites-available/app.conf that listens on port 443 (Certbot created it), then run sudo nginx -t && sudo systemctl reload nginx:

location /assets/ {
    add_header Cache-Control "public, max-age=31536000, immutable";
    access_log off;
}
location = /index.html {
    add_header Cache-Control "no-cache";
}

curl -I https://example.com/ should now show Cache-Control: no-cache, and any file under /assets/ should show the one-year immutable header. The one-year rule is only safe because Vite puts a content hash in every filename under /assets/; the guide to immutable Cache-Control and cache busting in Nginx explains why.

Step 8: Common Gotchas and Troubleshooting

Here are the errors that catch most people during this deployment:

  • 502 Bad Gateway: Nginx can’t reach port 8000. Check the service status with sudo systemctl status fastapi, or tail the crash logs via journalctl -u fastapi -n 50 --no-pager.
  • CORS errors in browser console: Double-check that your FastAPI allow_origins array matches your exact domain, including https://.
  • 404 on page refresh in React: You forgot try_files $uri $uri/ /index.html; in your Nginx root location block. Without it, Nginx looks for a real directory instead of letting React Router handle the route.
  • 403 Forbidden on the React site: Nginx cannot read dist, or the build has not run yet. Re-run the chmod from Step 4 and check that dist/index.html exists.

Frequently Asked Questions

Why use Gunicorn with Uvicorn workers instead of plain Uvicorn?

Either works. Recent Uvicorn releases can supervise several workers on their own (uvicorn main:app --workers 4), so plain Uvicorn is a valid choice. Gunicorn with the Uvicorn worker class adds a mature process manager: graceful reloads on HUP, worker timeouts, and worker recycling.

How do I deploy updates to the React frontend?

Pull your latest code into /var/www/app/frontend and run npm run build. Nginx reads straight from dist, so changes go live as soon as the build finishes.

How do I deploy backend updates without downtime?

Pull your new backend code, activate the virtualenv to install new dependencies and run any database migrations, and reload the service with sudo systemctl reload fastapi. Gunicorn then starts fresh workers with the new code while the old ones finish their in-flight requests.

Next Steps

Your FastAPI API and React app now run on one VPS behind Nginx with HTTPS. Reboot the server once and check that sudo systemctl status fastapi shows it running again, then put your CDN in front of the domain and confirm that files under /assets/ are served from the CDN cache.

all_in_one_marketing_tool