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

# 使用 PM2 将 Bun 作为守护进程运行

[PM2](https://pm2.keymetrics.io/) 是一个进程管理器，可以将你的应用程序作为守护进程（后台进程）运行。

它提供进程监控、自动重启和扩展功能，并在你将应用程序部署到云托管的虚拟专用服务器（VPS）时保持其运行。

***

你可以通过两种方式将 PM2 与 Bun 结合使用：作为 CLI 选项或配置文件。

### 使用 `--interpreter`

要通过 PM2 以 Bun 作为解释器启动应用程序，请运行：

```bash terminal icon="terminal" theme={null}
pm2 start --interpreter ~/.bun/bin/bun index.ts
```

***

### 使用配置文件

或者，在你的项目目录中创建一个名为 `pm2.config.js` 的文件，并添加以下内容。

```js pm2.config.js icon="file-code" theme={null}
module.exports = {
  name: "app", // 你的应用程序名称
  script: "index.ts", // 你的应用程序入口点
  interpreter: "bun", // Bun 解释器
  env: {
    PATH: `${process.env.HOME}/.bun/bin:${process.env.PATH}`, // 将 "~/.bun/bin/bun" 添加到 PATH
  },
};
```

***

保存文件后，使用 PM2 启动你的应用程序。

```bash terminal icon="terminal" theme={null}
pm2 start pm2.config.js
```

***

你的 JavaScript/TypeScript Web 服务器现在作为守护进程使用 PM2 运行，使用 Bun 作为解释器。
