# 部署

> Source: https://strapi.nodejs.cn/cms/deployment

🌐 Deployment

部署选项包括硬件/软件先决条件、环境变量设置以及在启动前构建管理面板。在文档中：提供了针对特定提供商的指南和高级指南的链接，以帮助选择合适的托管策略。

Strapi 为你的项目或应用提供了多种部署选项。你的 Strapi 应用可以部署在传统托管服务器上，或你偏好的托管提供商上。

🌐 Strapi provides many deployment options for your project or application. Your Strapi applications can be deployed on traditional hosting servers or your preferred hosting provider.

以下文档介绍了如何使用几个常见的托管选项准备 Strapi 以进行部署的基础知识。

🌐 The following documentation covers the basics of how to prepare Strapi for deployment on with several common hosting options.

:::strapi Strapi Cloud

你可以使用 [Strapi Cloud](/cloud/intro) 快速部署和托管你的项目。

🌐 You can use [Strapi Cloud](/cloud/intro) to quickly deploy and host your project.

:::

:::tip

如果你已经使用内容类型构建器创建了内容结构，并通过内容管理器向本地（开发）Strapi 实例添加了一些数据，你可以利用[数据管理系统](/cms/features/data-management)将数据从一个 Strapi 实例传输到另一个实例。

🌐 If you already created a content structure with the Content-Type Builder and added some data through the Content Manager to your local (development) Strapi instance, you can leverage the [data management system](/cms/features/data-management) to transfer data from a Strapi instance to another one.

另一种可能的工作流程是首先在本地创建内容结构，将项目推送到基于 git 的存储库，将更改部署到生产环境，然后再将内容添加到生产实例。

🌐 Another possible workflow is to first create the content structure locally, push your project to a git-based repository, deploy the changes to production, and only then add content to the production instance.

:::

:::caution

对于自托管的 Kubernetes 部署，我们建议使用 **npm** 而不是 **pnpm**。`pnpm` 对依赖的激进提升可能会破坏本地模块，例如 `mysql2`——你的应用可能依赖它们。`npm` 更平坦、更可预测的 `node_modules` 布局有助于确保本地包正确加载。

🌐 For self-hosted Kubernetes deployments, we recommend using **npm** rather than **pnpm**. `pnpm` aggressive hoisting of dependencies can break native modules, such as `mysql2`— that your application may rely on. `npm` flatter, more predictable `node_modules` layout helps ensure native packages load correctly.

:::

## 一般指南 {#general-guidelines}

🌐 General guidelines

### 硬件和软件要求 {#hardware-and-software-requirements}

🌐 Hardware and software requirements

为了为 Strapi 提供尽可能最佳的环境，以下要求适用于开发（本地）以及登台和生产工作流程。

🌐 To provide the best possible environment for Strapi the following requirements apply to development (local) and staging and production workflows.

在安装 Strapi 之前，你的计算机上必须安装以下要求：

🌐 Before installing Strapi, the following requirements must be installed on your computer:

- [Node.js](https://nodejs.cn)：仅支持 [Active LTS or Maintenance LTS versions](https://nodejs.cn/about/previous-releases) （目前为 `v22`、`v24` 和 `v26`）。Node 的奇数版本发布，称为 Node.js 的“当前”版本，不受支持（例如 v23、v25）。
- 你首选的 Node.js 包管理器：
    - [npm](https://npm.nodejs.cn/cli/v6/commands/npm-install) (`v6`及以上)
    - [pnpm](https://pnpm.nodejs.cn/) （在 Strapi Cloud 上， [Corepack](https://nodejs.cn/api/corepack.html)  与项目的 `package.json` `packageManager` 字段中固定的 pnpm 版本匹配，如果没有固定，则使用 Corepack 打包的默认版本）
- [Python](https://www.python.org/downloads/) （如果使用 SQLite 数据库）
- 受支持的网络浏览器：管理面板的目标浏览器是匹配默认 [Browserslist](https://github.com/browserslist/browserslist) 查询的浏览器：`last 3 major versions`、`Firefox ESR`、`last 2 Opera versions` 和 `not dead`。请参见 [browsersl.ist](https://browsersl.ist/#q=last+3+major+versions%2C+Firefox+ESR%2C+last+2+Opera+versions%2C+not+dead) 了解当前的覆盖矩阵。项目可以在项目根目录下通过 Browserslist 配置覆盖这些默认设置。

- 适用于你的操作系统的标准构建工具（在大多数基于 Debian 的系统上为 `build-essentials` 包）
- 服务器的硬件规范（CPU、RAM、存储）：

| 硬件 | 推荐 | 最低 |
|------|------|------|
| CPU | 2核以上 | 1核 |
| 内存 | 4GB以上 | 2GB |
| 磁盘 | 32GB以上 | 8GB |

- 支持的数据库版本：

| 数据库 | 推荐 | 最低 |
|------------|-------------|---------|
| MySQL | 8.4 | 8.0 |
| MariaDB | 11.4 | 10.3 |
| PostgreSQL | 17.0 | 14.0 |
| SQLite | 3 | 3 |

Strapi 不支持 MongoDB（或任何 NoSQL 数据库），也不支持任何“云原生”数据库（例如 Amazon Aurora、Google Cloud SQL 等）。

🌐 Strapi does not support MongoDB (or any NoSQL databases), nor does it support any "Cloud Native" databases (e.g., Amazon Aurora, Google Cloud SQL, etc.).

:::strapi Database deployment

有关与 Strapi 一起部署数据库的内容，请参阅 [数据库指南](/cms/configurations/database#databases-installation)。

🌐 Deploying databases along with Strapi is covered in the [databases guide](/cms/configurations/database#databases-installation).

:::

- 受支持的操作系统：

| 操作系统 | 推荐 | 最低 |
|------------------|----------------|---------------|
| Ubuntu (LTS) | 24.04 | 20.04 |
| Debian | 11.x | 10.x |
| RHEL | 10.x | 8.x |
| macOS | 26.0 | 11.x |
| Windows 桌面 | 11 | 10 |
| Windows 服务器 | 不支持 | 不支持 |

### 应用配置 {#application-configuration}

🌐 Application Configuration

<br/>

#### 1. 配置 {#1-configure}

🌐 1. Configure

我们建议根据环境使用环境变量来配置你的应用，例如：

🌐 We recommend using environment variables to configure your application based on the environment, for example:

```js title="/config/server.js"

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
});
```

当你创建新项目时，Strapi 会生成一个带有默认值的 `.env` 文件。你可以编辑此文件或在所选的部署平台中设置变量（参见 [示例 `.env` 文件](/cms/configurations/environment#example-env-file)）：

🌐 Strapi generates a `.env` file with default values when you create a new project. You can edit this file or set variables in your chosen deployment platform (see [example `.env` file](/cms/configurations/environment#example-env-file)):

```
HOST=10.0.0.1
PORT=1338
```

:::tip

要了解更多关于配置的详细信息，请参阅 [配置](/cms/configurations) 文档。

🌐 To learn more about configuration details, see the [configurations](/cms/configurations) documentation.

:::

#### 2. 启动服务器 {#2-launch-the-server}

🌐 2. Launch the server

在生产中运行服务器之前，你需要构建生产管理面板：

🌐 Before running your server in production you need to build your admin panel for production:

```bash
NODE_ENV=production yarn build
```

```bash
NODE_ENV=production npm run build
```

```bash
npm install cross-env
```

然后在你的 `package.json` 脚本部分：

🌐 Then in your `package.json` scripts section:

```bash
"build:win": "cross-env NODE_ENV=production npm run build",
```

然后运行：

🌐 And run:

```bash
npm run build:win
```

使用 `production` 设置运行服务器：

🌐 Run the server with the `production` settings:

```bash
NODE_ENV=production yarn start
```

```bash
NODE_ENV=production npm run start
```

```bash
npm install cross-env
```

然后在你的 `package.json` 脚本部分：

🌐 Then in your `package.json` scripts section:

```bash
"start:win": "cross-env NODE_ENV=production npm start",
```

然后运行：

🌐 And run:

```bash
npm run start:win
```

:::caution

我们强烈推荐使用 [pm2](https://github.com/Unitech/pm2/) 来管理你的流程。

:::

如果你需要一个 server.js 文件来运行 `node server.js` 而不是 `npm run start`，那么请创建一个 `./server.js` 文件，如下所示：

🌐 If you need a server.js file to be able to run `node server.js` instead of `npm run start` then create a `./server.js` file as follows:

```js title="./server.js"

const strapi = require('@strapi/strapi');
strapi.createStrapi(/* {...} */).start();
```

:::caution

如果你正在开发一个基于 `TypeScript` 的项目，必须提供 `distDir` 选项来启动服务器。
欲了解更多信息，请查阅 [TypeScript 文档](/cms/typescript/development#use-the-createstrapi-factory)。

🌐 If you are developing a `TypeScript`-based project you must provide the `distDir` option to start the server.
For more information, consult the [TypeScript documentation](/cms/typescript/development#use-the-createstrapi-factory).

:::

:::tip Health check endpoint

Strapi 在 `/_health` 提供了一个轻量级的健康检查路径，用于运行时间监控和负载均衡器。当服务器准备就绪时，它会返回一个 HTTP `204 No Content` 状态和一个 `strapi: You are so French!` 头值，你可以使用它来确认应用可访问。

🌐 Strapi exposes a lightweight health check route at `/_health` for uptime monitors and load balancers. When the server is ready, it responds with an HTTP `204 No Content` status and a `strapi: You are so French!` header value, which you can use to confirm the application is reachable.

:::

### 高级配置 {#advanced-configurations}

🌐 Advanced configurations

如果你想将管理界面托管在不同于 API 的服务器上，[请查看此专门部分](/cms/configurations/admin-panel#deploy-on-different-servers)。

🌐 If you want to host the administration on another server than the API, [please take a look at this dedicated section](/cms/configurations/admin-panel#deploy-on-different-servers).

## 附加资源 {#additional-resources}

🌐 Additional resources

:::prerequisites

* 你的 Strapi 项目已[创建](/cms/installation)，其代码托管在 GitHub 上。
* 你已经阅读了[一般部署指南](/cms/deployment#general-guidelines)。

:::

Strapi 网站的 [integrations page](https://strapi.io/integrations) 包含有关如何将 Strapi 与许多资源集成的信息，包括如何在以下第三方平台上部署 Strapi：

- [在 AWS 上部署 Strapi](https://strapi.io/integrations/aws)

- [在 Azure 上部署 Strapi](https://strapi.io/integrations/azure)

- [在 DigitalOcean 应用平台上部署 Strapi](https://strapi.io/integrations/digital-ocean)

- [在 Heroku 上部署 Strapi](https://strapi.io/integrations/heroku)

<br/>

此外，社区维护的关于其他提供商的指南可在 [Strapi Forum](https://forum.strapi.io/c/community-guides/28)获得。这包括以下指南：

- [使用 Caddy 进行代理](https://forum.strapi.io/t/caddy-proxying-with-strapi/)
- [使用 HAProxy 进行代理](https://forum.strapi.io/t/haproxy-proxying-with-strapi/)
- [使用 NGinx 进行代理](https://forum.strapi.io/t/nginx-proxing-with-strapi/)
- [使用 PM2 进程管理器](https://forum.strapi.io/t/how-to-use-pm2-process-manager-with-strapi/)

<br/>

以下外部指南（非 Strapi 官方维护）可能也有助于在各种环境中部署 Strapi：

🌐 The following external guide(s), not officially maintained by Strapi, might also help deploy Strapi on various environments:

- [[微软社区] 在 Azure 上部署](https://techcommunity.microsoft.com/blog/appsonazureblog/strapi-on-app-service-quick-start/4401398)

:::strapi Multi-tenancy

如果你在寻找多租户选项，Strapi 博客有一个 [comprehensive guide](https://strapi.io/blog/multi-tenancy-in-strapi-a-comprehensive-guide)。

:::
