-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
153 lines (132 loc) · 3.75 KB
/
install.sh
File metadata and controls
153 lines (132 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
#############################################################################
# Multi-Terminal 一键部署脚本
# 自动安装 Node.js + tmux + 部署应用
#############################################################################
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${BLUE}[INFO]${NC} $1"; }
ok() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
err() { echo -e "${RED}[ERROR]${NC} $1"; }
# 检查 root
if [ $EUID -ne 0 ]; then
err "请使用 sudo 运行:sudo $0"
exit 1
fi
# 检测包管理器
if command -v apt-get &> /dev/null; then
PM="apt"
INSTALL="apt-get install -y"
elif command -v yum &> /dev/null; then
PM="yum"
INSTALL="yum install -y"
else
err "不支持的系统,仅支持 Debian/Ubuntu/CentOS"
exit 1
fi
log "更新软件包列表..."
if [ "$PM" = "apt" ]; then
apt-get update -qq
else
yum makecache -q || true
fi
# 安装系统依赖
log "安装基础依赖..."
$INSTALL curl git tmux
# 安装 Node.js 18
log "安装 Node.js 18..."
if command -v node &> /dev/null; then
NODE_VER=$(node -v | cut -d. -f1 | tr -d 'v')
if [ "$NODE_VER" -ge 18 ]; then
ok "Node.js 已安装 $(node -v)"
else
warn "Node.js 版本过低,重新安装..."
if [ "$PM" = "apt" ]; then
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
$INSTALL nodejs
else
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
$INSTALL nodejs
fi
fi
else
if [ "$PM" = "apt" ]; then
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
$INSTALL nodejs
else
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
$INSTALL nodejs
fi
fi
ok "Node.js $(node -v) 已安装"
# 部署目录
APP_DIR="/opt/multi-terminal"
log "部署到 $APP_DIR"
mkdir -p $APP_DIR
# 复制文件
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
log "从 $SCRIPT_DIR 复制文件..."
# 复制所有项目文件(排除 .git 目录)
cd "$SCRIPT_DIR"
if [ -f "server.js" ]; then
# 使用 rsync 或 cp 复制文件
if command -v rsync &> /dev/null; then
rsync -av --exclude='.git' --exclude='node_modules' . $APP_DIR/
else
# 手动复制每个文件
for f in * .*; do
[ "$f" = "." ] || [ "$f" = ".." ] || [ "$f" = ".git" ] && continue
[ -f "$f" ] || [ -d "$f" ] || continue
cp -r "$f" $APP_DIR/ 2>/dev/null || true
done
fi
cd $APP_DIR
else
err "未找到 server.js,请确保 install.sh 在项目根目录运行"
exit 1
fi
# 检查是否有代码
if [ ! -f "server.js" ]; then
err "未找到 server.js,请确保脚本与项目文件一起上传"
exit 1
fi
# 安装依赖
log "安装 npm 依赖..."
npm install --production
# 生成随机密钥
JWT_SECRET=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# 创建 .env
cat > $APP_DIR/.env << EOF
NODE_ENV=production
PORT=3000
JWT_SECRET=$JWT_SECRET
USERS='[{"username":"admin","password":"admin123"}]'
EOF
# 安装 PM2
log "安装 PM2..."
npm install -g pm2
# PM2 启动
log "启动服务..."
pm2 start server.js --name multi-terminal
pm2 save
pm2 startup 2>/dev/null || true
echo ""
echo "========================================"
ok "部署完成!"
echo "========================================"
echo ""
echo "访问:http://\$(hostname -I | awk '{print \$1}'):3000"
echo ""
echo "默认账号:admin / admin123"
echo ""
echo "常用命令:"
echo " pm2 logs multi-terminal # 查看日志"
echo " pm2 restart multi-terminal # 重启"
echo " pm2 stop multi-terminal # 停止"
echo " tmux # 进入 tmux 会话"
echo ""