跳转到内容

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 磁盘空间(模型文件较大)
Terminal window
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
Terminal window
nvidia-smi

确保输出中显示了你的 GPU 型号和驱动版本。

Terminal window
sudo useradd -m -s /bin/bash sduser
sudo su - sduser
Terminal window
cd /opt
sudo mkdir -p stable-diffusion
sudo chown $(whoami):$(whoami) stable-diffusion
cd stable-diffusion
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui

你至少需要一个 Stable Diffusion 模型检查点文件。从 Hugging Face 或 Civitai 下载模型并放入 models/Stable-diffusion/ 目录:

Terminal window
# 下载 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.safetensors

WebUI 的启动脚本会自动创建虚拟环境并安装所有 Python 依赖:

Terminal window
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
Terminal window
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如果生成黑图,尝试此参数

在浏览器中打开 http://your-server-ip:7860

  1. 在顶部选择已加载的模型
  2. 在 Prompt 框中输入英文提示词,描述你想生成的图像
  3. 在 Negative Prompt 中输入不希望出现的元素
  4. 调整参数:
    • 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 为随机
  5. 点击 Generate

切换到 img2img 标签页,上传一张参考图,填写提示词,调整 Denoising strength(0.3-0.7),点击生成。

启动时加上 --api 参数后,可以通过 HTTP API 生成图像:

Terminal window
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 服务以便管理和自动启动:

Terminal window
sudo tee /etc/systemd/system/stable-diffusion.service > /dev/null <<'EOF'
[Unit]
Description=Stable Diffusion WebUI
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=sduser
Group=sduser
WorkingDirectory=/opt/stable-diffusion/stable-diffusion-webui
ExecStart=/usr/bin/python3.11 launch.py --listen --port 7860 --xformers --api --enable-insecure-extension-access
Restart=on-failure
RestartSec=10
Environment="HOME=/home/sduser"
# 资源限制
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF

启用并启动服务:

Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now stable-diffusion

管理服务:

Terminal window
# 查看状态
sudo systemctl status stable-diffusion
# 查看日志
sudo journalctl -u stable-diffusion -f
# 重启
sudo systemctl restart stable-diffusion
Terminal window
sudo firewall-cmd --permanent --add-port=7860/tcp
sudo firewall-cmd --reload

使用 Nginx 反向代理的示例配置:

Terminal window
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
Terminal window
python3.11 -m ensurepip --upgrade

尝试添加 --no-half--no-half-vae 启动参数:

Terminal window
python3.11 launch.py --listen --xformers --no-half-vae
Terminal window
# 启用显存优化
python3.11 launch.py --listen --xformers --medvram
# 更激进的显存优化
python3.11 launch.py --listen --xformers --lowvram
# 减小生成尺寸
# 在 WebUI 中将 Width/Height 减小到 512x512

设置 pip 国内镜像:

Terminal window
mkdir -p ~/.config/pip
cat > ~/.config/pip/pip.conf <<'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
EOF
Terminal window
cd /opt/stable-diffusion/stable-diffusion-webui
git pull
# 重新启动即可,会自动更新依赖