StableDiffusion 服务器部署

0. 服务器

GPU A5000-24G 数量: 1 显存: 24 GB

CPU AMD EPYC 7551P 核心: 8 核

实例内存: 63G

系统 Ubuntu20.04

验证是否有nvidia驱动

nvidia-smi

如果没有显示出显卡信息(如下)

+---------------------------------------------------------------------------------------+

| NVIDIA-SMI 530.30.02 Driver Version: 530.30.02 CUDA Version: 12.1 |

|-----------------------------------------+----------------------+----------------------+

| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |

| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |

| | | MIG M. |

|=========================================+======================+======================|

| 0 NVIDIA RTX A5000 On | 00000000:06:00.0 Off | Off |

| 30% 33C P8 15W / 230W| 1MiB / 24564MiB | 0% Default |

| | | N/A |

+-----------------------------------------+----------------------+----------------------+

则需要参考ubuntu安装nvidia驱动

https://blog.csdn.net/Perfect886/article/details/119109380

1. 使用工具

远程连接服务器工具:VS Code https://code.visualstudio.com/Download

VS Code 插件:Remote 文件传输工具 FileZilla https://www.filezilla.cn/download

2. 装python环境

下载地址:https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

./Miniconda3-latest-Linux-x86_64.sh

# 一路回车+yes

在.bashrc中添加以下内容

# >>> conda initialize >>>

# !! Contents within this block are managed by 'conda init' !!

__conda_setup="$('/usr/local/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"

if [ $? -eq 0 ]; then

eval "$__conda_setup"

else

if [ -f "/usr/local/miniconda3/etc/profile.d/conda.sh" ]; then

. "/usr/local/miniconda3/etc/profile.d/conda.sh"

else

export PATH="/usr/local/miniconda3/bin:$PATH"

fi

fi

unset __conda_setup

# <<< conda initialize <<<

# 根据自己的安装路径去修改

3. 设置Conda、PIP国内源

codna

touch .condarc # 如果有了就不用了

写入以下内容

channels:

- defaults

show_channel_urls: true

auto_activate_base: true

channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda

default_channels:

- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r

custom_channels:

conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

pip

mkdir .pip

cd .pip

touch pip.conf

写入以下内容

[global]

index-url = https://pypi.tuna.tsinghua.edu.cn/simple

4. 设置git加速

该方案为使用镜像网站加速git代码的下载

git config --global url."https://hub.njuu.cf/".insteadOf https://github.com

http://lib.zuotiyi.cn/tool/github.html # 从这里可以找其他镜像网站

注意,镜像网站可能存在滞后

5. 拉取代码

mkdir Project

cd Project

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

cd stable-diffusion-webui

6. 创建python虚拟环境并下载依赖包

conda create -n sd python=3.11 -y

conda activate sd

# 优先安装torch会避免很多麻烦

pip install torch torchvision torchaudio

pip install -r requirements.txt

pip install xformers

可能遇到的问题

在执行的时候可能会卡在安装 basicsr 这里,可以尝试进行以下操作

# 1.下载 tb_nightly

https://files.pythonhosted.org/packages/c0/1c/4d95416f1d9c8bab3d2c3271642084a9a93324a85b1b897ef44fb05ab734/tb_nightly-2.15.0a20230816-py3-none-any.whl

# 2. 安装tb_nightly

pip install tb_nightly-2.15.0a20230816-py3-none-any.whl

# 3.拉取basicsr源码

git clone https://github.com/XPixelGroup/BasicSR.git

# 4. 安装basicsr

cd BasicSR

pip install -r requirements.txt

python setup.py install

单独安装完basicsr后再执行

pip install -r requirements.txt

出现 ImportError: libGL.so.1: cannot open shared object file: No such file or directory

apt install libgl1-mesa-glx

7. 修改webui.sh

使用现有conda环境而不是新建一个环境 在webui.sh中搜索 use_venv

use_venv=1 (默认) -> use_venv=0

允许root用户运行 在webui.sh中搜索 can_run_as_root

can_run_as_root=0 (默认) -> can_run_as_root=1

8. 修改 paths_internal.py

# stable-diffusion-webui/modules/paths_internal.py

# 搜索 commandline_args

# 将

commandline_args = os.environ.get('COMMANDLINE_ARGS', "")

# 改为

commandline_args = os.environ.get('COMMANDLINE_ARGS', "--xformers")

9. 安装TCMalloc

sudo apt-get install google-perftools

10. 下载一个模型到指定路径

https://civitai.com/api/download/models/130312

将下载到的模型放入stable-diffusion-webui/models/Stable-diffusion

# 模型网站

https://civitai.com/

https://huggingface.co/

11. 运行webui.sh

./webui.sh

正常启动后,你就能看到vs code贴心的帮你转发了127.0.0.1:7860,通过vs code点击这个链接,你就能看到SD的页面了

完成以上操作就可以给自己用了

好了,可以Ctrl+c关掉程序了

下面的操作是用来给别人用的

12. 修改 webui.py

# webui.py中搜索 api.launch

# 将

api.launch(

server_name="0.0.0.0" if cmd_opts.listen else "127.0.0.1",

port=cmd_opts.port if cmd_opts.port else 7861,

root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else ""

)

# 修改为

api.launch(

server_name="127.0.0.1",

port=18101, # 自己选一个端口

root_path="/StableDiffusion/Web"

)

# webui.py中搜索 shared.demo.launch

# 将

app, local_url, share_url = shared.demo.launch(

share=cmd_opts.share,

server_name=server_name,

server_port=cmd_opts.port,

ssl_keyfile=cmd_opts.tls_keyfile,

ssl_certfile=cmd_opts.tls_certfile,

ssl_verify=cmd_opts.disable_tls_verify,

debug=cmd_opts.gradio_debug,

auth=gradio_auth_creds,

inbrowser=cmd_opts.autolaunch and os.getenv('SD_WEBUI_RESTARTING') != '1',

prevent_thread_lock=True,

allowed_paths=cmd_opts.gradio_allowed_path,

app_kwargs={

"docs_url": "/docs",

"redoc_url": "/redoc",

},

root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else "",

)

# 修改为

app, local_url, share_url = shared.demo.launch(

share=cmd_opts.share,

server_name="127.0.0.1",

server_port=18101,

ssl_keyfile=cmd_opts.tls_keyfile,

ssl_certfile=cmd_opts.tls_certfile,

ssl_verify=cmd_opts.disable_tls_verify,

debug=cmd_opts.gradio_debug,

auth=gradio_auth_creds,

inbrowser=cmd_opts.autolaunch and os.getenv('SD_WEBUI_RESTARTING') != '1',

prevent_thread_lock=True,

allowed_paths=cmd_opts.gradio_allowed_path,

app_kwargs={

"docs_url": "/docs",

"redoc_url": "/redoc",

},

root_path="/StableDiffusion/Web",

)

13. 安装Nginx

sudo apt install nginx -y

nginx

14. 设置Nginx转发

把配置文件软链到自己目录下好修改

cd ~

ln -s /etc/nginx/ Nginx

# 打开Nginx下的nginx.conf

# 搜索 include /etc/nginx/sites-enabled/*;

# 在这行下添加

include /etc/nginx/myHost/*.conf;

mkdir myHost

cd myHost

touch StableDiffusion.conf

# 在StableDiffusion.conf中写入如下内容

server {

listen 8080;

server_name example.com www.example.com; # Change this to your domain name

location /StableDiffusion/Web/ { # Change this if you'd like to server your Gradio app on a different path

proxy_pass http://127.0.0.1:18101/; # Change this if your Gradio app will be running on a different port

proxy_redirect off;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_set_header Host $host;

}

}

# 验证配置是否正确

nginx -t

# 重启nginx

nginx -s reload

15. 后台启动webui

nohup webui.sh &

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: