Deploying a React.js application on standard cPanel shared hosting requires building it into static files and serving them through a lightweight Node.js/Express server, since you cannot run the React development server on shared hosting.
Important:
- All files must be placed inside a subfolder under public_html (e.g., /home/yourusername/public_html/myapp).
- You cannot run npm start or use hot-reload on the server – develop locally and deploy the production build only.
Step 1: Create the Express server that will serve your built React app
- In cPanel File Manager, create a folder: /home/yourusername/public_html/myapp/server
- Go to Setup Node.js App → Create Application
- Choose the appropriate Node.js version.
- Application root: /home/yourusername/public_html/myapp/server
- Application URL: your live domain (e.g., https://myapp.com)
- Application startup file: app.js
- Set a log path (e.g., /home/yourusername/logs/passenger.log)
- Click Create.
- Open Terminal → enter the virtual environment for this app (use the source command shown).
- Run:
npm install express path - Create app.js inside the server folder with this code:
javascript
const express = require('express');
const path = require('path');
const app = express();
app.use('/', express.static(path.join(__dirname, '../build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../build', 'index.html'));
});
app.listen(process.env.PORT || 9001);
- Switch the app to Production mode and restart it.
Step 2: Build your React project on the server
- Upload your full React project (with package.json, src folder, etc.) to /home/yourusername/public_html/myapp
- In package.json, set "homepage": "/" (or "/myapp" if deploying to a subfolder).
- In cPanel → Setup Node.js App → Create another application:
- Application root: /home/yourusername/public_html/myapp
- Application URL: anything temporary (e.g., myapp.com/build)
- Startup file: can be left empty
npm install
npm run build
→ This creates the optimized files in /home/yourusername/public_html/myapp/build Restart both Node.js applications.
Your React app is now live at your domain!
If deploying to a subfolder (e.g., myapp.com/reactapp), set "homepage": "/reactapp" in package.json and change the Express line to:
app.use('/reactapp', express.static(path.join(__dirname, '../build')));