searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

自动化部署脚本(含数据库迁移)

2025-03-13 19:22:15
2
0

#!/bin/bash

# 配置参数
APP_NAME="my_flask_app"
APP_PORT=5000
NGINX_CONF="/etc/nginx/sites-available/$APP_NAME"
NGINX_CONF_ENABLED="/etc/nginx/sites-enabled/$APP_NAME"
APP_DIR="/var/www/$APP_NAME"
REPO_URL="https://github.com/yourusername/$APP_NAME.git"
DB_USER="root"
DB_PASSWORD="your_password"
DB_NAME="your_database"

# 安装依赖
sudo apt-get update
sudo apt-get install -y git python3 python3-pip nginx mysql-server mysql-client

# 克隆应用代码
if [ -d "$APP_DIR" ]; then
  echo "Application directory already exists. Pulling latest changes..."
  cd "$APP_DIR"
  git pull
else
  echo "Cloning application repository..."
  git clone "$REPO_URL" "$APP_DIR"
  cd "$APP_DIR"
fi

# 安装Python依赖
pip3 install -r requirements.txt

# 数据库迁移
echo "Running database migrations..."
python3 manage.py db upgrade

# 配置Nginx
cat <<EOF > "$NGINX_CONF"
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:$APP_PORT;
        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;
    }
}
EOF

# 启用Nginx配置
sudo ln -s "$NGINX_CONF" "$NGINX_CONF_ENABLED"
sudo systemctl restart nginx

# 启动Flask应用
nohup python3 app.py > /dev/null 2>&1 &

echo "Deployment complete. Your app is running at http://yourdomain.com"

脚本说明

  1. 安装依赖
    • 安装Git、Python、Pip、Nginx和MySQL。
  2. 克隆或更新应用代码
    • 如果应用目录已存在,则拉取最新代码;否则克隆仓库。
  3. 安装Python依赖
    • 使用pip3安装requirements.txt中列出的Python依赖。
  4. 数据库迁移
    • 假设你的Flask应用使用了Flask-Migrate进行数据库迁移。运行python3 manage.py db upgrade来应用数据库迁移。
  5. 配置Nginx
    • 配置Nginx以代理到Flask应用。
  6. 启动Flask应用
    • 在后台启动Flask应用。
0条评论
0 / 1000
王****际
180文章数
2粉丝数
王****际
180 文章 | 2 粉丝
原创

自动化部署脚本(含数据库迁移)

2025-03-13 19:22:15
2
0

#!/bin/bash

# 配置参数
APP_NAME="my_flask_app"
APP_PORT=5000
NGINX_CONF="/etc/nginx/sites-available/$APP_NAME"
NGINX_CONF_ENABLED="/etc/nginx/sites-enabled/$APP_NAME"
APP_DIR="/var/www/$APP_NAME"
REPO_URL="https://github.com/yourusername/$APP_NAME.git"
DB_USER="root"
DB_PASSWORD="your_password"
DB_NAME="your_database"

# 安装依赖
sudo apt-get update
sudo apt-get install -y git python3 python3-pip nginx mysql-server mysql-client

# 克隆应用代码
if [ -d "$APP_DIR" ]; then
  echo "Application directory already exists. Pulling latest changes..."
  cd "$APP_DIR"
  git pull
else
  echo "Cloning application repository..."
  git clone "$REPO_URL" "$APP_DIR"
  cd "$APP_DIR"
fi

# 安装Python依赖
pip3 install -r requirements.txt

# 数据库迁移
echo "Running database migrations..."
python3 manage.py db upgrade

# 配置Nginx
cat <<EOF > "$NGINX_CONF"
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:$APP_PORT;
        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;
    }
}
EOF

# 启用Nginx配置
sudo ln -s "$NGINX_CONF" "$NGINX_CONF_ENABLED"
sudo systemctl restart nginx

# 启动Flask应用
nohup python3 app.py > /dev/null 2>&1 &

echo "Deployment complete. Your app is running at http://yourdomain.com"

脚本说明

  1. 安装依赖
    • 安装Git、Python、Pip、Nginx和MySQL。
  2. 克隆或更新应用代码
    • 如果应用目录已存在,则拉取最新代码;否则克隆仓库。
  3. 安装Python依赖
    • 使用pip3安装requirements.txt中列出的Python依赖。
  4. 数据库迁移
    • 假设你的Flask应用使用了Flask-Migrate进行数据库迁移。运行python3 manage.py db upgrade来应用数据库迁移。
  5. 配置Nginx
    • 配置Nginx以代理到Flask应用。
  6. 启动Flask应用
    • 在后台启动Flask应用。
文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0