React Site hosting with pm2
Hosting a React app with PM2 involves a few steps, including building your React app, serving it with a static server, and managing the server process using PM2. Below is a detailed guide on how to do it:
Prerequisites:
Node.js and npm installed on your server.
PM2 installed globally (
npm install -g pm2).A React app ready to be deployed.
Step-by-Step Guide:
1. Build Your React App
First, navigate to your React app's root directory and build the production-ready files using npm or yarn.
cd /path/to/your/react-app
npm install # or yarn install if you're using yarn
npm run build # or yarn build
This will create a build/ directory containing all the production files needed for your app.
2. Install PM2
If you haven't installed PM2 yet, you can do so globally using npm:
npm install -g pm2
PM2 will help you manage and monitor your React app in the background.
3. Install a Static File Server
Since React apps are essentially static files after building, you'll need a server to serve them. You can use a simple static server like serve to do that.
Install serve globally:
npm install -g serve
4. Serve Your React App
Once the app is built, you can serve the app with the serve command. You can run this locally or use PM2 to manage it in the background.
serve -s build
The -s flag ensures the app is served as a single-page application (SPA).
5. Start Your React App with PM2
Now that the app is ready to be served, use PM2 to manage the process:
pm2 serve ./build 3000 --name "react-app"
This command tells PM2 to serve the files from the build directory on port 3000 and name the process "react-app". You can change the port to your preferred one.
./build: Path to your build folder.3000: The port you want the app to run on.--name "react-app": A name to refer to your app in PM2.
6. Check PM2 Status
To check if your app is running, use the following command:
pm2 list
This will show a list of all the processes being managed by PM2, and you should see your React app listed.
7. Set PM2 to Start on Boot
You can configure PM2 to restart the app automatically when the server reboots.
Run the following command to generate startup scripts for your system:
pm2 startup
Follow the instructions given in the output to configure PM2 to run on startup.
Then, save the current PM2 process list:
pm2 save
This will save the current list of processes, ensuring they are restarted automatically after a system reboot.
8. Monitor Your React App with PM2
To monitor the logs of your app, use:
pm2 logs react-app
This will stream the logs for the React app process.
9. Other Useful PM2 Commands
- To stop the app:
pm2 stop react-app
- To restart the app:
pm2 restart react-app
- To delete the app from PM2's process list:
pm2 delete react-app
10. Optional: Setting Up a Reverse Proxy with Nginx
If you want your app to be accessible from a domain (e.g., example.com), you can set up a reverse proxy with Nginx to forward requests to your React app. Here's how:
Install Nginx on your server if it's not already installed:
sudo apt update sudo apt install nginxConfigure Nginx to reverse proxy requests to the port your React app is running on. Edit the Nginx configuration:
sudo nano /etc/nginx/sites-available/defaultAdd the following configuration (replace
example.comwith your domain):server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }Restart Nginx to apply the changes:
sudo systemctl restart nginx
Now, your React app will be available at http://example.com.
Conclusion
By following these steps, you can successfully host your React app with PM2, ensuring that it's running in the background, automatically restarts on reboots, and can be monitored easily. If you're using Nginx, you can also set up a reverse proxy to make it accessible through your domain name. Let me know if you need further clarification!
About the author
Anand Mohan is a Software Developer turned DevOps Engineer and a Public speaker. Anand has worked with multiple startups and MNCs and helped them scale their Development and Deployment and community. Anand currently working with HeaTrec Solutions. In his leisure time, Anand writes about Web Development, Cloud technologies, and DevOps . Anand also creates educational content on multiple platforms like Twitter and LinkedIn and Own Blogging Websites.
Comments
Join the discussionNo comments yet. Be the first to comment.