-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh.sample
347 lines (258 loc) · 9.11 KB
/
install.sh.sample
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env bash
# ----------- 请根据实际情况,修改如下配置 ------------ #
#是否安装测试数据(on:是,off:否)
SITE_DEMO=off
#站点域名(不包括http)
SITE_DOMAIN=abc.com
#站点密钥(数字字母组合,不要用特殊字符)
SITE_KEY=1qaz2wsx3edc
#mysql超级用户密码(数字字母组合,不要用特殊字符)
MYSQL_ROOT_PASSWORD=1qaz2wsx3edc
#mysql主机名称(非独立实例请不要修改,独立实例填写主机IP)
MYSQL_HOST=mysql
#mysql主机端口(非独立实例请不要修改)
MYSQL_PORT=3306
#mysql项目数据库名称(数字字母组合,不要用特殊字符)
MYSQL_DATABASE=ctc
#mysql项目数据库用户(数字字母组合,不要用特殊字符)
MYSQL_USER=ctc
#mysql项目数据库密码(数字字母组合,不要用特殊字符)
MYSQL_PASSWORD=1qaz2wsx3edc
#redis主机名称(非独立实例请不要修改,独立实例填写主机IP)
REDIS_HOST=redis
#redis访问密码(数字字母组合,不要用特殊字符)
REDIS_PASSWORD=1qaz2wsx3edc
# ------------ @@@ 以下内容,非专业人士请勿修改,新手请远离! @@@ ------------- #
#基准目录
base_dir=~/ctc-docker
#ctc目录
ctc_dir=${base_dir}/html/ctc
#普通信息输出
normal_print() {
echo -e "\033[34m \n $1 \n \033[0m"
}
#成功信息输出
success_print() {
echo -e "\033[32m \n $1 \n \033[0m"
}
#失败信息输出
error_print() {
echo -e "\033[31m \n $1 \n \033[0m"
}
#系统判断
os_type() {
os=$(grep "^ID=" /etc/os-release)
if [[ ${os} =~ 'centos' ]]; then
echo 'centos'
elif [[ ${os} =~ 'ubuntu' ]]; then
echo 'ubuntu'
elif [[ ${os} =~ 'debian' ]]; then
echo 'debian'
else
echo 'other'
fi
}
#判断是否root用户
if [[ $EUID -ne 0 ]]; then
error_print "------ this script must be run as root ------"
exit 1
fi
normal_print "------ update host system ------"
#安装git和curl
if [ "$(os_type)" = 'ubuntu' ] || [ "$(os_type)" = 'debian' ]; then
sudo apt-get update && apt-get install -y curl git
elif [ "$(os_type)" = 'centos' ]; then
sudo yum update && yum install -y curl git
else
error_print "------ we only support (ubuntu|debian|centos) currently ------"
exit 1
fi
normal_print "------ git clone ctc-docker ------"
#克隆ctc-docker项目
if [ ! -d ${base_dir} ]; then
git clone https://gitee.com/koogua/course-tencent-cloud-docker.git ${base_dir}
else
cd ${base_dir} && git pull
fi
normal_print "------ git clone ctc ------"
#克隆ctc项目
if [ ! -d ${ctc_dir} ]; then
git clone https://gitee.com/koogua/course-tencent-cloud.git ${ctc_dir}
else
cd ${ctc_dir} && git pull
fi
#加入git安全目录
git config --global --add safe.directory ${ctc_dir}
#docker .env文件
docker_env=${base_dir}/.env
#复制环境变量文件
cp ${base_dir}/.env.default ${docker_env}
#替换docker .env配置项
sed -i "s/MYSQL_ROOT_PASSWORD.*/MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}/g" ${docker_env}
sed -i "s/MYSQL_DATABASE.*/MYSQL_DATABASE=${MYSQL_DATABASE}/g" ${docker_env}
sed -i "s/MYSQL_USER.*/MYSQL_USER=${MYSQL_USER}/g" ${docker_env}
sed -i "s/MYSQL_PASSWORD.*/MYSQL_PASSWORD=${MYSQL_PASSWORD}/g" ${docker_env}
sed -i "s/REDIS_PASSWORD.*/REDIS_PASSWORD=${REDIS_PASSWORD}/g" ${docker_env}
#nginx配置目录
nginx_conf_dir=${base_dir}/nginx/conf.d
nginx_default_conf=${nginx_conf_dir}/default.conf
#复制nginx站点配置文件
cp ${nginx_conf_dir}/default.conf.sample ${nginx_default_conf}
#替换nginx default.conf配置项
sed -i "s/server_name .*/server_name ${SITE_DOMAIN};/g" ${nginx_default_conf}
#ctc config目录
ctc_config_dir=${ctc_dir}/config
ctc_config_php=${ctc_config_dir}/config.php
#复制config.php
cp ${ctc_config_dir}/config.default.php ${ctc_config_php}
#替换config.php配置项
sed -i "s/\$config\['key'\].*/\$config['key'] = '${SITE_KEY}';/g" ${ctc_config_php}
sed -i "s/\$config\['db'\]\['host'\].*/\$config['db']['host'] = '${MYSQL_HOST}';/g" ${ctc_config_php}
sed -i "s/\$config\['db'\]\['port'\].*/\$config['db']['port'] = '${MYSQL_PORT}';/g" ${ctc_config_php}
sed -i "s/\$config\['db'\]\['dbname'\].*/\$config['db']['dbname'] = '${MYSQL_DATABASE}';/g" ${ctc_config_php}
sed -i "s/\$config\['db'\]\['username'\].*/\$config['db']['username'] = '${MYSQL_USER}';/g" ${ctc_config_php}
sed -i "s/\$config\['db'\]\['password'\].*/\$config['db']['password'] = '${MYSQL_PASSWORD}';/g" ${ctc_config_php}
sed -i "s/\$config\['redis'\]\['host'\].*/\$config['redis']['host'] = '${REDIS_HOST}';/g" ${ctc_config_php}
sed -i "s/\$config\['redis'\]\['auth'\].*/\$config['redis']['auth'] = '${REDIS_PASSWORD}';/g" ${ctc_config_php}
sed -i "s/\$config\['websocket'\]\['connect_address'\].*/\$config['websocket']['connect_address'] = '${SITE_DOMAIN}:8282';/g" ${ctc_config_php}
#复制xunsearch配置文件
cp ${ctc_config_dir}/xs.course.default.ini ${ctc_config_dir}/xs.course.ini
cp ${ctc_config_dir}/xs.article.default.ini ${ctc_config_dir}/xs.article.ini
cp ${ctc_config_dir}/xs.question.default.ini ${ctc_config_dir}/xs.question.ini
normal_print "------ install docker ------"
#安装docker
if [ -z "$(command -v docker)" ]; then
sudo bash ${base_dir}/get-docker.sh --mirror Aliyun
sudo groupadd docker
sudo usermod -aG docker "${USER}"
sudo systemctl enable docker
sudo systemctl start docker
fi
if [ -z "$(command -v docker)" ]; then
error_print "------ error: docker command not found, please try again ------"
exit 1
fi
if [ ! -d '/etc/docker' ]; then
sudo mkdir -p '/etc/docker'
fi
#docker镜像加速
if [ ! -e '/etc/docker/daemon.json' ]; then
tee -a /etc/docker/daemon.json <<-EOF
{
"registry-mirrors": [
"https://mirror.ccs.tencentyun.com"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "500m",
"max-file": "3"
}
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo chmod 666 /var/run/docker.sock
fi
normal_print "------ install docker-compose ------"
docker_compose_url="https://download.koogua.com/docker-compose-$(uname -s)-$(uname -m)"
#安装docker-compose(路径转小写)
if [ -z "$(command -v docker-compose)" ]; then
sudo curl -L "${docker_compose_url,,}" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
if [ -z "$(command -v docker-compose)" ]; then
error_print "------ error: docker-compose command not found, please try again ------"
exit 1
fi
#切换到基准目录
cd ${base_dir} || exit
normal_print "------ build docker images ------"
#构建镜像
docker-compose build
normal_print "------ start docker services ------"
#启动容器
docker-compose up -d
docker exec -i ctc-php bash <<'EOF'
ctc_dir=/var/www/html/ctc
tmp_sitemap_xml=${ctc_dir}/storage/tmp/sitemap.xml
public_sitemap_xml=${ctc_dir}/public/sitemap.xml
#创建并连接sitemap.xml
if [ ! -e ${tmp_sitemap_xml} ]; then
touch ${tmp_sitemap_xml}
fi
#覆盖软连接,避免错误的软连接
ln -sf ${tmp_sitemap_xml} ${public_sitemap_xml}
#统一ctc目录权限
chown -R www-data:www-data ${ctc_dir}
#判断mysql容器是否已经启动成功
until nc -z -v -w30 mysql 3306 &> /dev/null
do
echo "waiting for mysql service..."
sleep 1
done
exit
EOF
#导入测试数据
if [ ${SITE_DEMO} = 'on' ]; then
demo_sql=${base_dir}/mysql/data/demo.sql.gz
normal_print "------ import demo.sql.gz ------"
curl https://download.koogua.com/ctc/demo.sql.gz -o ${demo_sql}
docker exec -i ctc-mysql bash <<'EOF'
gunzip < /var/lib/mysql/demo.sql.gz | mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE}
exit
EOF
rm -f ${demo_sql}
fi
normal_print "------ ctc upgrade ------"
#使用www-data用户运行,避免权限问题
docker exec -i --user www-data:www-data ctc-php bash <<'EOF'
ctc_dir=/var/www/html/ctc
#切换到ctc目录
cd ${ctc_dir}
echo -e "\n------ install composer packages ------\n"
#安装依赖包
composer install --no-dev
composer dump-autoload --optimize
echo -e "\n------ phinx migrate ------\n"
#数据迁移
vendor/bin/phinx migrate
echo -e "\n------ php console upgrade ------\n"
#程序升级
php console.php upgrade
echo -e "\n------ rebuild xunsearch index ------\n"
#重建xunsearch索引
php console.php course_index rebuild
php console.php article_index rebuild
php console.php question_index rebuild
#清理log(之前步骤以root身份产生日志会让www-data用户无写入权限)
rm -f storage/log/*.log
exit
EOF
#重启php容器(websocket依赖WorkerGateway相关包,之前启动php容器时并未安装相关包)
docker container restart ctc-php
docker_ps=$(docker ps)
if [[ "${docker_ps}" =~ 'nginx' ]]; then
success_print "------ nginx service ok ------"
else
error_print "------ nginx service failed ------"
fi
if [[ "${docker_ps}" =~ 'php' ]]; then
success_print "------ php service ok ------"
else
error_print "------ php service failed ------"
fi
if [[ "${docker_ps}" =~ 'mysql' ]]; then
success_print "------ mysql service ok ------"
else
error_print "------ mysql service failed ------"
fi
if [[ "${docker_ps}" =~ 'redis' ]]; then
success_print "------ redis service ok ------"
else
error_print "------ redis service failed ------"
fi
if [[ "${docker_ps}" =~ 'xunsearch' ]]; then
success_print "------ xunsearch service ok ------"
else
error_print "------ xunsearch service failed ------"
fi