提交工单

香港GPU服务器跑Stable Diffusion:AUTOMATIC1111 + ComfyUI部署电商图片生成实战

文档更新于

独立站的产品图、营销 Banner、社媒素材——每张外包给设计师至少 ¥50~200,批量生产时成本惊人。在香港 GPU 服务器上部署 Stable Diffusion,月费约 ¥500~1500,可以无限生成高质量图片,团队成员通过浏览器即可使用,是电商、内容创作团队降低视觉成本的利器。


一、GPU 服务器选型

GPU 型号 显存 SD 生成速度(512×512) 适合模型 月费参考
RTX 3060 12GB 约 3~5 秒/张 SDXL、SD1.5 ¥800~1200
RTX 3090 24GB 约 1~2 秒/张 SDXL、Flux ¥1500~2500
RTX 4090 24GB < 1 秒/张 所有模型 ¥3000~5000
A100(40GB) 40GB < 1 秒/张 Flux、大模型 ¥5000+

没有 GPU 的服务器也可以用 CPU 运行,但速度非常慢(约 5~15 分钟/张),仅适合测试。

二、安装 NVIDIA 驱动和 CUDA

<code"># Ubuntu 22.04 + NVIDIA GPU 服务器

# 安装 NVIDIA 驱动
apt update && apt install -y ubuntu-drivers-common
ubuntu-drivers autoinstall

# 或手动指定版本
apt install -y nvidia-driver-550

reboot

# 验证安装
nvidia-smi
# 输出应包含 GPU 型号和 CUDA 版本

# 安装 CUDA Toolkit
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
dpkg -i cuda-keyring_1.1-1_all.deb
apt update && apt install -y cuda-toolkit-12-4

# 安装 Python 和虚拟环境
apt install -y python3.10 python3.10-venv python3-pip git wget

三、部署 AUTOMATIC1111 WebUI

<code"># 创建专用用户(不用 root 运行 AI 应用)
useradd -m -s /bin/bash sduser
su - sduser

# 克隆 WebUI 仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui

# 安装依赖(首次运行自动安装,需要约10分钟)
# 首先下载基础模型(选择一个)
mkdir -p models/Stable-diffusion

# 下载 SDXL Base 1.0(通用高质量,约 7GB)
# 需从 Hugging Face 或 CivitAI 下载,放入 models/Stable-diffusion/

# 启动 WebUI(允许外网访问 + API)
python launch.py \
  --listen \
  --port 7860 \
  --api \                    # 开启 API(供程序调用)
  --xformers \               # 显存优化(RTX 系列推荐)
  --enable-insecure-extension-access \
  --no-gradio-queue          # 避免队列问题
<code"># Nginx 反向代理(HTTPS + Basic Auth)
# /etc/nginx/sites-available/sd.yourdomain.com
server {
    listen 443 ssl http2;
    server_name sd.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/sd.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sd.yourdomain.com/privkey.pem;

    # 限制访问(防止被公开使用)
    auth_basic "Stable Diffusion";
    auth_basic_user_file /etc/nginx/.htpasswd;

    client_max_body_size 100M;
    proxy_read_timeout 600s;    # 生成图片需要较长时间

    location / {
        proxy_pass http://127.0.0.1:7860;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

四、常用电商图片生成 Prompt

<code">
# 产品主图(白背景)
Prompt:
product photo of [商品描述], white background, studio lighting,
professional product photography, high resolution, 8k, sharp focus,
clean minimalist style, commercial photography

Negative Prompt:
blurry, low quality, text, watermark, background, shadow, person

# 营销 Banner(场景图)
Prompt:
[商品] on [场景], lifestyle photography, natural lighting,
warm color tone, magazine style, professional photo shoot,
bokeh background, high quality commercial image

# 社媒素材(Instagram 风格)
Prompt:
aesthetic flat lay of [商品], instagram style photography,
pastel color palette, minimalist composition, overhead shot,
natural daylight, influencer style

五、API 调用(批量生产图片)

<code">import requests
import base64
import os
from pathlib import Path

SD_API = "http://localhost:7860"

def generate_product_image(
    product_name: str,
    output_path: str,
    width: int = 1024,
    height: int = 1024,
    steps: int = 25
) -> str:
    """批量生成产品图片"""
    prompt = f"""
    product photo of {product_name}, white background, studio lighting,
    professional commercial photography, high resolution, 8k, sharp focus,
    clean minimalist
    """
    negative = "blurry, low quality, text, watermark, shadow, person, hand"

    payload = {
        "prompt": prompt,
        "negative_prompt": negative,
        "steps": steps,
        "width": width,
        "height": height,
        "cfg_scale": 7,
        "sampler_name": "DPM++ 2M Karras",
        "batch_size": 1,
        "seed": -1,   # -1 = 随机种子
    }

    response = requests.post(f"{SD_API}/sdapi/v1/txt2img", json=payload)
    result = response.json()

    # 保存图片
    image_data = base64.b64decode(result['images'][0])
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    with open(output_path, 'wb') as f:
        f.write(image_data)

    print(f"已生成:{output_path}")
    return output_path

# 批量生成产品列表的图片
products = [
    "wireless bluetooth headphones",
    "portable phone charger",
    "ergonomic laptop stand",
    "smart watch",
    "USB-C hub",
]

for product in products:
    output = f"/output/{product.replace(' ', '_')}.png"
    generate_product_image(product, output)

六、部署 ComfyUI(高级工作流)

<code"># ComfyUI 基于节点的工作流设计,适合复杂图像处理流程
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI

python3 -m venv venv
source venv/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu124
pip install -r requirements.txt

# 启动 ComfyUI
python main.py --listen 0.0.0.0 --port 8188

# ComfyUI 优势:
# - 节点式工作流,可视化复杂管线
# - 支持 Flux 等最新模型
# - 工作流可导出分享
# - API 调用返回更详细的控制

七、LoRA 模型微调应用(品牌风格一致性)

<code">
LoRA(Low-Rank Adaptation)让模型学习特定风格或产品外观

应用场景:
1. 品牌产品风格:上传 20~50 张产品图 → 训练 LoRA → 生成保持品牌风格的图片
2. 人物一致性:训练品牌代言人形象 → 生成不同场景但形象一致的图片
3. Logo/ICON:学习品牌视觉语言 → 生成风格一致的素材

在 AUTOMATIC1111 中使用 LoRA:
在 Prompt 中添加:
数字(0.8)= LoRA 影响强度(0-1)

推荐 LoRA 训练工具:
- kohya_ss(功能完整,适合精确训练)
- SimpleTuner(简单快速)

八、成本效益分析

方案 月费 月产出图片量(估算) 单张成本
外包设计师 按需 — ¥50~200/张
MidJourney Pro $60/月 约 3000 张 约 ¥0.14/张
香港 RTX 3090 GPU 服务器 ¥1500/月 约 10 万张 约 ¥0.015/张

九、总结

香港 GPU 服务器部署 Stable Diffusion 的优势:无内容审查限制(可生成各类商业图片)、无版权风险(生成图片可商业使用)、团队多人共享、成本随使用量增加而边际成本接近零。对于月产图需求超过 1000 张的电商团队,自建 GPU 服务器的成本优势极为明显。

需要服务器?

香港 / 美国 / 日本服务器与 VPS,工程师 7×24 在线,工单平均 10 分钟内响应。