手把手教你从0开始在服务器上部署stable diffusion

12次阅读

StableDiffusion 服务器部署

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

  • 远程连接服务器工具 :VS Code

    https://code.visualstudio.com/Download

    • VS Code 插件 :Remote
  • 文件传输工具 FileZilla

    https://www.filezilla.cn/download

下载地址 :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 <<< # 根据自己的安装路径去修改 
  • 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 

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

git config --global url."https://hub.njuu.cf/".insteadOf https://github.com 
http://lib.zuotiyi.cn/tool/github.html # 从这里可以找其他镜像网站 

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

mkdir Project cd Project git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git cd stable-diffusion-webui 
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 
# stable-diffusion-webui/modules/paths_internal.py # 搜索 commandline_args # 将 commandline_args = os.environ.get('COMMANDLINE_ARGS', "") # 改为 commandline_args = os.environ.get('COMMANDLINE_ARGS', "--xformers") 
sudo apt-get install google-perftools 
https://civitai.com/api/download/models/130312 
将下载到的模型放入 stable-diffusion-webui/models/Stable-diffusion 
# 模型网站 https://civitai.com/ https://huggingface.co/ 
./webui.sh 

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

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

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

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

# 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", ) 
sudo apt install nginx -y 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 
nohup webui.sh & 

原文链接:https://blog.csdn.net/qq_44525568/article/details/132338630

正文完
 
追风者
版权声明:本站原创文章,由 追风者 2024-01-02发表,共计5836字。
转载说明:声明:本站内容均来自互联网,归原创作者所有,如有侵权必删除。 本站文章皆由CC-4.0协议发布。