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).
Reference
Core Concepts
Terms you will use in this deploy:
Concept
Meaning
create-react-app
Classic scaffold that outputs a build/ folder
npm run build
Production compile of the React app into static files
express.static
Serves JS/CSS/assets from the build directory
GET * fallback
Returns index.html for client-side routes on refresh
PM2
Keeps the Express process running after SSH disconnect
PORT 3000
Sample listen port; must match the security group rule
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.
Remember
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.