install_arm_ubuntu.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/bin/bash
  2. # 一键安装脚本 - ARM Ubuntu版本
  3. echo "========================================"
  4. echo " DZXJ DTU 应用一键安装脚本 (ARM Ubuntu) "
  5. echo "========================================"
  6. # 设置颜色输出
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[1;33m'
  10. NC='\033[0m' # No Color
  11. # 检查是否在ARM平台
  12. if [[ $(uname -m) != "aarch64" && $(uname -m) != "armv7l" && $(uname -m) != "armv8l" ]]; then
  13. echo -e "${RED}错误: 此安装脚本仅适用于ARM架构的Ubuntu系统${NC}"
  14. echo -e "当前系统架构: $(uname -m)"
  15. exit 1
  16. fi
  17. # 检查是否是Ubuntu系统
  18. if ! command -v lsb_release &> /dev/null || [[ ! $(lsb_release -i 2>/dev/null) =~ Ubuntu ]]; then
  19. echo -e "${YELLOW}警告: 此脚本针对Ubuntu系统优化,您的系统可能不受完全支持${NC}"
  20. read -p "是否继续安装? (y/n): " -n 1 -r
  21. echo
  22. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  23. exit 1
  24. fi
  25. fi
  26. # 检查是否为root用户并调整安装目录
  27. if [ "$EUID" -eq 0 ]; then
  28. echo -e "${YELLOW}警告: 正在以root用户运行脚本${NC}"
  29. # 为root用户设置不同的安装目录
  30. INSTALL_DIR="/opt/dzxj_dtu"
  31. echo -e "${GREEN}将使用 /opt/dzxj_dtu 作为安装目录${NC}"
  32. else
  33. INSTALL_DIR="$HOME/dzxj_dtu"
  34. fi
  35. # 创建安装目录
  36. INSTALL_DIR="$HOME/dzxj_dtu"
  37. if [ -d "$INSTALL_DIR" ]; then
  38. echo -e "${YELLOW}警告: 安装目录已存在$INSTALL_DIR${NC}"
  39. echo -e "${GREEN}自动覆盖现有安装目录...${NC}"
  40. rm -rf "$INSTALL_DIR"
  41. fi
  42. mkdir -p "$INSTALL_DIR"
  43. echo -e "${GREEN}创建安装目录: $INSTALL_DIR${NC}"
  44. # 配置apt-get使用阿里源
  45. echo -e "${GREEN}配置apt-get使用阿里源...${NC}"
  46. ARCH=$(dpkg --print-architecture)
  47. CODENAME=$(lsb_release -cs)
  48. # 备份原有源文件
  49. if [ ! -f /etc/apt/sources.list.bak ]; then
  50. sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  51. echo -e "${GREEN}已备份原有源文件到 /etc/apt/sources.list.bak${NC}"
  52. fi
  53. # 配置阿里源
  54. echo -e "${GREEN}设置阿里源...${NC}"
  55. sudo tee /etc/apt/sources.list > /dev/null << EOF
  56. deb http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME main restricted universe multiverse
  57. deb-src http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME main restricted universe multiverse
  58. deb http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-security main restricted universe multiverse
  59. deb-src http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-security main restricted universe multiverse
  60. deb http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-updates main restricted universe multiverse
  61. deb-src http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-updates main restricted universe multiverse
  62. deb http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-backports main restricted universe multiverse
  63. deb-src http://mirrors.aliyun.com/ubuntu-ports/ $CODENAME-backports main restricted universe multiverse
  64. EOF
  65. # 安装系统依赖
  66. echo -e "${GREEN}安装系统依赖...${NC}"
  67. sudo apt-get update
  68. # 检查并安装nginx
  69. echo -e "${GREEN}检查nginx安装状态...${NC}"
  70. if ! command -v nginx &> /dev/null; then
  71. echo -e "${YELLOW}nginx未安装,正在安装...${NC}"
  72. sudo apt-get install -y nginx
  73. if [ $? -eq 0 ]; then
  74. echo -e "${GREEN}nginx安装成功${NC}"
  75. else
  76. echo -e "${RED}错误: nginx安装失败${NC}"
  77. exit 1
  78. fi
  79. else
  80. echo -e "${GREEN}nginx已安装${NC}"
  81. fi
  82. # 安装其他依赖
  83. sudo apt-get install -y --no-install-recommends \
  84. python3 python3-venv python3-pip python3-dev \
  85. gcc libffi-dev make \
  86. git curl \
  87. net-tools iproute2 \
  88. mosquitto mosquitto-clients
  89. if [ $? -ne 0 ]; then
  90. echo -e "${RED}错误: 系统依赖安装失败${NC}"
  91. exit 1
  92. fi
  93. # 设置npm镜像源为阿里源以加速(如果需要在目标机器上构建)
  94. # echo -e "${GREEN}配置npm镜像源...${NC}"
  95. # npm config set registry https://registry.npmmirror.com --global
  96. # 复制项目文件到安装目录
  97. echo -e "${GREEN}复制项目文件...${NC}"
  98. cp -r "$(pwd)/backend" "$INSTALL_DIR/"
  99. # 复制前端预编译的dist目录
  100. echo -e "${GREEN}复制前端预编译文件...${NC}"
  101. mkdir -p "$INSTALL_DIR/frontend"
  102. # 检查是否存在dist目录
  103. if [ -d "$(pwd)/frontend/dist" ]; then
  104. cp -r "$(pwd)/frontend/dist" "$INSTALL_DIR/frontend/" || echo -e "${YELLOW}警告: 复制frontend/dist失败${NC}"
  105. echo -e "${GREEN}已复制前端预编译文件到 $INSTALL_DIR/frontend/dist${NC}"
  106. else
  107. echo -e "${YELLOW}警告: 未找到前端预编译的dist目录${NC}"
  108. echo -e "${YELLOW}请先在本地执行 npm install && npm run build 构建前端${NC}"
  109. fi
  110. # 创建Python虚拟环境
  111. echo -e "${GREEN}创建Python虚拟环境...${NC}"
  112. python3 -m venv "$INSTALL_DIR/venv"
  113. # 激活虚拟环境
  114. source "$INSTALL_DIR/venv/bin/activate"
  115. # 升级pip并设置镜像源
  116. pip install --upgrade pip
  117. pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
  118. # 安装Python依赖
  119. echo -e "${GREEN}安装Python依赖...${NC}"
  120. pip install --no-cache-dir -r "$INSTALL_DIR/backend/requirements.txt"
  121. if [ $? -ne 0 ]; then
  122. echo -e "${RED}错误: Python依赖安装失败${NC}"
  123. exit 1
  124. fi
  125. # 配置nginx部署前端
  126. echo -e "${GREEN}配置nginx部署前端...${NC}"
  127. # 创建nginx配置文件
  128. NGINX_CONF="dzxj_dtu.conf"
  129. sudo tee "/etc/nginx/sites-available/$NGINX_CONF" > /dev/null << EOF
  130. server {
  131. listen 80;
  132. server_name localhost;
  133. # 前端静态文件目录
  134. root $INSTALL_DIR/frontend/dist;
  135. index index.html;
  136. location / {
  137. try_files \$uri \$uri/ /index.html;
  138. }
  139. # 代理后端API请求
  140. location /api {
  141. proxy_pass http://localhost:5001/api;
  142. proxy_set_header Host \$host;
  143. proxy_set_header X-Real-IP \$remote_addr;
  144. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  145. proxy_set_header X-Forwarded-Proto \$scheme;
  146. }
  147. # WebSocket连接代理
  148. location /socket.io {
  149. proxy_pass http://localhost:5001/socket.io;
  150. proxy_http_version 1.1;
  151. proxy_set_header Upgrade \$http_upgrade;
  152. proxy_set_header Connection 'upgrade';
  153. proxy_set_header Host \$host;
  154. proxy_cache_bypass \$http_upgrade;
  155. }
  156. }
  157. EOF
  158. # 启用nginx配置
  159. if [ -f /etc/nginx/sites-enabled/default ]; then
  160. sudo rm /etc/nginx/sites-enabled/default
  161. echo -e "${GREEN}已移除默认nginx配置${NC}"
  162. fi
  163. sudo ln -sf /etc/nginx/sites-available/$NGINX_CONF /etc/nginx/sites-enabled/
  164. echo -e "${GREEN}已启用nginx配置: $NGINX_CONF${NC}"
  165. # 测试nginx配置并重启
  166. sudo nginx -t
  167. if [ $? -eq 0 ]; then
  168. sudo systemctl restart nginx
  169. echo -e "${GREEN}nginx服务已重启,前端部署成功${NC}"
  170. else
  171. echo -e "${RED}错误: nginx配置有误,请检查${NC}"
  172. fi
  173. # 创建启动脚本
  174. echo -e "${GREEN}创建启动脚本...${NC}"
  175. cat > "$INSTALL_DIR/start.sh" << 'EOF'
  176. #!/bin/bash
  177. # 启动脚本
  178. RED='\033[0;31m'
  179. GREEN='\033[0;32m'
  180. NC='\033[0m'
  181. echo -e "${GREEN}启动DZXJ DTU应用...${NC}"
  182. # 确保脚本在正确的目录下执行
  183. cd "$(dirname "$0")"
  184. # 激活虚拟环境
  185. source "venv/bin/activate"
  186. # 设置环境变量
  187. export FLASK_APP=app.py
  188. export FLASK_ENV=production
  189. export TZ=Asia/Shanghai
  190. # 进入后端目录
  191. cd backend
  192. # 检查端口是否已被占用
  193. if lsof -Pi :5001 -sTCP:LISTEN -t >/dev/null ; then
  194. echo -e "${YELLOW}警告: 端口5001已被占用${NC}"
  195. read -p "是否继续启动? (y/n): " -n 1 -r
  196. echo
  197. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  198. exit 1
  199. fi
  200. fi
  201. echo -e "${GREEN}应用启动中,访问地址: http://localhost:5001${NC}"
  202. # 启动应用
  203. python -m flask run --host=0.0.0.0 --port=5001
  204. EOF
  205. # 创建停止脚本
  206. echo -e "${GREEN}创建停止脚本...${NC}"
  207. cat > "$INSTALL_DIR/stop.sh" << 'EOF'
  208. #!/bin/bash
  209. # 停止脚本
  210. RED='\033[0;31m'
  211. GREEN='\033[0;32m'
  212. NC='\033[0m'
  213. echo -e "${GREEN}停止DZXJ DTU应用...${NC}"
  214. # 查找并停止应用进程
  215. PIDS=$(lsof -Pi :5001 -sTCP:LISTEN -t)
  216. if [ -n "$PIDS" ]; then
  217. echo -e "${GREEN}停止进程: $PIDS${NC}"
  218. kill -15 $PIDS
  219. # 等待进程停止
  220. for pid in $PIDS; do
  221. echo -e "${GREEN}等待进程 $pid 停止...${NC}"
  222. wait $pid 2>/dev/null
  223. done
  224. echo -e "${GREEN}应用已停止${NC}"
  225. else
  226. echo -e "${YELLOW}没有发现运行中的DZXJ DTU应用${NC}"
  227. fi
  228. EOF
  229. # 创建systemd服务配置(可选)
  230. echo -e "${GREEN}创建systemd服务配置...${NC}"
  231. # 根据运行用户设置适当的配置
  232. if [ "$EUID" -eq 0 ]; then
  233. # root用户的服务配置
  234. cat > "$INSTALL_DIR/dzxj_dtu.service" << EOF
  235. [Unit]
  236. Description=DZXJ DTU Application
  237. After=network.target
  238. [Service]
  239. User=root
  240. WorkingDirectory=$INSTALL_DIR/backend
  241. Environment="FLASK_APP=app.py"
  242. Environment="FLASK_ENV=production"
  243. Environment="TZ=Asia/Shanghai"
  244. ExecStart=$INSTALL_DIR/venv/bin/python -m flask run --host=0.0.0.0 --port=5001
  245. Restart=on-failure
  246. RestartSec=5
  247. [Install]
  248. WantedBy=multi-user.target
  249. EOF
  250. else
  251. # 普通用户的服务配置
  252. cat > "$INSTALL_DIR/dzxj_dtu.service" << EOF
  253. [Unit]
  254. Description=DZXJ DTU Application
  255. After=network.target
  256. [Service]
  257. User=$USER
  258. WorkingDirectory=$INSTALL_DIR/backend
  259. Environment="FLASK_APP=app.py"
  260. Environment="FLASK_ENV=production"
  261. Environment="TZ=Asia/Shanghai"
  262. ExecStart=$INSTALL_DIR/venv/bin/python -m flask run --host=0.0.0.0 --port=5001
  263. Restart=on-failure
  264. RestartSec=5
  265. [Install]
  266. WantedBy=multi-user.target
  267. EOF
  268. fi
  269. # 设置脚本执行权限
  270. chmod +x "$INSTALL_DIR/start.sh"
  271. chmod +x "$INSTALL_DIR/stop.sh"
  272. # 配置开机自启动
  273. echo -e "${GREEN}配置开机自启动...${NC}"
  274. # 复制service文件到systemd目录
  275. sudo cp "$INSTALL_DIR/dzxj_dtu.service" /etc/systemd/system/
  276. if [ $? -eq 0 ]; then
  277. echo -e "${GREEN}已复制服务配置到 /etc/systemd/system/dzxj_dtu.service${NC}"
  278. # 重新加载systemd配置
  279. sudo systemctl daemon-reload
  280. if [ $? -eq 0 ]; then
  281. echo -e "${GREEN}已重新加载systemd配置${NC}"
  282. # 启用服务
  283. sudo systemctl enable dzxj_dtu.service
  284. if [ $? -eq 0 ]; then
  285. echo -e "${GREEN}已启用dzxj_dtu服务开机自启动${NC}"
  286. # 启动服务
  287. sudo systemctl start dzxj_dtu.service
  288. if [ $? -eq 0 ]; then
  289. echo -e "${GREEN}dzxj_dtu服务已启动${NC}"
  290. else
  291. echo -e "${YELLOW}警告: dzxj_dtu服务启动失败,请手动检查${NC}"
  292. fi
  293. else
  294. echo -e "${RED}错误: 启用dzxj_dtu服务失败${NC}"
  295. fi
  296. else
  297. echo -e "${RED}错误: 重新加载systemd配置失败${NC}"
  298. fi
  299. else
  300. echo -e "${RED}错误: 复制服务配置失败${NC}"
  301. fi
  302. # 显示安装完成信息
  303. echo -e "\n${GREEN}========================================${NC}"
  304. echo -e "${GREEN} DZXJ DTU 应用安装完成! ${NC}"
  305. echo -e "${GREEN}========================================${NC}\n"
  306. echo -e "安装目录: ${GREEN}$INSTALL_DIR${NC}"
  307. echo -e "\n使用方法:"
  308. echo -e " 1. 手动启动应用:"
  309. echo -e " ${YELLOW}cd $INSTALL_DIR && ./start.sh${NC}"
  310. echo -e "\n 2. 停止应用:"
  311. echo -e " ${YELLOW}cd $INSTALL_DIR && ./stop.sh${NC}"
  312. echo -e "\n 3. 管理系统服务:"
  313. echo -e " ${YELLOW}# 查看服务状态"
  314. echo -e " sudo systemctl status dzxj_dtu.service"
  315. echo -e " # 重启服务"
  316. echo -e " sudo systemctl restart dzxj_dtu.service"
  317. echo -e " # 禁用自启动"
  318. echo -e " sudo systemctl disable dzxj_dtu.service${NC}"
  319. echo -e "\n 4. 检查nginx状态:"
  320. echo -e " ${YELLOW}sudo systemctl status nginx${NC}"
  321. echo -e "\n应用访问地址: ${GREEN}http://localhost${NC}"
  322. echo -e "\n${GREEN}安装成功!${NC}"