Stable Diffusion WebUI
Stable Diffusion WebUI(通常指 AUTOMATIC1111 的 stable-diffusion-webui 项目)是最流行的 Stable Diffusion 图形界面,提供了文生图(txt2img)、图生图(img2img)、图像放大、ControlNet 等丰富功能。通过浏览器即可访问,适合在 GPU 服务器上部署供团队使用。
- CentOS Stream 9 & 10 / AlmaLinux 9 & 10 / Rocky Linux 9 & 10
- Python 3.10 或 3.11(EL 10 上默认 Python 3.12,但 SD WebUI 推荐 3.10/3.11,需手动安装旧版本)
- Git
- NVIDIA GPU,至少 6 GB 显存(推荐 8 GB 以上)
- 已安装 NVIDIA 驱动和 CUDA(参考 NVIDIA 驱动与 CUDA)
- 至少 20 GB 磁盘空间(模型文件较大)
安装系统依赖
Section titled “安装系统依赖”sudo dnf install -y git python3.11 python3.11-devel python3.11-pip \ gcc gcc-c++ make wget \ libffi-devel openssl-devel bzip2-devel \ mesa-libGL确认 GPU 环境
Section titled “确认 GPU 环境”nvidia-smi确保输出中显示了你的 GPU 型号和驱动版本。
创建专用用户(可选但推荐)
Section titled “创建专用用户(可选但推荐)”sudo useradd -m -s /bin/bash sdusersudo su - sduser安装 Stable Diffusion WebUI
Section titled “安装 Stable Diffusion WebUI”cd /optsudo mkdir -p stable-diffusionsudo chown $(whoami):$(whoami) stable-diffusioncd stable-diffusion
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.gitcd stable-diffusion-webui你至少需要一个 Stable Diffusion 模型检查点文件。从 Hugging Face 或 Civitai 下载模型并放入 models/Stable-diffusion/ 目录:
# 下载 Stable Diffusion v1.5(约 4 GB)wget -P models/Stable-diffusion/ \ https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
# 或下载 SDXL Base(约 6.5 GB)wget -P models/Stable-diffusion/ \ https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensorsWebUI 的启动脚本会自动创建虚拟环境并安装所有 Python 依赖:
cd /opt/stable-diffusion/stable-diffusion-webui
# 使用 Python 3.11 启动python3.11 launch.py --listen --xformers参数说明:
--listen:监听所有网络接口(默认仅 127.0.0.1),允许远程访问--xformers:启用 xformers 加速,减少显存占用并加速生成
首次运行会下载并安装大量依赖,可能需要 10-20 分钟,取决于网络速度。
安装完成后,会输出类似以下信息:
Running on local URL: http://0.0.0.0:7860常用启动参数
Section titled “常用启动参数”python3.11 launch.py \ --listen \ --port 7860 \ --xformers \ --enable-insecure-extension-access \ --api \ --medvram| 参数 | 说明 |
|---|---|
--listen | 监听所有接口 |
--port | 指定端口号 |
--xformers | 启用 xformers 内存优化 |
--api | 启用 REST API 接口 |
--medvram | 中等显存优化(6-8 GB 显存推荐) |
--lowvram | 低显存优化(4-6 GB 显存) |
--enable-insecure-extension-access | 允许在 WebUI 中安装扩展 |
--gradio-auth user:pass | 设置访问用户名和密码 |
--no-half-vae | 如果生成黑图,尝试此参数 |
访问 WebUI
Section titled “访问 WebUI”在浏览器中打开 http://your-server-ip:7860。
文生图(txt2img)
Section titled “文生图(txt2img)”- 在顶部选择已加载的模型
- 在 Prompt 框中输入英文提示词,描述你想生成的图像
- 在 Negative Prompt 中输入不希望出现的元素
- 调整参数:
- Sampling method:推荐 DPM++ 2M Karras 或 Euler a
- Sampling steps:20-30 步
- Width/Height:SD 1.5 推荐 512x512;SDXL 推荐 1024x1024
- CFG Scale:7-12
- Seed:-1 为随机
- 点击 Generate
图生图(img2img)
Section titled “图生图(img2img)”切换到 img2img 标签页,上传一张参考图,填写提示词,调整 Denoising strength(0.3-0.7),点击生成。
API 调用
Section titled “API 调用”启动时加上 --api 参数后,可以通过 HTTP API 生成图像:
curl -X POST http://localhost:7860/sdapi/v1/txt2img \ -H "Content-Type: application/json" \ -d '{ "prompt": "a beautiful sunset over mountains, photorealistic", "negative_prompt": "blurry, low quality", "steps": 25, "width": 512, "height": 512, "cfg_scale": 7 }'API 文档可在 http://your-server-ip:7860/docs 查看。
systemd 服务
Section titled “systemd 服务”创建 systemd 服务以便管理和自动启动:
sudo tee /etc/systemd/system/stable-diffusion.service > /dev/null <<'EOF'[Unit]Description=Stable Diffusion WebUIAfter=network-online.targetWants=network-online.target
[Service]Type=simpleUser=sduserGroup=sduserWorkingDirectory=/opt/stable-diffusion/stable-diffusion-webuiExecStart=/usr/bin/python3.11 launch.py --listen --port 7860 --xformers --api --enable-insecure-extension-accessRestart=on-failureRestartSec=10Environment="HOME=/home/sduser"
# 资源限制LimitNOFILE=65536
[Install]WantedBy=multi-user.targetEOF启用并启动服务:
sudo systemctl daemon-reloadsudo systemctl enable --now stable-diffusion管理服务:
# 查看状态sudo systemctl status stable-diffusion
# 查看日志sudo journalctl -u stable-diffusion -f
# 重启sudo systemctl restart stable-diffusionsudo firewall-cmd --permanent --add-port=7860/tcpsudo firewall-cmd --reload使用 Nginx 反向代理的示例配置:
sudo tee /etc/nginx/conf.d/sd-webui.conf > /dev/null <<'EOF'server { listen 443 ssl; server_name sd.example.com;
ssl_certificate /etc/letsencrypt/live/sd.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/sd.example.com/privkey.pem;
client_max_body_size 100M;
location / { proxy_pass http://127.0.0.1:7860; 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; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 300s; }}EOF启动报错:No module named pip
Section titled “启动报错:No module named pip”python3.11 -m ensurepip --upgrade黑色图像或纯色图像
Section titled “黑色图像或纯色图像”尝试添加 --no-half 或 --no-half-vae 启动参数:
python3.11 launch.py --listen --xformers --no-half-vae显存不足(Out of Memory)
Section titled “显存不足(Out of Memory)”# 启用显存优化python3.11 launch.py --listen --xformers --medvram
# 更激进的显存优化python3.11 launch.py --listen --xformers --lowvram
# 减小生成尺寸# 在 WebUI 中将 Width/Height 减小到 512x512pip 安装依赖缓慢
Section titled “pip 安装依赖缓慢”设置 pip 国内镜像:
mkdir -p ~/.config/pipcat > ~/.config/pip/pip.conf <<'EOF'[global]index-url = https://mirrors.aliyun.com/pypi/simple/trusted-host = mirrors.aliyun.comEOF更新 WebUI
Section titled “更新 WebUI”cd /opt/stable-diffusion/stable-diffusion-webuigit pull# 重新启动即可,会自动更新依赖