How to deploy your React.js app Print

  • 4

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

  1. In cPanel File Manager, create a folder: /home/yourusername/public_html/myapp/server
  2. Go to Setup Node.js App → Create Application
  3. Choose the appropriate Node.js version.
  4. Application root: /home/yourusername/public_html/myapp/server
  5. Application URL: your live domain (e.g., https://myapp.com)
  6. Application startup file: app.js
  7. Set a log path (e.g., /home/yourusername/logs/passenger.log)
  8. Click Create.
  9. Open Terminal → enter the virtual environment for this app (use the source command shown).
  10. Run:
    npm install express path
  11. 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);

  1. Switch the app to Production mode and restart it.

Step 2: Build your React project on the server

  1. Upload your full React project (with package.json, src folder, etc.) to /home/yourusername/public_html/myapp
  2. In package.json, set "homepage": "/" (or "/myapp" if deploying to a subfolder).
  3. 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
Click Create. Open Terminal → activate this second app’s virtual environment. Run:
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')));


Was this answer helpful?

« Back