tg-channel-bot/deploy/setup.sh

66 lines
1.9 KiB
Bash
Executable File
Raw Permalink 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
# ============================================
# Telegram Channel Bot - 宝塔服务器部署脚本
# ============================================
set -e
APP_DIR="/www/wwwroot/tg-channel-bot"
SERVICE_NAME="tg-channel-bot"
echo "=========================================="
echo " Telegram Channel Bot 部署脚本"
echo "=========================================="
# 1. 创建项目目录
echo "[1/6] 创建项目目录..."
mkdir -p ${APP_DIR}/logs
# 2. 复制项目文件(假设已上传到 APP_DIR
echo "[2/6] 检查项目文件..."
if [ ! -f "${APP_DIR}/bot.py" ]; then
echo "错误: 请先将项目文件上传到 ${APP_DIR}"
exit 1
fi
# 3. 创建虚拟环境并安装依赖
echo "[3/6] 创建 Python 虚拟环境..."
cd ${APP_DIR}
python3 -m venv venv
${APP_DIR}/venv/bin/pip install -r requirements.txt
# 4. 配置环境变量
echo "[4/6] 配置环境变量..."
if [ ! -f "${APP_DIR}/.env" ]; then
cp ${APP_DIR}/.env.example ${APP_DIR}/.env
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo " 请编辑 ${APP_DIR}/.env 填入 BOT_TOKEN"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo ""
fi
# 5. 设置权限
echo "[5/6] 设置文件权限..."
chown -R www:www ${APP_DIR}
chmod 600 ${APP_DIR}/.env
# 6. 安装并启动 systemd 服务
echo "[6/6] 配置 systemd 服务..."
cp ${APP_DIR}/deploy/${SERVICE_NAME}.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
echo ""
echo "=========================================="
echo " 部署完成!"
echo "=========================================="
echo ""
echo "常用命令:"
echo " 查看状态: systemctl status ${SERVICE_NAME}"
echo " 查看日志: tail -f ${APP_DIR}/logs/bot.log"
echo " 重启服务: systemctl restart ${SERVICE_NAME}"
echo " 停止服务: systemctl stop ${SERVICE_NAME}"
echo ""