ECS部署

0 阅读7分钟

ECS部署

中州养老系统

环境

线上部署
ecs服务,三台
CentOS Stream 9 64位

节点规划

  • node1 mysql redis
  • node2 后端
  • 前端nginx

mysql8.0部署

安装官方源与导入密钥
#安装官方源
dnf install -y https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
#导入密钥
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
mysql安装
#安装
dnf install -y mysql-community-server
启动
systemctl start mysqld
systemctl status mysqld
#设置自启动
systemctl enable mysqld
初始化
  1. 查看密码

    grep password /var/log/mysqld.log
    
  2. 登录配置

    mysql_secure_installation
    
    
     Securing the MySQL server deployment.
    
     Enter password for user root: #输入临时root密码
    
     The existing password for the user account root has expired. Please set a new password.
    
    
     The 'validate_password' component is installed on the server.
     The subsequent steps will run with the existing configuration
     of the component.
     Using existing password for root.
    
     New password: 
    
     Re-enter new password:
    
     Estimated strength of the password: 100
     Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
    
     New password:
    
     Re-enter new password:
     Estimated strength of the password: 100
     Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
     By default, a MySQL installation has an anonymous user,
     allowing anyone to log into MySQL without having to have
     a user account created for them. This is intended only for
     testing, and to make the installation go a bit smoother.
     You should remove them before moving into a production
     environment.
    
     Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
     Success.
    
    
     Normally, root should only be allowed to connect from
     'localhost'. This ensures that someone cannot guess at
     the root password from the network.
    
     Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
     By default, MySQL comes with a database named 'test' that
     anyone can access. This is also intended only for testing,
     and should be removed before moving into a production
     environment.
    
    
     Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
     - Dropping test database...
     Success.
    
     - Removing privileges on test database...
     Success.
    
     Reloading the privilege tables will ensure that all changes
     made so far will take effect immediately.
    
     Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
     Success.
    
     All done!
    
  3. 登录测试

    mysql -uroot -p
    

mysql导入数据

创建目录
mkdir -p /opt/zzyl/sql
上传数据文件到node1
scp  scp .\ry-zzyl.sql root@node1:/opt/zzyl/sql
导入
cd /opt/zzyl/sql/
mysql -uroot -pyoupwd < ry-zzyl.sql
查看验证
mysql -uroot -p
show databases;
use ry-zzyl;
show tables;

redis部署

安装
dnf install -y redis
修改配置
vim /etc/redis/redis.conf

#75行 所有服务都能连redis
bind * -::*
# 903行 修改cli密码
requirepass 123456
启动
systemctl start redis
systemctl status redis
systemctl enable redis
测试
redis-cli -a 123456
keys *

接入阿里云存储OSS

创建bugket

在这里插入图片描述

生成访问accessKey

在这里插入图片描述 在这里插入图片描述

修改配置文件

打开zzyl-admin.jar\BOOT-INF\classes\application-prod.yml

oss:
  #修改为自己的地域
  endpoint: oss-cn-guangzhou.aliyuncs.com
  #填入刚才的创建的key
  accessKeyId: yourKeyID
  accessKeySecret: yourAccessKeySecret
  #修改成自己的名字
  bucketName: your

修改完,重新打包即可

接入百度千帆模型

申请授权key

在这里插入图片描述

修改配置文件

打开zzyl-admin.jar\BOOT-INF\classes\application-prod.yml

baidu:
  accessKey: yourKey
  secretKey: youKey
  qianfanModel: ERNIE-4.0-8K-Preview

修改完,重新打包即可

后端部署

修改配置文件

打开zzyl-admin.jar\BOOT-INF\classes\application-prod.yml

#修改redis主机地址
redis:
    # 地址
    host: ecs内网ip
#mysql数据库信息修改
 # 主库数据源
      master:
        url: jdbc:mysql://ecs内网ip:3306/ry-zzyl?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: pwd
部署jdk11

node2上安装

解压
mkdir -p export/software
cd export/software/
tar -zxvf openjdk-11.0.0.2_linux-x64.tar.gz -C /opt/
配置环境变量
vim /etc/profile

export JAVA_HOME=/opt/jdk-11.0.0.2
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH

#重载
source /etc/profile

#测试
java -version
服务部署
上传服务包

上传zzyl-admin.jar到node2

kdir -p /opt/zzyl/backend
scp zzyl-admin.jar node2:/opt/zzyl/backend
配置mysql远程连接

node1上配置

 mysql -uroot -p
 #允许所有ip连接
 create User'root'@'%'IDENTIFIED WITH mysql_native_password BY '密码';
 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
 #刷新权限
 FLUSH PRIVILEGES;
启动服务
java -jar zzyl-admin.jar
安装screen

screen方便挂在后台运行

#先安装第三方库
dnf install -y epel-release

dnf install -y screen
screen操作
#创建一个窗口
screen -S zzyl-backend
java -jar zzyl-admin.jar
#ctrl+a 再按d 返回主窗口

#查看
screen -ls

部署nginx

node3上 部署

安装与启动
 dnf install -y nginx
 systemctl start nginx
 systemctl enable nginx
ecs安全组放行端口

在这里插入图片描述

测试 浏览器访问 http://ecs公网ip

前端部署

创建目录与上传文件
mkdir /var/www

#上传dist文件到www

scp -r .\\前端打包文件\\dist\\ root@node3:/var/www
配置nginx
http {
   ...
	# 在HTTP的目录下, 添加以下三个add_header   CORS 配置
	# 这几行配置用于设置 跨域资源共享(CORS),它们在 Nginx 中用来允许其他域名访问你的资源。CORS 是一种机制,允许通过浏览器发起跨域 HTTP 请求,常用于 Web 应用与不同域名的 API 交互时,解决浏览器的同源策略限制。
	add_header Access-Control-Allow-Origin *;
	add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
	add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization";
	
    server {
        listen       80;
        listen       [::]:80;
		# 这些配置项与 Nginx 处理客户端请求的请求体大小、请求头缓冲区等相关,主要用于控制 Nginx 在处理上传文件、请求体内容和请求头时的行为
      client_max_body_size 60m; # 限制客户端请求体的最大大小。
		client_body_buffer_size 512k; # 设置 Nginx 用于缓冲客户端请求体的内存大小。
		client_header_buffer_size 2k; # 设置 Nginx 用于缓冲请求头的内存大小。
      ...

		# 处理 静态页面
        location / {
                root   /var/www/dist;
                index  index.html index.htm;
                proxy_set_header   Upgrade          $http_upgrade;
                proxy_set_header   Connection       upgrade;
                try_files $uri $uri/ /index.html;
        }
        # 处理反向代理
        location /prod-api/ {
                proxy_pass http://更改为node2的私网IP地址:9000/;
                proxy_set_header   Upgrade          $http_upgrade;
                proxy_set_header   Connection       upgrade;
        }
    }
}
访问测试

浏览器:http://node3公网IP地址

问题

后端没有启动,导致验证码没有请求到 nginx配置问题,导致登录异常 浏览器缓存导致,无法重新登陆

大模型部署

创建一台阿里云的T4加速服务器

一下所有操作都在这台服务器上执行

安装GPU驱动

  1. 查看显卡信息
       #显卡是英伟达的直接过滤
       lspci | grep -i nvidia
       #查看驱动是否可用
       nvidia-smi 
    
  2. 下载驱动 是英伟达的显卡,下载开发版的驱动 驱动链接
    由于系统是centos9,选择红帽系列 在这里插入图片描述
  3. 安装
       #下载
       wget https://developer.download.nvidia.com/compute/cuda/13.3.0/local_installers/cuda_13.3.0_610.43.02_linux.run
       #安装
       sh cuda_13.3.0_610.43.02_linux.run
    
    在这里插入图片描述
  4. 添加环境变量
       #添加CUDA驱动
       echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
       echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
       source ~/.bashrc
    
  5. 验证
    nvidia-smi
    

在这里插入图片描述

安装Anaconda

由于python环境较多使用Anaconda部署

  1. 下载
       #网慢的可以在本地下载完在上传到服务器
       wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh
    
  2. 安装
       bash Anaconda3-2024.10-1-Linux-x86_64.sh
    

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

看到Thank you for installing Anaconda3!就安装完了 3. 没有初始界面解决方法

   cd ~ # 如果你不是root用户,切换到自己的家目录下
   vim .bashrc
   滚到文件的最后面。输入 i 进入插入模式,添加以下内容:
   # >>> conda initialize >>>
   # !! Contents within this block are managed by 'conda init' !!
   __conda_setup="$('/root/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
   if [ $? -eq 0 ]; then
   eval "$__conda_setup"
   else
   if [ -f "/root/anaconda3/etc/profile.d/conda.sh" ]; then
   . "/root/anaconda3/etc/profile.d/conda.sh"
   else
   export PATH="/root/anaconda3/bin:$PATH"
   fi
   fi
   unset __conda_setup
   # <<< conda initialize <<<
   添加后, 保存退出即可
  1. 重连服务器,看到(base) root就成功了

Anacoda虚拟环境创建

# 创建虚拟环境
conda create --name chatglm python=3.10
# 进入虚拟环境
conda activate chatglm
# 离开此虚拟环境
conda deactivate

下载chatglm-6b模型(知识库)

# 创建目录
mkdir -p /export/data/glm
# 切换目录
cd /export/data/glm
# 安装 国内模型资源平台
pip install -U huggingface_hub
# 设置模型资源地址
export HF_ENDPOINT=https://hf-mirror.com
# 下载对应模型内容
# 注意ssh的连接时间,断开可能导致下载失败
huggingface-cli download --resume-download THUDM/chatglm-6b-int4 --local-dir THUDM/chatglm-6b-int4

看到Successfully 就下载成功了

上传ChatGLM-6B代码与安装依赖

上传
scp -r ./ChatGLM-6B root@node4:/export/data/glm
安装依赖
cd /export/data/glm/ChatGLM-6B
pip install -r requirements.txt -i https://mirror.sjtu.edu.cn/pypi/web/simple

修改api.py的模型路径

vim /export/data/glm/ChatGLM-6B/api.py
# 增加一行内容
model_path = "/export/data/glm/THUDM/chatglm-6b-int4"
# 修改部分参数
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda()
model = model.eval()

运行测试

#安装一个额外的依赖
pip install fastapi uvicorn
#跑模型
python api.py

#测试
curl -X POST "http://127.0.0.1:8000" -H 'Content-Type: application/json' -d '{"prompt": "你好", "history": []}'
```sh
model_path = "/export/data/glm/THUDM/chatglm-6b-int4"
# 修改部分参数
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda()
model = model.eval()