1 Star 0 Fork 119

wojiaofeiji/course-tencent-cloud-docker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
install.sh 7.23 KB
一键复制 编辑 原始数据 按行查看 历史
FredG 提交于 2024-08-13 07:50 . 1.修改install脚本
#!/usr/bin/env bash
# ----------- 请根据实际情况,修改如下配置 ------------ #
#是否安装测试数据(on:是,off:否)
SITE_DEMO=on
#站点域名(不包括http)
SITE_DOMAIN=bombusbee.cn
#站点密钥(数字字母组合,不要用特殊字符)
SITE_KEY=1qaz2wsx3edc
#mysql超级用户密码(数字字母组合,不要用特殊字符)
MYSQL_ROOT_PASSWORD=1qaz2wsx3edc
#mysql主机名称(非独立实例请不要修改,独立实例填写主机IP)
MYSQL_HOST=BomBusBee-mysql
#mysql主机端口(非独立实例请不要修改)
MYSQL_PORT=13306
#mysql项目数据库名称(数字字母组合,不要用特殊字符)
MYSQL_DATABASE=ctc
#mysql项目数据库用户(数字字母组合,不要用特殊字符)
MYSQL_USER=ctc
#mysql项目数据库密码(数字字母组合,不要用特殊字符)
MYSQL_PASSWORD=1q23a2z2wsxd3edc
#redis主机名称(非独立实例请不要修改,独立实例填写主机IP)
REDIS_HOST=BomBusBee-redis
#redis端口
REDIS_PORT=16379
#redis访问密码(数字字母组合,不要用特殊字符)
REDIS_PASSWORD=xagM@#maQPJDEp7f
# ------------ @@@ 以下内容,非专业人士请勿修改,新手请远离! @@@ ------------- #
#基准目录
base_dir=/home/ubuntu/ctc
#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
#加入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'\]\['port'\].*/\$config['redis']['port'] = '${REDIS_PORT}';/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 ------"
#切换到基准目录
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Docker
1
https://gitee.com/FredFeiJi/course-tencent-cloud-docker.git
[email protected]:FredFeiJi/course-tencent-cloud-docker.git
FredFeiJi
course-tencent-cloud-docker
course-tencent-cloud-docker
master

搜索帮助