# 🚀 Strapi 生产环境部署指南

URL: https://caijiao.org/posts/37
Source: docs/posts/37.md
Description: 本文介绍如何在生产环境部署 Strapi，包括环境要求、构建步骤、使用 PM2 守护进程、Nginx 反向代理配置以及环境变量和优化建议。

在生产环境部署 **Strapi** 时，需要确保安全性、稳定性和性能。下面是详细的生产部署指南（以 Linux + Node.js + [Nginx](/nginx/) 为例）。

## 🧩 一、前置条件

在服务器上安装好以下环境：

| 软件          | 说明                           | 检查命令             |
| ------------- | ------------------------------ | -------------------- |
| Node.js (LTS) | 建议使用 v18+                  | `node -v`            |
| npm 或 yarn   | 用于安装依赖                   | `npm -v` / `yarn -v` |
| 数据库        | [PostgreSQL](/postgresql/) / MySQL / SQLite 等 | —                    |
| [PM2](/pm2/)           | Node 进程守护工具              | `npm i -g pm2`       |
| Nginx         | 用作反向代理和静态资源服务器   | `nginx -v`           |

## ⚙️ 二、构建生产版本

1️⃣ 进入项目目录

```bash
cd /var/www/my-strapi
```

````

2️⃣ 安装依赖

```bash
npm install --production
# 或
yarn install --production
```

3️⃣ 构建管理面板（Admin UI）

```bash
npm run build
```

4️⃣ 启动测试

```bash
npm run start
```

访问 `http://localhost:1337` 确认运行正常。

## 🚀 三、使用 PM2 启动守护进程

创建 PM2 配置：

```bash
pm2 start npm --name "strapi" -- run start
pm2 save
pm2 startup
```

> 💡 PM2 会在系统重启后自动启动 Strapi。
> 你可以查看状态：
>
> ```bash
> pm2 list
> pm2 logs strapi
> ```

## 🌍 四、配置 Nginx 反向代理（推荐）

编辑配置文件：

```bash
sudo nano /etc/nginx/sites-available/strapi.conf
```

内容示例：

```nginx
server {
  listen 80;
  server_name your-domain.com;

  location / {
    proxy_pass http://127.0.0.1:1337;
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  # 可选：服务静态文件
  location /uploads/ {
    alias /var/www/my-strapi/public/uploads/;
  }
}
```

启用并重启：

```bash
sudo ln -s /etc/nginx/sites-available/strapi.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

## 🔐 五、环境变量配置

在生产中，不要在代码里写死配置，而是使用 `.env` 文件：

```bash
NODE_ENV=production
HOST=0.0.0.0
PORT=1337

APP_KEYS=your_app_keys
API_TOKEN_SALT=your_api_token_salt
ADMIN_JWT_SECRET=your_admin_jwt_secret
JWT_SECRET=your_jwt_secret

# 数据库连接
DATABASE_CLIENT=postgres
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_NAME=strapi_db
DATABASE_USERNAME=strapi_user
DATABASE_PASSWORD=your_password
```

然后在 Strapi 的 `config/database.js`、`config/server.js` 使用 `env()` 读取。

## 📦 六、备份与升级建议

| 类型       | 建议                                    |
| ---------- | --------------------------------------- |
| 数据库备份 | 定期备份 PostgreSQL / MySQL             |
| 上传文件   | 同步 `/public/uploads/`                 |
| 安全更新   | 定期执行 `npm audit`、`pm2 update`      |
| HTTPS      | 使用 Let’s Encrypt 配置 SSL (`certbot`) |

## 🧠 七、可选优化

- **使用 Docker 部署**：通过 `Dockerfile + docker-compose.yml` 封装环境。
- **使用 CI/CD**：在 GitLab/Jenkins 中自动构建、发布。
- **集群部署**：结合 **PM2 Cluster Mode** 或 Kubernetes。

## ✅ 示例生产启动命令

```bash
NODE_ENV=production pm2 start npm --name strapi -- run start
```
````
