From 7e55ff785323bc4babe314dcab85242b66d309c9 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Fri, 25 Dec 2020 11:46:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3sitemap?= =?UTF-8?q?.xml=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed9dad7..f00b108 100644 --- a/README.md +++ b/README.md @@ -133,10 +133,10 @@ cp xs.user.default.ini xs.user.ini chmod -R 777 /var/www/html/ctc/storage ``` -修改 sitemap.xml 文件读写权限 +创建 sitemap.xml 软链接 ``` -chmod 777 /var/www/html/ctc/public/sitemap.xml +ln -s /var/www/html/ctc/storage/tmp/sitemap.xml /var/www/html/ctc/public/sitemap.xml ``` 安装依赖包 -- Gitee From 386f1378ac4a49cbb31bf5ce15fd3d30a61c91eb Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 19 Jan 2021 18:48:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E5=AE=89=E8=A3=85=E5=92=8C=E5=A4=87=E4=BB=BD=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + backup.sh | 48 +++++++++++++ install.sh | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ php/Dockerfile | 5 +- 4 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 backup.sh create mode 100644 install.sh diff --git a/README.md b/README.md index f00b108..c2bb80e 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ ln -s /var/www/html/ctc/storage/tmp/sitemap.xml /var/www/html/ctc/public/sitemap ``` cd /var/www/html/ctc composer install --no-dev +composer dump-autoload --optimize ``` 数据库迁移 diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..ec9980e --- /dev/null +++ b/backup.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +docker exec -i ctc-mysql bash <<'EOF' + +#数据库名称 +db_name=ctc + +#数据库用户 +db_user=ctc + +#数据库密码 +db_pwd=1qaz2wsx3edc + +#备份目录(不要修改,末尾不带"/") +backup_dir=/var/lib/mysql/backup + +#备份保留天数 +backup_days=15 + +#创建备份目录 +if [ ! -d ${backup_dir} ]; then + mkdir -p ${backup_dir} +fi + +#导出数据 +mysqldump --no-tablespaces -u ${db_user} -p${db_pwd} ${db_name} | gzip > ${backup_dir}/${db_name}-$(date +%Y-%m-%d).sql.gz + +#待删过期备份文件 +rm_filename=${backup_dir}/${db_name}-$(date -d -${backup_days}day +%Y-%m-%d).sql.gz + +#删除过期备份文件 +if [ `ls -l ${backup_dir} | grep sql.gz | wc -l` -gt ${backup_days} ]; then + if [ -e ${rm_filename} ]; then + rm -f ${rm_filename} + fi +fi + +exit +EOF + +#本地目录(根据实际调整,末尾带"/") +local_dir=/home/ubuntu/ctc-docker/mysql/data/backup/ + +#远程目录(根据实际调整,末尾带"/") +remote_dir=/backup/database/ + +#同步备份 +coscmd upload -rs ${local_dir} ${remote_dir} diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..79f28d2 --- /dev/null +++ b/install.sh @@ -0,0 +1,179 @@ +#!/usr/bin/env bash + +# ----------- 请根据实际情况,修改如下配置 ------------ # + +#是否安装测试数据(on:是,off:否) +SITE_DEMO=off + +#站点域名(不包括http) +SITE_DOMAIN=abc.com + +#站点密钥(数字字母组合,不要用特殊字符) +SITE_KEY=1qaz2wsx3edc + +#mysql超级用户密码(数字字母组合,不要用特殊字符) +MYSQL_ROOT_PASSWORD=1qaz2wsx3edc + +#mysql项目数据库名称(数字字母组合,不要用特殊字符) +MYSQL_DATABASE=ctc + +#mysql项目数据库用户(数字字母组合,不要用特殊字符) +MYSQL_USER=ctc + +#mysql项目数据库密码(数字字母组合,不要用特殊字符) +MYSQL_PASSWORD=1qaz2wsx3edc + +#redis访问密码(数字字母组合,不要用特殊字符) +REDIS_PASSWORD=1qaz2wsx3edc + +# --------------- !!! 以下内容,请勿修改!!! --------------- # + +#安装git和curl +sudo apt-get update && apt-get install -y curl git + +#写入docker配置 +sudo echo '{"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]}' | tee /etc/docker/daemon.json + +#安装docker +if [ -z "$(command -v docker)" ]; then + sudo curl -sSL https://get.daocloud.io/docker | sh +fi + +#启动docker +sudo service docker start + +#安装docker-composer +if [ -z "$(command -v docker-compose)" ]; then + sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose +fi + +#docker-compose增加执行权限 +sudo chmod +x /usr/local/bin/docker-compose + +#基准目录 +base_dir=~/ctc-docker + +#ctc目录 +ctc_dir=${base_dir}/html/ctc + +#克隆ctc-docker项目 +if [ ! -d ${base_dir} ]; then + git clone https://gitee.com/koogua/course-tencent-cloud-docker.git ${base_dir} +fi + +#克隆ctc项目 +if [ ! -d ${ctc_dir} ]; then + git clone https://gitee.com/koogua/course-tencent-cloud.git ${ctc_dir} +fi + +#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'\]\['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'\]\['auth'\].*/\$config['redis']['auth'] = '${REDIS_PASSWORD}';/g" ${ctc_config_php} +sed -i "s/\$config\['websocket'\]\['connect_address'\].*/\$config['websocket']['connect_address'] = 'http:\/\/${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.group.default.ini ${ctc_config_dir}/xs.group.ini +cp ${ctc_config_dir}/xs.user.default.ini ${ctc_config_dir}/xs.user.ini + +#切换到基准目录 +cd ${base_dir} || exit + +#构建镜像 +docker-compose build + +#启动容器 +docker-compose up -d + +#导入测试数据 +if [ ${SITE_DEMO} == 'on' ]; then + + echo -e "\n------ start import demo data ------\n" + + docker exec -i ctc-mysql bash <<'EOF' + + #安装curl + apt-get update && apt-get install -y curl + + #下载数据 + curl http://download.koogua.com/ctc-test.sql.gz -o ctc-test.sql.gz + + #导入数据 + gunzip < ctc-test.sql.gz | mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} + + echo -e "\n------ finish import demo data ------\n" + + exit +EOF + +fi + +docker exec -i ctc-php bash <<'EOF' + +ctc_dir=/var/www/html/ctc + +#修改storage目录权限 +chmod -R 777 ${ctc_dir}/storage + +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} + ln -s ${tmp_sitemap_xml} ${public_sitemap_xml} +fi + +#切换到ctc目录 +cd ${ctc_dir} + +#安装依赖包 +composer install --no-dev +composer dump-autoload --optimize + +#执行升级 +vendor/bin/phinx migrate + +#重建xunsearch索引 +php console.php course_index rebuild +php console.php group_index rebuild +php console.php user_index rebuild + +exit +EOF + +#安装完成 +echo -e "\n------ install finished, you can visit your website now. ------\n" diff --git a/php/Dockerfile b/php/Dockerfile index a2bf4c9..23ade76 100644 --- a/php/Dockerfile +++ b/php/Dockerfile @@ -23,15 +23,14 @@ RUN echo "" > ${SOURCES_LIST} \ && echo "deb http://mirrors.cloud.tencent.com/debian buster main contrib non-free" >> ${SOURCES_LIST} \ && echo "deb http://mirrors.cloud.tencent.com/debian buster-updates main contrib non-free" >> ${SOURCES_LIST} \ && apt-get update \ - && apt-get install -y --no-install-recommends supervisor cron nano zip unzip libpng-dev \ + && apt-get install -y --no-install-recommends git supervisor cron nano zip unzip libpng-dev \ && rm -rf /var/lib/apt/lists/* RUN docker-php-ext-install pdo_mysql pcntl mbstring bcmath gd \ && pecl install igbinary redis \ && docker-php-ext-enable igbinary redis -RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ - && composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer +RUN curl https://mirrors.aliyun.com/composer/composer.phar -o /usr/local/bin/composer \ COPY ./cron.d/course /etc/cron.d/course -- Gitee