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

# Deploying

> Deploying Unified CDN to production.

So far, you have installed Unified CDN and ran it in development mode. Now, you can deploy it to production to use in your apps.

## Prerequisites

1. You have completed the [installation](https://unified-cdn-docs.ngqian.com/dashboard/installation.md) steps and have a working Unified CDN instance.
2. You have a domain name and a **server** to host Unified CDN. This can be a VPS, cloud server, or any other hosting solution that **supports Node.js**.

> [!WARNING]
>
> **You must not use a serverless platform to host**.
>
> Serverless platforms like Vercel and Netlify are not suitable as they often impose a max request body payload, blocking files from being uploaded directly to Unified CDN. [See here](https://vercel.com/docs/functions/limitations#request-body-size) for more information.

## Build step

Unified CDN is made with Next.js and comes **unbuilt**. To push it to production, you have to first build the project. Run the following command in your terminal:

> [tab] Node
>
> > [!NOTE]
> >
> > Before running any commands, make sure the `OUTPUT_METHOD` variable in your `.env` file is set to `node`. This will ensure that Unified CDN is built for a Node.js environment.
>
> Run the build script using npm:
>
> ```bash theme={null}
> npm run build
> ```

> [tab] Docker
>
> > [!NOTE]
> >
> > Before running any commands, make sure the `OUTPUT_METHOD` variable in your `.env` file is set to `docker`. This will ensure that Unified CDN is built for a Node.js environment.
>
> Unified CDN already comes with a Dockerfile (modified from the guide [here](https://docs.docker.com/guides/nextjs/)) Build the image using docker build:
>
> ```bash theme={null}
> docker build -f Dockerfile.docker  --secret id=envfile,src=.env -t unified-cdn .
> ```

## Hosting

Once the build step is completed, you can host it on your server.

> [tab] Node
>
> ```bash theme={null}
> npm run start 
> ```
>
> In a **production environment**, you would typically use a process manager like [PM2](https://pm2.keymetrics.io/) to keep Unified CDN running in the background, even after you close your terminal or restart your server. You can install PM2 globally with:
>
> ```bash theme={null}
> npm install -g pm2
> ```
>
> Then run the following command to start Unified CDN with PM2:
>
> ```bash theme={null}
> pm2 start ecosystem.config.cjs
> ```
>
> This creates a new **pm2 process**, with the name `unified-cdn`. Use the name `unified-cdn` if you want to start, stop or restart Unified CDN with PM2. For example, to restart Unified CDN, run:
>
> ```bash theme={null}
> pm2 restart unified-cdn
> ```
>
> For more information on PM2 process management, see [here](https://pm2.keymetrics.io/docs/usage/process-management/).
>
> If you want to make Unified CDN persist across server boots, check the [PM2 startup guide](https://pm2.keymetrics.io/docs/usage/startup/).

> [tab] Docker
>
> Create and run a **docker container** from the image built:
>
> ```bash theme={null}
> docker run --name unified-cdn -d --env-file .env -p 3000:3000 unified-cdn
> ```
>
> You can now access it at port `3000`. To use a different port, simply modify the `-p` flag: `-p [host_port]:[container_port]`
>
> Use the container name `unified-cdn` to start, stop or restart Unified CDN. For example, to restart Unified CDN, run:
>
> ```bash theme={null}
> docker restart unified-cdn
> ```

## Updates

When a new version of Unified CDN is released, you can update your instance by following these steps:

**1. Stop Unified CDN**

> [tab] Node
>
> Stop Unified CDN with PM2:
>
> ```bash theme={null}
> pm2 stop unified-cdn
> ```

> [tab] Docker
>
> Stop Unified CDN with Docker:
>
> ```bash theme={null}
> docker stop unified-cdn
> ```

**2. Pull latest changes**

Get the latest changes from the repository:

```bash theme={null}
git pull
```

**3. Install dependencies**

> [tab] Node
>
> Install any new packages from the update:
>
> ```bash theme={null}
> npm install
> ```

> [tab] Docker
>
> No need to install packages manually. The Dockerfile handles this.

**4. Rebuild Unified CDN**

> [tab] Node
>
> Rebuild Unified CDN to apply the updates:
>
> ```bash theme={null}
> npm run build
> ```

> [tab] Docker
>
> Rebuild Unified CDN to apply the updates:
>
> ```bash theme={null}
> docker build --secret id=envfile,src=.env -t unified-cdn .
> ```

**5. Restart Unified CDN**

> [tab] Node
>
> Restart Unified CDN with PM2:
>
> ```bash theme={null}
> pm2 restart unified-cdn
> ```

> [tab] Docker
>
> Restart Unified CDN with Docker:
>
> ```bash theme={null}
> docker restart unified-cdn
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.