#!/bin/bash set -e echo "=== ThinkPHP Docker权限初始化 ===" # 定义应用根目录,优先使用环境变量,否则使用默认值 APP_ROOT="${PHP_APP_ROOT:-/var/www/html}" echo "使用应用根目录: $APP_ROOT" # 获取正确的用户和组 if [ -n "$USER_ID" ] && [ -n "$GROUP_ID" ]; then # 如果指定了用户ID,修改www-data usermod -u $USER_ID www-data groupmod -g $GROUP_ID www-data fi echo "当前用户: $(whoami)" echo "UID: $(id -u), GID: $(id -g)" # 修复所有目录权限(处理挂载覆盖问题,一次性处理) if [ -d "$APP_ROOT" ]; then echo "修复应用目录权限(处理 Docker 挂载覆盖问题)" # 设置所有权 chown -R www-data:www-data "$APP_ROOT" # 设置目录权限和setgid find "$APP_ROOT" -type d -exec chmod 775 {} \; find "$APP_ROOT" -type f -exec chmod 664 {} \; find "$APP_ROOT" -type d -exec chmod g+s {} \; # 设置ACL(如果支持) if command -v setfacl >/dev/null 2>&1; then setfacl -dR -m u:www-data:rwx "$APP_ROOT" 2>/dev/null || echo "ACL 设置失败,继续执行" fi # 设置umask umask 0002 echo "✅ 应用目录权限修复完成" # 验证文件权限是否足够 echo "=== 验证文件权限 ===" test_file="$APP_ROOT/index.php" if [ -f "$test_file" ]; then echo "测试文件: $test_file" echo "文件权限: $(stat -c '%a %n' "$test_file")" echo "www-data 用户测试读权限: $(su -s /bin/sh -c "cat '$test_file' >/dev/null && echo '✅ 可读' || echo '❌ 不可读'" www-data 2>/dev/null || '无法测试')" echo "www-data 用户测试写权限: $(su -s /bin/sh -c "echo 'test' >> '$test_file.test' && rm '$test_file.test' && echo '✅ 可写' || echo '❌ 不可写'" www-data 2>/dev/null || '无法测试')" fi fi echo "=== 启动应用 ===" # 执行原有的启动命令 exec "$@"