Deploy a React App on EC2

Beginner
13 min read
Updated: Nov 2026
5 examples
React / Express / PM2

Definition and Usage

This guide hosts a React production build on Amazon EC2: build static files locally or on the server, serve the build/ folder with a small Express app, and keep it running under PM2.

What is a React production deploy on EC2?

After npm run build, Create React App outputs HTML, JS, and CSS under build/. Express (or Nginx) serves those files. A catch-all route returns index.html so client-side routes still work on refresh.

When should you use this approach?

Use it for classic SPAs on a single Ubuntu VM. For SSR, routing, and API routes in one framework, prefer Next.js on EC2. For a plain API without a React UI, see Express.js on EC2.

What you will build

Scaffold with Create React App, add root index.js, upload with FileZilla (no node_modules), then npm install, npm run build, and pm2 start index.js on port 3000.

Create React App vs Vite

CRA is in maintenance mode; new apps often use Vite (dist/ instead of build/). The Express pattern is the same—point express.static at your output folder.

Static files + SPA fallback.

Serve assets from build/, then send index.html for unknown paths so React Router can handle the URL.

CRA is maintenance-mode.

These steps still match classic build/ output. For greenfield projects, consider Vite or a meta-framework such as Next.js.

Match the security group to the port.

The sample uses 3000. Open inbound TCP 3000 (or change PORT and the group together).

Core Concepts

Terms you will use in this deploy:

ConceptMeaning
create-react-appClassic scaffold that outputs a build/ folder
npm run buildProduction compile of the React app into static files
express.staticServes JS/CSS/assets from the build directory
GET * fallbackReturns index.html for client-side routes on refresh
PM2Keeps the Express process running after SSH disconnect
PORT 3000Sample listen port; must match the security group rule
node_modulesInstall on EC2; do not upload from your laptop

Prerequisites

Have these ready before you deploy:

Examples

Scaffold React, add Express to serve build/, upload to EC2, build, start with PM2, and open the public URL.

Example 1 — Create the React App Locally

Scaffold with Create React App and confirm the template runs.

local.sh
npx create-react-app@latest my-react-app
cd my-react-app
npm start

The app opens at http://localhost:3000/. Edit the UI under src/ (commonly src/App.js or src/App.jsx).

Default Create React App running on localhost
Browser

How It Works

  1. create-react-app generates a React project with start and build scripts.
  2. npm start is the local dev server on port 3000.
  3. Customize src/ first so the later EC2 deploy shows your UI.
  4. Production uses npm run build, not the CRA dev server, on EC2.

Example 2 — Add Express to Serve the Build Folder

In the project root (next to package.json), add index.js before you upload.

index.js
const express = require('express');
const path = require('path');

const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(path.join(__dirname, 'build')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

How It Works

  1. express.static('build') serves hashed JS/CSS and other assets.
  2. GET * returns index.html when no static file matches (SPA routing).
  3. Port defaults to 3000; override with the PORT env var if needed.
  4. Vite users: swap build for dist after a Vite production build.

Example 3 — Prepare EC2 and Upload (No node_modules)

Create a folder on the instance and SFTP the project without heavy folders.

  1. Confirm the instance is running; note the public DNS or Elastic IP.
  2. SSH with PuTTY or ssh; verify Node with node -v / npm -v.
  3. Upload with FileZilla. Exclude .git, node_modules, and usually build.
ec2.sh
sudo mkdir -p /home/ubuntu/reactjs-website
sudo chown ubuntu:ubuntu /home/ubuntu/reactjs-website

# Demo only � tighten after upload
sudo chmod 777 /home/ubuntu/reactjs-website
EC2 instance running
Amazon EC2
PuTTY session to EC2
PuTTY
Node and npm versions
Ubuntu
Creating reactjs-website directory on EC2
Ubuntu
reactjs-website folder permissions on EC2
Ubuntu
FileZilla uploading React project to EC2
FileZilla

How It Works

  1. Source + lockfile on the wire keeps SFTP fast.
  2. Skipping build forces a clean production compile on EC2.
  3. Include index.js so Express is ready after install.
  4. chmod 777 is demo-only—tighten ownership after upload.

Example 4 — Install, Build, and Start with PM2

Install dependencies on the server, add Express, compile the React app, then start the static server.

ec2.sh
sudo npm i -g pm2

cd /home/ubuntu/reactjs-website
npm install
npm install express
NODE_ENV=production npm run build

sudo pm2 start index.js
sudo pm2 status
PM2 global install
Ubuntu
PM2 starting Express server for React build
Ubuntu

How It Works

  1. npm install restores React tooling and app deps for this Linux host.
  2. npm install express adds the static file server dependency.
  3. npm run build writes production files into build/.
  4. pm2 start index.js serves that folder; open TCP 3000 in the security group.
Prefer chown over root npm.

Use sudo npm install only if ownership is broken. Fix chown and run npm as your deploy user when you can.

Example 5 — Open the App in a Browser

Confirm the public URL serves the production React build.

  1. Browse to http://<public-ip>:3000/ (or your chosen port).
  2. You should see the built React UI.
  3. If you use React Router, refresh a deep link—the Express fallback should still return the app.
React app served from EC2
Browser

How It Works

  1. The browser hits Express on 3000; static assets load from build/.
  2. Unknown paths get index.html so the SPA can route client-side.
  3. PM2 keeps the process up after you close SSH.
  4. Next upgrades: HTTPS, an ALB, CloudFront, S3 static hosting, or Next.js.

Edge Cases & Pitfalls

Most failures are a missing build, wrong port, or a 404 on React Router refresh.

build

Started without npm run build

Express looks for build/. Without a successful build, assets and index.html are missing.

spa

Deep links 404 on refresh

You need the catch-all GET * → index.html route. Serving only static files without the fallback breaks client routes.

port

Security group port mismatch

Opening only port 80 while Node listens on 3000 will time out. Match the inbound rule to PORT.

npm

Uploaded laptop node_modules

Native modules can break across OS/CPU. Delete them and run a fresh npm install on EC2.

vite

Vite output is dist/

If you switched toolchains, update Express paths from build to dist or the server will 404.

order

Catch-all before static middleware

Register express.static first, then GET *. Reversing the order can break asset URLs.

Key Takeaways

  • Build first: npm run build creates static files in build/.
  • Express: serve static assets, then fall back to index.html for SPA routes.
  • PM2 + port: keep the process alive and open the matching security group port.
  • Alternatives: Nginx, S3 + CloudFront, or Next.js when you outgrow a static SPA.

One line: build React to static files, serve with Express + SPA fallback, supervise with PM2.

Frequently Asked Questions

After npm run build, React is a set of static files. Express (or Nginx) can serve the build folder and return index.html for client-side routes. You can also use a dedicated static file server or object storage plus CloudFront.
Open the security group inbound rule for the same port your Node process listens on. The sample uses 3000; if you switch to port 80, allow TCP 80 instead.
Usually no. Upload package.json and the lockfile, then run npm install on the server so native dependencies match the instance.
React Router (and similar) handles paths in the browser. On refresh, the server must still return index.html for unknown paths so the SPA can boot and read the URL.
Yes in spirit. Vite outputs to dist/ by default. Point express.static and sendFile at dist instead of build, then run the Vite production build command.
CRA is in maintenance mode. These steps still match classic build/ output. For new projects, many teams prefer Vite or a meta-framework such as Next.js.

Next: Deploy Next.js on EC2

When you outgrow a static React bundle, Next.js adds routing, SSR, and API routes in one framework.

Next.js on EC2 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful