chore(docker): update docker/php/entrypoint.sh

This commit is contained in:
2025-12-25 08:58:34 +08:00
parent 267cfa12a2
commit 866ca8d938

View File

@@ -3,8 +3,15 @@ set -e
echo "=== ThinkPHP Docker权限初始化 ==="
# 定义应用根目录
APP_ROOT="/var/www/html"
# 定义应用根目录,支持参数传入
APP_ROOT="${1:-/var/www/html}"
echo "使用应用根目录: $APP_ROOT"
if [ $# -eq 1 ]; then
echo "✅ 使用传入参数: $1"
else
echo "✅ 使用默认参数: /var/www/html"
fi
# 获取正确的用户和组
if [ -n "$USER_ID" ] && [ -n "$GROUP_ID" ]; then
@@ -103,10 +110,131 @@ groups www-data
# 检查目录的实际权限
echo "检查目录权限..."
ls -ld $APP_ROOT/runtime/log
ls -ld $APP_ROOT/runtime/cache
ls -ld $APP_ROOT/runtime/temp
ls -ld $APP_ROOT/upload
for dir in "${directories[@]}"; do
echo "检查目录权限: $dir"
ls -ld "$APP_ROOT/$dir"
done
echo "检查setgid权限..."
for dir in "${directories[@]}"; do
echo "检查setgid权限: $dir"
stat -c '%a %n' "$APP_ROOT/$dir" | grep 's'
done
echo "=== 检测 PHP 执行用户 ==="
# 检查命令可用性函数
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# 方法1通过 PHP 脚本检测当前用户
echo "方法1: 通过 PHP 检测当前执行用户"
if command_exists php; then
php -r "
if (function_exists('get_current_user')) {
echo '当前 PHP 执行用户: ' . get_current_user() . PHP_EOL;
} else {
echo 'get_current_user() 函数不可用' . PHP_EOL;
}
if (function_exists('getmyuid')) {
echo '当前用户 ID: ' . getmyuid() . PHP_EOL;
} else {
echo 'getmyuid() 函数不可用' . PHP_EOL;
}
if (function_exists('getmygid')) {
echo '当前组 ID: ' . getmygid() . PHP_EOL;
} else {
echo 'getmygid() 函数不可用' . PHP_EOL;
}
if (function_exists('posix_geteuid') && function_exists('posix_getpwuid')) {
echo '当前进程用户: ' . posix_getpwuid(posix_geteuid())['name'] . PHP_EOL;
} else {
echo 'posix 函数不可用,尝试系统方法' . PHP_EOL;
echo '当前用户: ' . exec('whoami') . PHP_EOL;
}
" 2>/dev/null || echo "PHP 执行检测失败"
else
echo "❌ PHP 命令不可用,跳过检测"
fi
# 方法2通过进程检测 PHP-FPM 运行用户
echo "方法2: 检测 PHP-FPM 进程用户"
if command_exists pgrep; then
if pgrep php-fpm > /dev/null 2>&1; then
if command_exists ps; then
ps aux | grep php-fpm | grep -v grep | head -3 2>/dev/null || echo "进程检测失败"
else
echo "ps 命令不可用,但找到 PHP-FPM 进程"
fi
else
echo "PHP-FPM 进程未运行"
fi
else
echo "pgrep 命令不可用,尝试其他方法"
if command_exists ps; then
echo "使用 ps 直接检测:"
ps aux | grep php-fpm | grep -v grep | head -3 2>/dev/null || echo "未找到 PHP-FPM 进程"
else
echo "ps 和 pgrep 命令都不可用,无法检测进程"
fi
fi
# 方法3检测配置文件中的用户设置
echo "方法3: 检查配置文件中的用户设置"
# PHP-FPM 配置检查
php_fpm_configs=(
"/usr/local/etc/php-fpm.d/www.conf"
"/etc/php-fpm.d/www.conf"
"/usr/local/etc/php-fpm.conf"
"/etc/php-fpm.conf"
)
for config_file in "${php_fpm_configs[@]}"; do
if [ -f "$config_file" ]; then
echo "找到 PHP-FPM 配置: $config_file"
if command_exists grep; then
echo "PHP-FPM 用户配置:"
grep -E "^user|^group" "$config_file" 2>/dev/null || echo "未找到用户配置行"
else
echo "grep 命令不可用,但配置文件存在"
fi
break
fi
done
# Nginx 配置检查
nginx_configs=(
"/etc/nginx/nginx.conf"
"/usr/local/nginx/conf/nginx.conf"
"/etc/nginx/conf/nginx.conf"
)
for config_file in "${nginx_configs[@]}"; do
if [ -f "$config_file" ]; then
echo "找到 Nginx 配置: $config_file"
if command_exists grep; then
echo "Nginx 用户配置:"
grep -E "^user" "$config_file" 2>/dev/null || echo "未找到用户配置行"
else
echo "grep 命令不可用,但配置文件存在"
fi
break
fi
done
# 方法4使用 /proc 文件系统检测(如果其他方法都失败)
echo "方法4: 使用 /proc 文件系统检测"
if [ -d "/proc" ]; then
echo "当前脚本执行用户: $(whoami 2>/dev/null || echo 'whoami 命令不可用')"
echo "当前脚本用户 ID: $(id -u 2>/dev/null || echo 'id 命令不可用')"
else
echo "/proc 目录不可用"
fi
echo "=== 启动应用 ==="