Files
chengshishouce/diary-system/deploy_to_cloud.sh

144 lines
4.0 KiB
Bash
Raw Normal View History

#!/bin/bash
# 日记系统 - 云服务器一键部署脚本
# 使用方法:在云服务器上执行 bash deploy_to_cloud.sh
set -e
SERVER_URL="cssc.datalibstar.com"
DEPLOY_PORT="8001"
echo "======================================"
echo "⚡ 码神日记系统 - 云服务器部署"
echo "======================================"
echo ""
# 检查是否在正确的服务器上
echo "🔍 检查服务器..."
if [ "$(hostname)" != "cssc" ] && [ "$(hostname)" != "cssc.datalibstar.com" ]; then
echo "⚠️ 警告:当前主机名不是 cssc请确认是否在正确的服务器上"
fi
# 1. 克隆或更新代码
echo "📦 获取代码..."
cd /root/.openclaw/workspace || {
echo "❌ 错误:/root/.openclaw/workspace 不存在"
exit 1
}
if [ -d "diary-system" ]; then
echo " 更新现有代码..."
cd diary-system
git pull 2>/dev/null || echo " (非 git 仓库,跳过)"
cd ..
else
echo " 代码已存在,跳过克隆"
fi
# 2. 安装依赖
echo "📦 安装 Python 依赖..."
cd /root/.openclaw/workspace/diary-system/backend
pip3 install -r requirements.txt --break-system-packages -q
# 3. 数据库迁移
echo "🗄️ 执行数据库迁移..."
python3 manage.py makemigrations diary --noinput 2>/dev/null || true
python3 manage.py migrate --noinput
# 4. 收集静态文件
echo "📁 收集静态文件..."
mkdir -p /root/.openclaw/workspace/diary-system/backend/static
python3 manage.py collectstatic --noinput
# 5. 创建 Gunicorn systemd 服务
echo "⚙️ 创建 Gunicorn 服务..."
cat > /etc/systemd/system/diary-system.service << EOF
[Unit]
Description=Diary System Gunicorn Service
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/.openclaw/workspace/diary-system/backend
ExecStart=/usr/bin/gunicorn --access-logfile - --workers 3 --bind 127.0.0.1:8002 diary_system.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable diary-system
systemctl restart diary-system
# 6. 配置 Nginx
echo "🌐 配置 Nginx..."
cat > /etc/nginx/sites-available/diary-system << 'EOF'
server {
listen 8001;
server_name _;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /root/.openclaw/workspace/diary-system/backend/static/;
}
location /api/ {
proxy_pass http://127.0.0.1:8002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /root/.openclaw/workspace/diary-system/frontend;
index index.html;
try_files $uri $uri/ /index.html;
}
}
EOF
ln -sf /etc/nginx/sites-available/diary-system /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
# 7. 同步日记数据
echo "📊 同步日记数据..."
cd /root/.openclaw/workspace/diary-system
python3 sync_diary.py
# 8. 检查服务状态
echo ""
echo "🔍 检查服务状态..."
systemctl is-active diary-system > /dev/null && echo " ✅ Gunicorn 运行中" || echo " ❌ Gunicorn 未运行"
systemctl is-active nginx > /dev/null && echo " ✅ Nginx 运行中" || echo " ❌ Nginx 未运行"
# 9. 测试访问
echo ""
echo "🧪 测试访问..."
sleep 2
if curl -s http://127.0.0.1:8001/ | grep -q "码神的日记系统"; then
echo " ✅ 前端访问正常"
else
echo " ⚠️ 前端访问可能有问题"
fi
if curl -s http://127.0.0.1:8001/api/entries/stats/ | grep -q "total_entries"; then
echo " ✅ API 访问正常"
else
echo " ⚠️ API 访问可能有问题"
fi
echo ""
echo "======================================"
echo "✅ 部署完成!"
echo "======================================"
echo ""
echo "📍 访问地址http://${SERVER_URL}:${DEPLOY_PORT}"
echo "📍 API: http://${SERVER_URL}:${DEPLOY_PORT}/api/"
echo "📍 Admin: http://${SERVER_URL}:${DEPLOY_PORT}/admin"
echo ""
echo "🎉 日记系统已在云服务器上线!"
echo ""