Saleor GraphQL 电商平台部署教程

Saleor 是一个基于 Python/Django 的开源电商平台,最大的特点是以 GraphQL 作为唯一的 API 接口。它提供了完善的电商功能,包括多仓库库存管理、多渠道销售、灵活的定价策略和权限系统。Saleor 的架构非常适合 Headless Commerce 场景。本文将通过 Docker 在搬瓦工 VPS 上部署完整的 Saleor 平台。

一、系统要求

  • DockerDocker Compose:推荐使用容器化部署。
  • 内存:至少 2GB,推荐 4GB。
  • 磁盘:至少 10GB 可用空间。

确保已安装 Docker 和 Docker Compose

二、使用 Saleor Platform 部署

Saleor Platform 是官方提供的一键部署方案,包含 API 后端、Dashboard 管理面板和示例 Storefront。

2.1 克隆项目

git clone https://github.com/saleor/saleor-platform.git
cd saleor-platform
git submodule update --init --recursive

2.2 配置环境变量

# 复制环境变量模板
cp .env.example .env

编辑 .env 文件,修改关键配置:

# 数据库密码
POSTGRES_PASSWORD=你的强数据库密码

# Django 密钥
SECRET_KEY=你的随机Django密钥

# 允许的域名
ALLOWED_HOSTS=api.example.com,localhost
CSRF_TRUSTED_ORIGINS=https://api.example.com

# Dashboard 和 Storefront URL
DASHBOARD_URL=https://admin.example.com
STOREFRONT_URL=https://store.example.com

2.3 构建和启动

# 构建所有服务
docker compose build

# 启动所有服务
docker compose up -d

# 查看服务状态
docker compose ps

2.4 初始化数据

# 运行数据库迁移
docker compose exec api python manage.py migrate

# 创建超级管理员
docker compose exec api python manage.py createsuperuser

# 填充示例数据(可选,包含示例商品和配置)
docker compose exec api python manage.py populatedb

三、Nginx 反向代理

# API 后端
server {
    listen 443 ssl http2;
    server_name api.example.com;

    ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;

    client_max_body_size 100M;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /media/ {
        alias /path/to/saleor-platform/saleor/media/;
        expires 30d;
    }
}

# Dashboard 管理面板
server {
    listen 443 ssl http2;
    server_name admin.example.com;

    ssl_certificate /etc/letsencrypt/live/admin.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/admin.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:9002;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
nginx -t && systemctl reload nginx

四、GraphQL API 使用

Saleor 的所有操作都通过 GraphQL API 完成。访问 https://api.example.com/graphql/ 可以打开 GraphQL Playground。

4.1 查询商品

curl -X POST https://api.example.com/graphql/ \
  -H "Content-Type: application/json" \
  -d '{
    "query": "{ products(first: 10, channel: \"default-channel\") { edges { node { id name description pricing { priceRange { start { gross { amount currency } } } } thumbnail { url } } } } }"
  }'

4.2 创建结账流程

# 创建结账
curl -X POST https://api.example.com/graphql/ \
  -H "Content-Type: application/json" \
  -d '{
    "query": "mutation { checkoutCreate(input: { channel: \"default-channel\", lines: [{ variantId: \"UHJvZHVjdFZhcmlhbnQ6MQ==\", quantity: 1 }] }) { checkout { id token totalPrice { gross { amount currency } } } errors { field message } } }"
  }'

五、配置支付网关

Saleor 支持多种支付网关。以 Stripe 为例:

# 安装 Saleor Stripe 插件
docker compose exec api pip install saleor-stripe-plugin

在 Dashboard 的"配置 > 插件"中启用 Stripe 并填入 API 密钥。

六、生产环境优化

6.1 配置 Redis 缓存

确保 docker-compose.yml 中已包含 Redis 服务,并在环境变量中配置:

CACHE_URL=redis://redis:6379/0
CELERY_BROKER_URL=redis://redis:6379/1

6.2 配置 Celery 异步任务

Saleor 使用 Celery 处理异步任务(邮件发送、Webhook 触发等)。确保 Celery Worker 正常运行:

docker compose exec api celery -A saleor worker --loglevel=info

6.3 数据库备份

# 备份 PostgreSQL 数据库
docker compose exec db pg_dump -U saleor saleor > /root/backups/saleor_$(date +%Y%m%d).sql

七、Storefront 前端

Saleor 官方提供了基于 React 的 Storefront 模板。也可以使用任何前端框架通过 GraphQL API 对接。社区还提供了 Next.js 版本的 Storefront 模板,可以在搬瓦工 VPS 上使用 PM2 部署运行。

总结

Saleor 是一个架构先进的电商平台,GraphQL-first 的 API 设计让前后端协作更加高效。通过 Docker Compose 部署在搬瓦工 VPS 上非常便捷。建议使用 4GB 内存以上的方案以获得最佳体验。如果你更倾向于 Node.js 技术栈,可以参考 Medusa 电商后端。选购搬瓦工 VPS 请查看 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。