How to Host/Deploy ReactJs Apps on cPanel
Estimated reading time: 1 min
Hosting a React.js app on cPanel involves building the app and configuring it to serve static files. Follow these steps:
1. Build Your React App
Run the following command in your React project directory to generate the production build:
This will create a build
folder containing the static files.
2. Upload the Build Folder to cPanel
- Log in to your cPanel.
- Go to File Manager.
- Navigate to the public_html directory (or create a subfolder if needed).
- Upload the contents of the
build
folder (not the folder itself) topublic_html
.
3. Set Up .htaccess (Fix Routing Issues)
Create a .htaccess
file in public_html
and add the following content:
This ensures that React’s client-side routing works correctly.
4. Configure the cPanel Settings
- Go to Domains > Addon Domains (if hosting in a subdomain).
- Set the document root to
public_html
(or the subfolder where you uploaded the files).
5. Verify and Test
- Visit your domain (e.g.,
yourdomain.com
). - If there are issues, check File Manager to confirm files are correctly placed.
Bonus: Set Up a Node.js Server (Optional)
If your React app needs a backend with Express.js:
- Use cPanel’s Node.js App feature.
- Deploy your backend in a separate directory.