Files
chengshishouce/city-manual/scripts/first-deploy.sh
maoshen 801da8a5e1 scripts: 添加首次部署脚本
- 新增 first-deploy.sh 用于首次部署到云服务器
- 自动克隆项目、配置环境、创建服务
- 支持 Ubuntu 用户和 sudo 权限
2026-04-12 21:56:05 +00:00

143 lines
3.7 KiB
Bash
Executable File
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
# 城市手册 - 首次部署到云服务器
# 用法:./first-deploy.sh
set -e
SERVER_HOST="cssc.datalibstar.com"
SERVER_USER="ubuntu"
SERVER_PASS="825670@MashenClaw"
echo "🚀 开始首次部署城市手册到云服务器..."
echo "服务器:$SERVER_USER@$SERVER_HOST"
echo ""
# 检查 sshpass
if ! command -v sshpass &> /dev/null; then
echo "❌ 错误sshpass 未安装"
echo "请运行sudo apt install sshpass"
exit 1
fi
# 执行远程部署
sshpass -p "$SERVER_PASS" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST << 'ENDSSH'
set -e
echo "📦 1. 克隆项目..."
cd /home/ubuntu
git clone http://10.2.0.100:8989/mashen/chengshishouce.git city-manual
cd /home/ubuntu/city-manual/city-manual
echo "🐍 2. 创建 Python 虚拟环境..."
cd backend
python3 -m venv venv
source venv/bin/activate
echo "📦 3. 安装依赖..."
pip install -r requirements.txt -q
pip install gunicorn -q
echo "🗄️ 4. 配置环境变量..."
cd /home/ubuntu/city-manual/city-manual
cat > .env << EOF
DJANGO_SETTINGS_MODULE=config.settings.production
DJANGO_SECRET_KEY=cssc-secret-key-$(date +%s)
DEBUG=False
ALLOWED_HOSTS=cssc.datalibstar.com,127.0.0.1,localhost
DATABASE_URL=postgres://coder:825670wl@10.2.0.100:5432/cssc
MEDIA_ROOT=/home/ubuntu/city-manual/city-manual/backend/media
STATIC_ROOT=/home/ubuntu/city-manual/city-manual/backend/static
EOF
echo "📦 5. 数据库迁移..."
cd backend
source ../venv/bin/activate
python manage.py migrate --noinput
python manage.py collectstatic --noinput
echo "⚙️ 6. 创建 Gunicorn systemd 服务..."
sudo cat > /etc/systemd/system/city-manual.service << 'EOF'
[Unit]
Description=City Manual Gunicorn Service
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/city-manual/city-manual/backend
ExecStart=/home/ubuntu/city-manual/city-manual/backend/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/city-manual.sock \
city_manual.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable city-manual
sudo systemctl start city-manual
echo "🌐 7. 配置 Nginx..."
sudo cat > /etc/nginx/sites-available/city-manual << 'EOF'
server {
listen 80;
server_name cssc.datalibstar.com;
access_log /var/log/nginx/city-manual-access.log;
error_log /var/log/nginx/city-manual-error.log;
location /static/ {
alias /home/ubuntu/city-manual/city-manual/backend/static/;
expires 30d;
add_header Cache-Control "public, immutable";
}
location /media/ {
alias /home/ubuntu/city-manual/city-manual/backend/media/;
expires 7d;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/city-manual.sock;
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;
}
client_max_body_size 10M;
}
EOF
sudo ln -sf /etc/nginx/sites-available/city-manual /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
echo "🔥 8. 配置防火墙..."
sudo ufw allow 80/tcp || echo "防火墙配置跳过"
echo ""
echo "✅ 部署完成!"
echo ""
echo "📍 访问地址http://cssc.datalibstar.com"
echo "📍 Admin: http://cssc.datalibstar.com/admin"
echo "📍 测试账号demo / demo123"
echo ""
echo "📊 服务状态:"
sudo systemctl status city-manual --no-pager | head -5
sudo systemctl status nginx --no-pager | head -5
ENDSSH
if [ $? -eq 0 ]; then
echo ""
echo "🎉 部署成功!"
else
echo ""
echo "❌ 部署失败,请检查错误信息"
exit 1
fi