> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chartbrew.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build and Deploy

> Learn how to build and deploy your own version of Chartbrew

<Note>
  With time, this guide will be updated with more ways of deploying. Any PR is welcome!
</Note>

## Building the app

Follow the [Getting Started](/quickstart) guide to set up the project somewhere on your server.

Please note that you have to set all the production environment variables in this case.

Keep in mind that the tools used in this guide are used as an example but they work really well. There might be multiple other tools that you can use, so feel free to do so if you wish.

## Serving the app

This guide is tried and tested with Ubuntu. The settings should be the same on all operating systems, but some of the files and commands may vary.

### Backend

The backend doesn't need to undergo a build process. You need the following things to make sure it can be served:

* MySQL running
* A database is created so that Chartbrew can use it (it must be empty if it wasn't used by Chartbrew before)
* `npm install` ran in the server folder or `npm run setup` in the project root folder
* Environment variables are set for Production (check `.env-template` in the root folder to see which)

Once all these are checked, we can either run `npm run start`, or use an external tool like [`pm2`](https://pm2.keymetrics.io) to monitor and manage Node apps better.

```sh theme={null}
npm install -g pm2

# starting the app in production
cd chartbrew/server
NODE_ENV=production pm2 start index.js --name "chartbrew-server"
```

You can also start [pm2 as a cluster](https://pm2.keymetrics.io/docs/usage/cluster-mode/) to take advantage of your server's multi-threading capabilities and 0-downtime reloads. If you wish to do this, replace the last command above with:

```sh theme={null}
NODE_ENV=production pm2 start index.js --name "chartbrew-server" -i max
```

This will then run the server on the `port` specified in `server/settings.js`.

### Frontend

The Frontend app needs to be built and then served using [pm2](https://pm2.keymetrics.io) like above.

```sh theme={null}
# build the app
cd chartbrew/client
npm run build
```

Create the `pm2` configuration file:

```sh theme={null}
vim app.config.json
```

```js theme={null}
{
  apps : [
    {
      name: "cbc-client",
      script: "npx",
      interpreter: "none",
      args: "serve -s dist -l 4018"
    }
  ]
}
```

Ensure that the port number after `-l` is the same as the `VITE_APP_CLIENT_PORT` in your `.env` file.

Now you can start the front-end app with `pm2 start app.config.json`

Using the methods above, the app can be accessed on localhost. To serve it on a domain, check the deployment guides for different platforms in the sidebar.
