Files
diary-system/deploy_cloud.sh
maoshen b273789ae8 Initial commit: 日记系统
完整的日记记录系统
- Django 后端 (diary app)
- 前端页面
- 部署脚本 (本地 + 云端)
- Nginx 配置
- 数据迁移和同步工具
2026-04-14 10:07:27 +00:00

125 lines
3.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 日记系统 - 云服务器部署脚本
# 在云服务器上执行bash deploy_cloud.sh
set -e
PROJECT_DIR="/home/ubuntu/diary-system"
BACKEND_DIR="$PROJECT_DIR/backend"
FRONTEND_DIR="$PROJECT_DIR/frontend"
echo "======================================"
echo "⚡ 码神日记系统 - 云服务器部署"
echo "======================================"
echo ""
cd "$PROJECT_DIR"
# 1. 安装依赖
echo "📦 安装 Python 依赖..."
cd "$BACKEND_DIR"
pip3 install -r requirements.txt -q 2>/dev/null || pip3 install -r requirements.txt --break-system-packages -q
# 2. 数据库迁移
echo "🗄️ 执行数据库迁移..."
python3 manage.py makemigrations diary --noinput 2>/dev/null || true
python3 manage.py migrate --noinput
# 3. 收集静态文件
echo "📁 收集静态文件..."
mkdir -p "$BACKEND_DIR/static"
python3 manage.py collectstatic --noinput
# 4. 创建 Gunicorn systemd 服务
echo "⚙️ 创建 Gunicorn 服务..."
sudo cat > /etc/systemd/system/diary-system.service << EOF
[Unit]
Description=Diary System Gunicorn Service
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=$BACKEND_DIR
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
sudo systemctl daemon-reload
sudo systemctl enable diary-system
sudo systemctl restart diary-system
# 5. 配置 Nginx
echo "🌐 配置 Nginx..."
sudo 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 $BACKEND_DIR/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 $FRONTEND_DIR;
index index.html;
try_files \$uri \$uri/ /index.html;
}
}
EOF
sudo ln -sf /etc/nginx/sites-available/diary-system /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
# 6. 同步日记数据
echo "📊 同步日记数据..."
cd "$PROJECT_DIR"
python3 sync_diary.py || echo " (首次部署,跳过日记同步)"
# 7. 检查服务状态
echo ""
echo "🔍 检查服务状态..."
sudo systemctl is-active diary-system > /dev/null && echo " ✅ Gunicorn 运行中" || echo " ❌ Gunicorn 未运行"
sudo systemctl is-active nginx > /dev/null && echo " ✅ Nginx 运行中" || echo " ❌ Nginx 未运行"
# 8. 测试访问
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://cssc.datalibstar.com:8001"
echo "📍 API: http://cssc.datalibstar.com:8001/api/"
echo "📍 Admin: http://cssc.datalibstar.com:8001/admin"
echo ""
echo "🎉 日记系统已在云服务器上线!"
echo ""