Stable Diffusion WebUI

最近更新时间:2025-07-25 14:36:51

我的收藏
本指导适用于在 TencentOS Server 3 上运行 Stable Diffusion WebUI 应用框架,以 Docker 方式启动。

Stable Diffusion WebUI 环境准备

从 GitHub 下载 Stable Diffusion WebUI Docker 开源仓库到本地。
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
cd stable-diffusion-webui-docker

安装对应环境以及下载模型

1. 为了加速模型下载以及防止模型由于网络限制下载失败,我们将下载模型的链接改为镜像网站。打开 stable-diffusion-webui-docker/services/download/links.txt,可以看到如下内容:
https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt
out=Stable-diffusion/v1-5-pruned-emaonly.ckpt
https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.ckpt
out=VAE/vae-ft-mse-840000-ema-pruned.ckpt
https://huggingface.co/runwayml/stable-diffusion-inpainting/resolve/main/sd-v1-5-inpainting.ckpt
out=Stable-diffusion/sd-v1-5-inpainting.ckpt
https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth
out=GFPGAN/GFPGANv1.4.pth
https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth
out=RealESRGAN/RealESRGAN_x4plus.pth
https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth
out=RealESRGAN/RealESRGAN_x4plus_anime_6B.pth
https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1
out=LDSR/project.yaml
https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1
out=LDSR/model.ckpt
其中将 huggingface.co 更改为 hf-mirror.com,在 https://github.com 前添加 https://github.moeyy.xyz/,更改后的内容为:
https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt
out=Stable-diffusion/v1-5-pruned-emaonly.ckpt
https://hf-mirror.com/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.ckpt
out=VAE/vae-ft-mse-840000-ema-pruned.ckpt
https://hf-mirror.com/runwayml/stable-diffusion-inpainting/resolve/main/sd-v1-5-inpainting.ckpt
out=Stable-diffusion/sd-v1-5-inpainting.ckpt
https://github.moeyy.xyz/https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth
out=GFPGAN/GFPGANv1.4.pth
https://github.moeyy.xyz/https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth
out=RealESRGAN/RealESRGAN_x4plus.pth
https://github.moeyy.xyz/https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth
out=RealESRGAN/RealESRGAN_x4plus_anime_6B.pth
https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1
out=LDSR/project.yaml
https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1
out=LDSR/model.ckpt
说明:
要下载 Hugging Face 的内容,必须更换其网站;而 GitHub 的内容可以直接下载,尽管下载速度可能较慢。如果 GitHub 的镜像链接后续失效,也可以尝试使用原始地址下载。
若不想更换下载地址,也可以将链接放入浏览器中下载,下载完成后在 stable-diffusion-webui-docker/data/models 文件夹下以链接下方 out 的目录地址保存模型。例如 v1-5-pruned-emaonly.ckpt 保存的地址为 stable-diffusion-webui-docker/data/models/Stable-diffusion/v1-5-pruned-emaonly.ckpt,其他的以此类推即可。
2. 模型下载完成后可以看到如下内容(参考):
...
download-1 | Download Results:
download-1 | gid |stat|avg speed |path/URI
download-1 | ======+====+===========+=======================================================
download-1 | 075dc9|OK | 0B/s|/data/models/Stable-diffusion/v1-5-pruned-emaonly.ckpt
download-1 | 9c4025|OK | 0B/s|/data/models/VAE/vae-ft-mse-840000-ema-pruned.ckpt
download-1 | c84a90|OK | 0B/s|/data/models/Stable-diffusion/sd-v1-5-inpainting.ckpt
download-1 | f50c51|OK | 0B/s|/data/models/LDSR/project.yaml
download-1 | c5bab1|OK | 0B/s|/data/models/LDSR/model.ckpt
download-1 | d8b39c|OK | 16MiB/s|/data/models/RealESRGAN/RealESRGAN_x4plus.pth
download-1 | 867f11|OK | 651KiB/s|/data/models/RealESRGAN/RealESRGAN_x4plus_anime_6B.pth
download-1 | 911cd2|OK | 6.5MiB/s|/data/models/GFPGAN/GFPGANv1.4.pth
download-1 |
download-1 | Status Legend:
download-1 | (OK):download completed.
download-1 | Checking SHAs...
download-1 | /data/models/LDSR/project.yaml: OK
download-1 | /data/models/RealESRGAN/RealESRGAN_x4plus_anime_6B.pth: OK
download-1 | /data/models/RealESRGAN/RealESRGAN_x4plus.pth: OK
download-1 | /data/models/GFPGAN/GFPGANv1.4.pth: OK
download-1 | /data/models/VAE/vae-ft-mse-840000-ema-pruned.ckpt: OK
download-1 | /data/models/LDSR/model.ckpt: OK
download-1 | /data/models/Stable-diffusion/sd-v1-5-inpainting.ckpt: OK
download-1 | /data/models/Stable-diffusion/v1-5-pruned-emaonly.ckpt: OK
download-1 | By using this software, you agree to the following licenses:
download-1 | https://github.com/AbdBarho/stable-diffusion-webui-docker/blob/master/LICENSE
download-1 | https://github.com/CompVis/stable-diffusion/blob/main/LICENSE
download-1 | https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/master/LICENSE.txt
download-1 | https://github.com/invoke-ai/InvokeAI/blob/main/LICENSE
download-1 | And licenses of all UIs, third party libraries, and extensions.
download-1 exited with code 0
其中 Download Results 的 stat 列显示全部下载 OK,以及 Checking SHAs...之后的每一个模型 SHA 安全散列算法都是匹配的并显示 OK。
3. 现在,通过命令可以看到 Stable Diffusion WebUI 的 Docker 容器已经被创建了:
docker ps -a

#可以看到如下内容(参考)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f58c9a2903c webui-docker-download "/docker/download.sh" 25 minutes ago Exited (0) 24 minutes ago webui-docker-download-1

运行 Stable Diffusion WebUI

本指导的 WebUI 拥有两种 UI 界面:auto 和 comfy,这里分别介绍两种 UI 的安装。

auto UI

1. 该UI界面源自 GitHub AUTOMATIC1111/stable-diffusion-webui,首先需安装各种环境。
2. 打开 stable-diffusion-webui-docker/services/AUTOMATIC1111/Dockerfile,可以看到该文件包含多个 GitHub 链接。为了加快下载速度,可以将每个 GitHub 链接前面加上 GitHub 镜像网站,以使用国内镜像站点进行下载。此外,由于 Dockerfile 中包含 pip install -r requirements_versions.txt 命令,使用 pip 安装各种包可能会很慢,因此需要将其替换为国内镜像源。整体修改后的 Dockerfile 文件如下:
FROM alpine/git:2.36.2 as download
COPY clone.sh /clone.sh
RUN . /clone.sh stable-diffusion-webui-assets https://github.moeyy.xyz/https://github.com/AUTOMATIC1111/stable-diffusion-webui-assets.git 6f7db241d2f8ba7457bac5ca9753331f0c266917
RUN . /clone.sh stable-diffusion-stability-ai https://github.moeyy.xyz/https://github.com/Stability-AI/stablediffusion.git cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf \\
&& rm -rf assets data/**/*.png data/**/*.jpg data/**/*.gif
RUN . /clone.sh BLIP https://github.moeyy.xyz/https://github.com/salesforce/BLIP.git 48211a1594f1321b00f14c9f7a5b4813144b2fb9
RUN . /clone.sh k-diffusion https://github.moeyy.xyz/https://github.com/crowsonkb/k-diffusion.git ab527a9a6d347f364e3d185ba6d714e22d80cb3c
RUN . /clone.sh clip-interrogator https://github.moeyy.xyz/https://github.com/pharmapsychotic/clip-interrogator 2cf03aaf6e704197fd0dae7c7f96aa59cf1b11c9
RUN . /clone.sh generative-models https://github.moeyy.xyz/https://github.com/Stability-AI/generative-models 45c443b316737a4ab6e40413d7794a7f5657c19f
RUN . /clone.sh stable-diffusion-webui-assets https://github.moeyy.xyz/https://github.com/AUTOMATIC1111/stable-diffusion-webui-assets 6f7db241d2f8ba7457bac5ca9753331f0c266917
FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime
ENV DEBIAN_FRONTEND=noninteractive PIP_PREFER_BINARY=1
RUN --mount=type=cache,target=/var/cache/apt \\
apt-get update && \\
# we need those
apt-get install -y fonts-dejavu-core rsync git jq moreutils aria2 \\
# extensions needs those
ffmpeg libglfw3-dev libgles2-mesa-dev pkg-config libcairo2 libcairo2-dev build-essential
RUN pip install tb-nightly
RUN pip install --upgrade pip
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
ENV HF_ENDPOINT="https://hf-mirror.com"
WORKDIR /
RUN --mount=type=cache,target=/root/.cache/pip \\
git clone https://github.moeyy.xyz/https://github.com/AUTOMATIC1111/stable-diffusion-webui.git && \\
cd stable-diffusion-webui && \\
git reset --hard v1.9.4 && \\
pip install -r requirements_versions.txt
ENV ROOT=/stable-diffusion-webui
COPY --from=download /repositories/ ${ROOT}/repositories/
RUN mkdir ${ROOT}/interrogate && cp ${ROOT}/repositories/clip-interrogator/clip_interrogator/data/* ${ROOT}/interrogate
RUN --mount=type=cache,target=/root/.cache/pip \\
pip install pyngrok xformers==0.0.26.post1 \\
git+https://github.moeyy.xyz/https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 \\
git+https://github.moeyy.xyz/https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 \\
git+https://github.moeyy.xyz/https://github.com/mlfoundations/open_clip.git@v2.20.0
# there seems to be a memory leak (or maybe just memory not being freed fast enough) that is fixed by this version of malloc
# maybe move this up to the dependencies list.
RUN apt-get -y install libgoogle-perftools-dev && apt-get clean
ENV LD_PRELOAD=libtcmalloc.so
COPY . /docker
RUN \\
# mv ${ROOT}/style.css ${ROOT}/user.css && \\
# one of the ugliest hacks I ever wrote \\
sed -i 's/in_app_dir = .*/in_app_dir = True/g' /opt/conda/lib/python3.10/site-packages/gradio/routes.py && \\
git config --global --add safe.directory '*'
WORKDIR ${ROOT}
ENV NVIDIA_VISIBLE_DEVICES=all
ENV CLI_ARGS=""
EXPOSE 7860
ENTRYPOINT ["/docker/entrypoint.sh"]
CMD python -u webui.py --listen --port 7860 ${CLI_ARGS}
注意:
RUN pip install tb-nightly 命令应该位于 RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 之前。这是因为在将 pip 源更换为清华源之后安装 tb-nightly 会产生错误。因此,应首先使用原始的 pip 源安装 tb-nightly,然后再切换到清华源进行其他包的安装。
设置 ENV HF_ENDPOINT="https://hf-mirror.com" 是为了从 Hugging Face 下载 Tokenizer。由于使用原网站可能导致下载失败,因此需要替换为镜像网站。
3. 运行命令开始下载 UI 环境:
docker compose --profile auto up --build
此时会经历较长时间的等待并下载环境,完成后可以看到如下内容(参考):
...
auto-1 | Mounted .cache
auto-1 | Mounted config_states
auto-1 | mkdir: created directory '/stable-diffusion-webui/repositories/CodeFormer'
auto-1 | mkdir: created directory '/stable-diffusion-webui/repositories/CodeFormer/weights'
auto-1 | Mounted .cache
auto-1 | Mounted embeddings
auto-1 | Mounted config.json
auto-1 | Mounted models
auto-1 | Mounted styles.csv
auto-1 | Mounted ui-config.json
auto-1 | Mounted extensions
auto-1 | Installing extension dependencies (if any)
auto-1 | Calculating sha256 for /stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt: Running on local URL: http://0.0.0.0:7860
auto-1 |
auto-1 | To create a public link, set `share=True` in `launch()`.
auto-1 | Startup time: 5.0s (import torch: 1.8s, import gradio: 0.6s, setup paths: 0.9s, initialize shared: 0.2s, other imports: 0.5s, load scripts: 0.3s, create ui: 0.2s, add APIs: 0.3s).
auto-1 | c6bbc15e3224e6973459ba78de4998b80b50112b0ae5b5c67113d56b4e366b19
auto-1 | Loading weights [c6bbc15e32] from /stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt
auto-1 | Creating model from config: /stable-diffusion-webui/configs/v1-inpainting-inference.yaml
auto-1 | /opt/conda/lib/python3.10/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
auto-1 | warnings.warn(
auto-1 | Applying attention optimization: xformers... done.
auto-1 | Model loaded in 4.9s (calculate hash: 3.2s, load weights from disk: 0.7s, create model: 0.2s, apply weights to model: 0.3s, calculate empty prompt: 0.3s).
注意:
运行命令下载 UI 环境可能会由于网络环境问题导致多次失败,但已经下载好的会缓存在容器中,重新输入命令会从上次中断的地方开始下载,所以请反复尝试直到下载成功为止。
4. 同时,Auto 容器也已经被创建。新建一个窗口并输入以下命令:
docker ps
即可看到 auto UI的docker 容器已经被创建:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93abc03d6600 sd-auto:78 "/docker/entrypoint.…" 2 days ago Exited (137) 21 minutes ago 0.0.0.0:7860->7860/tcp, :::7860->7860/tcp webui-docker-auto-1
3f58c9a2903c webui-docker-download "/docker/download.sh" 4 days ago Exited (137) 3 days ago webui-docker-download-1
5. 本地打开浏览器(不是服务器端)输入网址 http://(服务器公网ip):7860/,即可看到 auto UI的Stable Diffusion WebUI 界面:

说明:
当使用公网 IP 访问服务器服务时,请确保将7860端口添加到安全组中以允许放行,从而使得7860端口上的服务可以通过公网被访问。
如果不想通过公网访问,想在本地浏览器里输入http://localhost:7860/直接访问 webui 界面,请通过 VSCode 连接上服务器之后,在终端的 PORTS 新添加7860端口服务,再打开浏览器即可看到 webui 服务(因为 VSCode 也有端口转发的功能)。

6. 这里我们演示如何使用 txt2img 生成图像。在提示栏中输入green sapling growing out of ground, mud, dirt, grass, high quality, photorealistic, sharp focus, depth of field,单击右侧的生成,即可根据给定的提示生成图像:

生成的图像如下:

7. 生成的图像同时也会保存在 stable-diffusion-webui-docker/output/txt2img/ 当前日期 /00000-1540340456.png 目录下,同时窗口会显示如下信息:
100% 20/20 [00:00<00:00, 20.90it/s]
Total progress: 100% 20/20 [00:00<00:00, 22.10it/s]
表示生成图像运行成功。

comfy UI

1. 此 UI 界面源自 GitHub comfyanonymous/ComfyUI,首先需要安装各种环境。
2. 打开 stable-diffusion-webui-docker/services/comfy/Dockerfile,可以看到该文件包含一条 GitHub 链接。为了加快下载速度,可以在该 GitHub 链接前加上 GitHub 镜像网站地址,将其转换为国内镜像网站下载。此外,由于 Dockerfile 中也包含 pip install -r requirements_versions.txt 命令,使用 pip 安装各种包可能会很慢,因此需要将其替换为国内镜像源。经过修改,整个 Dockerfile 文件如下所示:
FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime
ENV DEBIAN_FRONTEND=noninteractive PIP_PREFER_BINARY=1
RUN apt-get update && apt-get install -y git && apt-get clean
RUN pip install --upgrade pip
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
ENV ROOT=/stable-diffusion
RUN --mount=type=cache,target=/root/.cache/pip \\
git clone https://github.moeyy.xyz/https://github.com/comfyanonymous/ComfyUI.git ${ROOT} && \\
cd ${ROOT} && \\
git checkout master && \\
git reset --hard 276f8fce9f5a80b500947fb5745a4dde9e84622d && \\
pip install -r requirements.txt
WORKDIR ${ROOT}
COPY . /docker/
RUN chmod u+x /docker/entrypoint.sh && cp /docker/extra_model_paths.yaml ${ROOT}
ENV NVIDIA_VISIBLE_DEVICES=all PYTHONPATH="${PYTHONPATH}:${PWD}" CLI_ARGS=""
EXPOSE 7860
ENTRYPOINT ["/docker/entrypoint.sh"]
CMD python -u main.py --listen --port 7860 ${CLI_ARGS}
3. 运行命令开始下载 UI 环境:
docker compose --profile comfy up --build
此时同样会经历一定的等待时间并下载环境,完成后可以看到如下内容(参考):
...
Attaching to comfy-1
comfy-1 | mkdir: created directory '/data/config/comfy'
comfy-1 | mkdir: created directory '/data/config/comfy/custom_nodes'
comfy-1 | Mounted .cache
comfy-1 | mkdir: created directory '/output/comfy'
comfy-1 | Mounted comfy
comfy-1 | mkdir: created directory '/data/config/comfy/input'
comfy-1 | Mounted input
comfy-1 | Total VRAM 45386 MB, total RAM 189442 MB
comfy-1 | pytorch version: 2.3.0
comfy-1 | Set vram state to: NORMAL_VRAM
comfy-1 | Device: cuda:0 NVIDIA L40 : cudaMallocAsync
comfy-1 | VAE dtype: torch.bfloat16
comfy-1 | Using pytorch cross attention
comfy-1 | ****** User settings have been changed to be stored on the server instead of browser storage. ******
comfy-1 | ****** For multi-user setups add the --multi-user CLI argument to enable multiple user profiles. ******
comfy-1 | Adding extra search path checkpoints /data/models/Stable-diffusion
comfy-1 | Adding extra search path configs /data/models/Stable-diffusion
comfy-1 | Adding extra search path vae /data/models/VAE
comfy-1 | Adding extra search path loras /data/models/Lora
comfy-1 | Adding extra search path upscale_models /data/models/RealESRGAN
comfy-1 | Adding extra search path upscale_models /data/models/ESRGAN
comfy-1 | Adding extra search path upscale_models /data/models/SwinIR
comfy-1 | Adding extra search path upscale_models /data/models/GFPGAN
comfy-1 | Adding extra search path hypernetworks /data/models/hypernetworks
comfy-1 | Adding extra search path controlnet /data/models/ControlNet
comfy-1 | Adding extra search path gligen /data/models/GLIGEN
comfy-1 | Adding extra search path clip /data/models/CLIPEncoder
comfy-1 | Adding extra search path embeddings /data/embeddings
comfy-1 | Adding extra search path custom_nodes /data/config/comfy/custom_nodes
comfy-1 |
comfy-1 | Import times for custom nodes:
comfy-1 | 0.0 seconds: /stable-diffusion/custom_nodes/websocket_image_save.py
comfy-1 |
comfy-1 | Starting server
comfy-1 |
comfy-1 | To see the GUI go to: http://0.0.0.0:7860
4. 表明 UI 环境安装成功。同时 comfy 容器也已经被创建,新建一个窗口输入以下命令:
docker ps
即可看到 comfy UI的docker 容器已经被创建:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16cd64facfba sd-comfy:7 "/docker/entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:7860->7860/tcp, :::7860->7860/tcp webui-docker-comfy-1
93abc03d6600 sd-auto:78 "/docker/entrypoint.…" 2 days ago Exited (137) 21 minutes ago webui-docker-auto-1
3f58c9a2903c webui-docker-download "/docker/download.sh" 4 days ago Exited (137) 3 days ago webui-docker-download-1
5. 本地打开浏览器输入网址http://(服务器公网ip):7860/,即可看到 comfy UI的Stable Diffusion WebUI 界面:

说明:
与 auto UI 类似,如果您想通过 localhost 访问,请通过 VSCode 的 PORTS 功能添加7860端口进行端口转发服务,然后在本地运行 webui 服务。
6. 该 UI 界面的优势在于能够在生成图像时实时观察前向传播信号在各个模型模块之间的流动过程。单击右侧的 Queue Prompt,即可根据所提供的提示生成图像。

生成的图像如下:

7. 生成的图像同时也会保存在 stable-diffusion-webui-docker/output/comfy/ComfyUI_00001_.png 目录下,同时窗口会显示如下信息:
comfy-1 | got prompt
comfy-1 | model_type EPS
comfy-1 | Using pytorch attention in VAE
comfy-1 | Using pytorch attention in VAE
comfy-1 | Requested to load SD1ClipModel
comfy-1 | Loading 1 new model
comfy-1 | Requested to load BaseModel
comfy-1 | Loading 1 new model
100% 20/20 [00:00<00:00, 24.65it/s]
comfy-1 | Requested to load AutoencoderKL
comfy-1 | Loading 1 new model
comfy-1 | Prompt executed in 2.72 seconds
表示生成图像运行成功。

注意事项

说明:
由于 OpenCloudOS 是 TencentOS Server 的开源版本,理论上上述文档当中的所有操作同样适用于 OpenCloudOS。

参考文档