diff --git a/.brashrc.alias b/.brashrc.alias new file mode 100644 index 0000000000000000000000000000000000000000..0d64a606a157bf69226a11213c2a8d91fc1069ac --- /dev/null +++ b/.brashrc.alias @@ -0,0 +1,26 @@ +alias dnginx="docker exec -it xh-nginx /bin/bash" +alias dphp="docker exec -it --user www-data:www-data xh-php /bin/bash" +alias dmysql="docker exec -it xh-mysql /bin/bash" +alias dredis="docker exec -it xh-redis /bin/bash" + + +php() { + docker run \ + -it \ + --rm \ + --volume $PWD:/www:rw \ + --user www-data:www-data \ + --workdir /www \ + xh_php php "$@" +} + + +composer () { + docker run \ + -it \ + --rm \ + --user www-data:www-data \ + --volume $(pwd):/app \ + --workdir /app \ + xh_php composer "$@" +} diff --git a/install.sh.sample b/install.sh.sample index bbc69b99238cde9cceb0290e155ee8a7eb8dda76..168813f1586a8227032d66062d4d9a71e0d29968 100644 --- a/install.sh.sample +++ b/install.sh.sample @@ -37,6 +37,12 @@ REDIS_PASSWORD=1qaz2wsx3edc # ------------ @@@ 以下内容,非专业人士请勿修改,新手请远离! @@@ ------------- # +#基准目录 +base_dir=~/ctc-docker + +#ctc目录 +ctc_dir=${base_dir}/html/ctc + #成功信息输出 success_print() { echo -e "\033[32m $1 \033[0m" @@ -61,6 +67,12 @@ os_type() { fi } +# 判断是否 root 用户,如果不是root用户,则中断运行 +if [[ $EUID -ne 0 ]]; then + error_print "Error:This script must be run as root!" 1>&2 + exit 1 +fi + #安装git和curl if [ "$(os_type)" = 'ubuntu' ] || [ "$(os_type)" = 'debian' ]; then sudo apt-get update && apt-get install -y curl git @@ -71,11 +83,6 @@ else exit fi -#基准目录 -base_dir=~/ctc-docker - -#ctc目录 -ctc_dir=${base_dir}/html/ctc #克隆ctc-docker项目 if [ ! -d ${base_dir} ]; then @@ -182,6 +189,36 @@ docker-compose build #启动容器 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 database connection..." + sleep 1 +done + +exit +EOF + #导入测试数据 if [ ${SITE_DEMO} = 'on' ]; then @@ -205,22 +242,11 @@ EOF fi -docker exec -i ctc-php bash <<'EOF' +# 使用 www-data 用户运行,避免权限问题 +docker exec -i --user www-data:www-data ctc-php bash <<'EOF' ctc_dir=/var/www/html/ctc -#修改storage目录权限 -chown -R www-data:www-data ${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} diff --git a/php/Dockerfile b/php/Dockerfile index c09cc9d724eb33ef4a68fd67e177ae7f8ddb4f7f..e295cf04d1f7ed28e704dfad5dc76195af2fba13 100644 --- a/php/Dockerfile +++ b/php/Dockerfile @@ -1 +1,10 @@ -FROM xiaochong0302/php:latest \ No newline at end of file +FROM xiaochong0302/php:latest + +# 修正 www-data 在 /var/www 目录下无法读写 .composer 的问题 +RUN mkdir -p /var/www && \ + chown www-data:www-data /var/www -R + +# 安装 nc,用于判断MySQL是否已经成功运行。 +RUN apt-get update && \ + apt-get install -y --no-install-recommends netcat && \ + rm -rf /var/lib/apt/lists/*