From 85cf52b0e417e65da13d344267d754c3c5dca777 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Thu, 25 Dec 2025 14:22:59 +0800 Subject: [PATCH] =?UTF-8?q?chore(docker):=20=E4=BC=98=E5=8C=96docker/php/e?= =?UTF-8?q?ntrypoint.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/php/entrypoint.sh | 135 +++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 49 deletions(-) diff --git a/docker/php/entrypoint.sh b/docker/php/entrypoint.sh index d952a308b..1b5a7ccf2 100644 --- a/docker/php/entrypoint.sh +++ b/docker/php/entrypoint.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -e +# 移除 set -e 以便更好的错误控制 echo "=== ThinkPHP Docker权限初始化 ===" @@ -9,8 +9,7 @@ APP_ROOT="${PHP_APP_ROOT:-/var/www/html}" echo "使用应用根目录: $APP_ROOT" # 创建统一的Web组并配置所有用户(最高效的权限管理) -# 错误处理:如果配置失败,不要终止整个脚本 -configure_web_users || echo "⚠️ Web用户配置出现问题,但继续执行权限设置"() { +configure_web_users() { # 常见Web服务器用户列表 WEB_USERS=("www-data" "www" "apache" "nginx") @@ -69,9 +68,9 @@ configure_web_users || echo "⚠️ Web用户配置出现问题,但继续执 if id "$web_user" &>/dev/null; then echo "📝 处理Web用户: $web_user" - # 获取用户当前组信息 - current_groups=$(id -Gn "$web_user" 2>/dev/null | tr ' ' ',') - echo " 当前所属组: $current_groups" + # 获取用户当前组信息(安全的变量处理) + current_groups=$(id -Gn "$web_user" 2>/dev/null | tr '\n' ' ' | sed 's/ *$//') + echo " 当前所属组: ${current_groups:-无}" # 尝试将用户加入统一组(使用-a参数保留现有组,只添加新组) if usermod -a -G "$WEB_GROUP" "$web_user" 2>/dev/null; then @@ -133,7 +132,8 @@ if [ -d "$APP_ROOT" ]; then # 最终验证 if [ -z "$FINAL_WEB_GROUP" ]; then echo "❌ 无法确定有效的Web组,跳过权限设置" - return 1 + echo "=== 启动应用 ===" + exec "$@" fi WEB_GROUP="$FINAL_WEB_GROUP" @@ -159,24 +159,17 @@ if [ -d "$APP_ROOT" ]; then dir_count=0 file_count=0 - # 设置目录权限 - while IFS= read -r -d '' dir; do - if chmod 775 "$dir" 2>/dev/null; then - dir_count=$((dir_count + 1)) - fi - done < <(find "$APP_ROOT" -type d -print0 2>/dev/null | head -z -1000) # 限制处理数量避免性能问题 - - # 设置文件权限 - while IFS= read -r -d '' file; do - if chmod 664 "$file" 2>/dev/null; then - file_count=$((file_count + 1)) - fi - done < <(find "$APP_ROOT" -type f -print0 2>/dev/null | head -z -1000) - - # 设置setgid位,确保新文件继承组权限 - while IFS= read -r -d '' dir; do - chmod g+s "$dir" 2>/dev/null - done < <(find "$APP_ROOT" -type d -print0 2>/dev/null | head -z -500) + # 设置目录权限(兼容性更好的方式) + if command -v find >/dev/null 2>&1; then + dir_count=$(find "$APP_ROOT" -type d -exec chmod 775 {} \; 2>/dev/null | wc -l) + file_count=$(find "$APP_ROOT" -type f -exec chmod 664 {} \; 2>/dev/null | wc -l) + find "$APP_ROOT" -type d -exec chmod g+s {} \; 2>/dev/null + else + # 备用方案:使用简单的循环 + echo "find命令不可用,跳过批量权限设置" + dir_count=0 + file_count=0 + fi echo "📊 权限设置完成: $dir_count个目录, $file_count个文件" @@ -223,59 +216,103 @@ if [ -d "$APP_ROOT" ]; then # 验证文件权限是否足够(测试统一组权限效果) echo "=== 验证统一组权限效果 ===" - test_file="$APP_ROOT/index.php" + + # 查找测试文件的更可靠方法 + test_file="" + + # 方法1: 查找index.html + if [ -f "$APP_ROOT/index.html" ]; then + test_file="$APP_ROOT/index.html" + fi + + # 方法2: 查找任意HTML文件(更安全的方式) + if [ -z "$test_file" ]; then + first_html=$(find "$APP_ROOT" -maxdepth 2 -name "*.html" -type f 2>/dev/null | head -1) + if [ -n "$first_html" ] && [ -f "$first_html" ]; then + test_file="$first_html" + fi + fi + + # 方法3: 查找index.php + if [ -z "$test_file" ] && [ -f "$APP_ROOT/index.php" ]; then + test_file="$APP_ROOT/index.php" + fi + + # 方法4: 创建专用测试文件(最安全的选择) + if [ -z "$test_file" ]; then + test_file="$APP_ROOT/.permission_test.html" + echo "创建专用权限测试文件" + cat > "$test_file" << 'EOF' + + +