# 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`.

```bash
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:

```bash
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:

```bash
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.

```bash
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:

```bash
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:

```bash
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:

```bash
pm2 startup
```

Follow the instructions given in the output to configure PM2 to run on startup.

Then, save the current PM2 process list:

```bash
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:

```bash
pm2 logs react-app
```

This will stream the logs for the React app process.

#### 9\. **Other Useful PM2 Commands**

* **To stop the app:**
    

```bash
pm2 stop react-app
```

* **To restart the app:**
    

```bash
pm2 restart react-app
```

* **To delete the app from PM2's process list:**
    

```bash
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`](http://example.com)), you can set up a reverse proxy with Nginx to forward requests to your React app. Here's how:

1. Install Nginx on your server if it's not already installed:
    
    ```bash
    sudo apt update
    sudo apt install nginx
    ```
    
2. Configure Nginx to reverse proxy requests to the port your React app is running on. Edit the Nginx configuration:
    
    ```bash
    sudo nano /etc/nginx/sites-available/default
    ```
    
    Add the following configuration (replace [`example.com`](http://example.com) with your domain):
    
    ```nginx
    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;
        }
    }
    ```
    
3. Restart Nginx to apply the changes:
    
    ```bash
    sudo systemctl restart nginx
    ```
    

Now, your React app will be available at [`http://example.com`](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!
