334 lines
8.6 KiB
Bash
Executable File
334 lines
8.6 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# iMeeting Docker 快速启动脚本
|
||
# 使用方法: ./start.sh [选项]
|
||
|
||
set -e
|
||
|
||
# 颜色定义
|
||
RED='\033[0;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
BLUE='\033[0;34m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# 打印带颜色的消息
|
||
print_info() {
|
||
echo -e "${BLUE}[INFO]${NC} $1"
|
||
}
|
||
|
||
print_success() {
|
||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||
}
|
||
|
||
print_warning() {
|
||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||
}
|
||
|
||
print_error() {
|
||
echo -e "${RED}[ERROR]${NC} $1"
|
||
}
|
||
|
||
# 打印banner
|
||
print_banner() {
|
||
echo -e "${BLUE}"
|
||
cat << "EOF"
|
||
_ __ __ _ _
|
||
(_) \/ | ___ ___| |_(_)_ __ __ _
|
||
| | |\/| |/ _ \/ _ \ __| | '_ \ / _` |
|
||
| | | | | __/ __/ |_| | | | | (_| |
|
||
|_|_| |_|\___|\___|\__|_|_| |_|\__, |
|
||
|___/
|
||
Docker Deployment Script
|
||
EOF
|
||
echo -e "${NC}"
|
||
}
|
||
|
||
# 检查依赖
|
||
check_dependencies() {
|
||
print_info "检查系统依赖..."
|
||
|
||
if ! command -v docker &> /dev/null; then
|
||
print_error "未安装 Docker,请先安装 Docker"
|
||
exit 1
|
||
fi
|
||
|
||
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
|
||
print_error "未安装 Docker Compose,请先安装 Docker Compose"
|
||
exit 1
|
||
fi
|
||
|
||
print_success "系统依赖检查通过"
|
||
}
|
||
|
||
# 检查环境变量文件
|
||
check_env_file() {
|
||
print_info "检查环境变量配置..."
|
||
|
||
if [ ! -f .env ]; then
|
||
print_warning ".env 文件不存在,从模板创建..."
|
||
cp .env.example .env
|
||
print_warning "请编辑 .env 文件,配置必要的参数(七牛云、LLM API等)"
|
||
print_warning "按任意键继续,或 Ctrl+C 退出..."
|
||
read -n 1 -s
|
||
else
|
||
print_success "环境变量文件已存在"
|
||
fi
|
||
}
|
||
|
||
# 检查并创建后端环境变量文件# 创建必要的目录
|
||
create_directories() {
|
||
print_info "创建必要的目录..."
|
||
|
||
# MySQL和Nginx配置目录
|
||
mkdir -p mysql/conf.d
|
||
mkdir -p nginx/conf.d
|
||
|
||
# 数据持久化目录
|
||
mkdir -p data/mysql
|
||
mkdir -p data/redis
|
||
mkdir -p data/uploads
|
||
mkdir -p data/logs/backend
|
||
mkdir -p data/logs/frontend
|
||
mkdir -p data/logs/nginx
|
||
|
||
# 后端配置和备份目录
|
||
mkdir -p backend/config
|
||
mkdir -p backups
|
||
|
||
print_success "目录创建完成"
|
||
}
|
||
|
||
# 生成Nginx配置
|
||
generate_nginx_config() {
|
||
print_info "检查Nginx配置..."
|
||
|
||
if [ ! -f nginx/nginx.conf ]; then
|
||
print_warning "nginx/nginx.conf 不存在,正在生成默认配置..."
|
||
cat > nginx/nginx.conf <<EOF
|
||
user nginx;
|
||
worker_processes auto;
|
||
|
||
error_log /var/log/nginx/error.log notice;
|
||
pid /var/run/nginx.pid;
|
||
|
||
events {
|
||
worker_connections 1024;
|
||
}
|
||
|
||
http {
|
||
include /etc/nginx/mime.types;
|
||
default_type application/octet-stream;
|
||
|
||
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
|
||
'\$status \$body_bytes_sent "\$http_referer" '
|
||
'"\$http_user_agent" "\$http_x_forwarded_for"';
|
||
|
||
access_log /var/log/nginx/access.log main;
|
||
|
||
sendfile on;
|
||
keepalive_timeout 65;
|
||
|
||
include /etc/nginx/conf.d/*.conf;
|
||
}
|
||
EOF
|
||
print_success "nginx/nginx.conf 已生成"
|
||
fi
|
||
|
||
if [ ! -f nginx/conf.d/default.conf ]; then
|
||
print_warning "nginx/conf.d/default.conf 不存在,正在生成默认站点配置..."
|
||
cat > nginx/conf.d/default.conf <<EOF
|
||
upstream frontend_server {
|
||
server frontend:80;
|
||
}
|
||
|
||
upstream backend_server {
|
||
server backend:8000;
|
||
}
|
||
|
||
server {
|
||
listen 80;
|
||
server_name localhost;
|
||
|
||
# 增加上传文件大小限制
|
||
client_max_body_size 100M;
|
||
|
||
# 后端API直连
|
||
location /api/ {
|
||
proxy_pass http://backend_server;
|
||
proxy_http_version 1.1;
|
||
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;
|
||
|
||
# 增加API超时时间
|
||
proxy_read_timeout 300s;
|
||
proxy_send_timeout 300s;
|
||
}
|
||
|
||
# 上传文件直连
|
||
location /uploads/ {
|
||
proxy_pass http://backend_server;
|
||
proxy_http_version 1.1;
|
||
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;
|
||
|
||
# 静态资源缓存配置
|
||
expires 30d;
|
||
add_header Cache-Control "public, no-transform";
|
||
}
|
||
|
||
# 前端代理 (其他请求)
|
||
location / {
|
||
proxy_pass http://frontend_server;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Upgrade \$http_upgrade;
|
||
proxy_set_header Connection 'upgrade';
|
||
proxy_set_header Host \$host;
|
||
proxy_cache_bypass \$http_upgrade;
|
||
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;
|
||
}
|
||
}
|
||
EOF
|
||
print_success "nginx/conf.d/default.conf 已生成"
|
||
fi
|
||
}
|
||
|
||
# 检查并创建后端环境变量文件
|
||
check_backend_env() {
|
||
print_info "检查后端环境变量配置..."
|
||
|
||
if [ ! -f backend/.env ]; then
|
||
print_warning "backend/.env 文件不存在,从模板创建..."
|
||
|
||
if [ -f backend/.env.example ]; then
|
||
cp backend/.env.example backend/.env
|
||
print_success "已创建 backend/.env"
|
||
else
|
||
print_warning "backend/.env.example 不存在,跳过"
|
||
fi
|
||
else
|
||
print_success "后端环境变量文件已存在"
|
||
fi
|
||
}
|
||
|
||
# 启动服务
|
||
start_services() {
|
||
print_info "启动 Docker 服务..."
|
||
|
||
# 使用 docker compose 或 docker-compose
|
||
if docker compose version &> /dev/null; then
|
||
COMPOSE_CMD="docker compose"
|
||
else
|
||
COMPOSE_CMD="docker-compose"
|
||
fi
|
||
|
||
$COMPOSE_CMD up -d
|
||
|
||
print_success "服务启动命令已执行"
|
||
}
|
||
|
||
# 等待服务健康
|
||
wait_for_health() {
|
||
print_info "等待服务启动..."
|
||
|
||
local max_wait=120
|
||
local waited=0
|
||
|
||
while [ $waited -lt $max_wait ]; do
|
||
if docker compose version &> /dev/null; then
|
||
COMPOSE_CMD="docker compose"
|
||
else
|
||
COMPOSE_CMD="docker-compose"
|
||
fi
|
||
|
||
local healthy_count=$($COMPOSE_CMD ps --format json 2>/dev/null | grep -c '"Health":"healthy"' || echo "0")
|
||
local total_count=5 # mysql, redis, backend, frontend, nginx
|
||
|
||
if [ "$healthy_count" -eq "$total_count" ]; then
|
||
print_success "所有服务已就绪"
|
||
return 0
|
||
fi
|
||
|
||
echo -ne "\r等待中... (${waited}s/${max_wait}s) 健康: ${healthy_count}/${total_count}"
|
||
sleep 5
|
||
waited=$((waited + 5))
|
||
done
|
||
|
||
echo ""
|
||
print_warning "服务启动超时,请手动检查状态: docker-compose ps"
|
||
return 1
|
||
}
|
||
|
||
# 显示服务状态
|
||
show_status() {
|
||
print_info "服务状态:"
|
||
|
||
if docker compose version &> /dev/null; then
|
||
docker compose ps
|
||
else
|
||
docker-compose ps
|
||
fi
|
||
}
|
||
|
||
# 显示访问信息
|
||
show_access_info() {
|
||
echo ""
|
||
print_success "==================================="
|
||
print_success " iMeeting 部署完成!"
|
||
print_success "==================================="
|
||
echo ""
|
||
echo -e "${GREEN}访问地址:${NC}"
|
||
echo -e " HTTP访问: ${BLUE}http://localhost${NC}"
|
||
echo -e " (或通过服务器IP访问:${BLUE}http://服务器IP${NC})"
|
||
echo -e " API文档: ${BLUE}http://localhost/docs${NC}"
|
||
echo ""
|
||
echo -e "${YELLOW}域名访问(HTTPS):${NC}"
|
||
echo -e " 需要在接入服务器配置Nginx代理"
|
||
echo -e " 参考文档: ${BLUE}GATEWAY_NGINX_CONFIG.md${NC}"
|
||
echo ""
|
||
echo -e "${YELLOW}数据目录:${NC}"
|
||
echo -e " 数据持久化: ${BLUE}./data/${NC}"
|
||
echo -e " - MySQL: ${BLUE}./data/mysql/${NC}"
|
||
echo -e " - Redis: ${BLUE}./data/redis/${NC}"
|
||
echo -e " - 上传文件: ${BLUE}./data/uploads/${NC}"
|
||
echo -e " - 日志: ${BLUE}./data/logs/${NC}"
|
||
echo ""
|
||
echo -e "${YELLOW}常用命令:${NC}"
|
||
echo -e " 管理菜单: ${BLUE}./manage.sh${NC}"
|
||
echo -e " 查看日志: ${BLUE}docker-compose logs -f${NC}"
|
||
echo -e " 停止服务: ${BLUE}./stop.sh${NC}"
|
||
echo -e " 重启服务: ${BLUE}docker-compose restart${NC}"
|
||
echo -e " 查看状态: ${BLUE}docker-compose ps${NC}"
|
||
echo ""
|
||
echo -e "${YELLOW}更多信息请查看: ${BLUE}DOCKER_DEPLOYMENT.md${NC}"
|
||
echo ""
|
||
}
|
||
|
||
# 主函数
|
||
main() {
|
||
print_banner
|
||
|
||
check_dependencies
|
||
check_env_file
|
||
check_backend_env
|
||
create_directories
|
||
generate_nginx_config
|
||
start_services
|
||
|
||
echo ""
|
||
wait_for_health
|
||
|
||
echo ""
|
||
show_status
|
||
show_access_info
|
||
}
|
||
|
||
# 执行主函数
|
||
main "$@"
|