chore(docker): docker/php/entrypoint.sh 支持带参数的这种执行,参数优先于环境变量

This commit is contained in:
2025-12-25 16:07:19 +08:00
parent 0443cc46ec
commit 0cb2dfa647

View File

@@ -1,12 +1,64 @@
#!/bin/bash #!/bin/bash
# 移除 set -e 以便更好的错误控制 # 移除 set -e 以便更好的错误控制
# 显示帮助信息
show_help() {
cat << EOF
=== Web应用权限初始化脚本 ===
用法: $0 [app_root_dir] [--help|-h]
参数:
app_root_dir 应用根目录路径 (可选)
--help, -h 显示此帮助信息
优先级:
1. 命令行参数 (最高优先级)
2. 环境变量 PHP_APP_ROOT
3. 默认值 /var/www/html
示例:
$0 /var/www/html # 使用命令行参数
$0 /data/www # 自定义路径
$0 # 使用环境变量或默认值
PHP_APP_ROOT=/custom $0 /var/www # 参数优先,忽略环境变量
EOF
}
echo "=== Web应用权限初始化 ===" echo "=== Web应用权限初始化 ==="
# 定义应用根目录,优先使用环境变量,否则使用默认值 # 解析命令行参数
APP_ROOT="${PHP_APP_ROOT:-/var/www/html}" # 用法: entrypoint.sh [app_root_dir] [--help|-h]
echo "使用应用根目录: $APP_ROOT" # 检查帮助参数
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
show_help
exit 0
fi
if [ $# -gt 0 ]; then
# 优先使用命令行参数
APP_ROOT="$1"
SOURCE="命令行参数"
elif [ -n "${PHP_APP_ROOT}" ]; then
# 其次使用环境变量
APP_ROOT="${PHP_APP_ROOT}"
SOURCE="环境变量 PHP_APP_ROOT"
else
# 最后使用默认值
APP_ROOT="/var/www/html"
SOURCE="默认值"
fi
echo "使用应用根目录: $APP_ROOT (来源: $SOURCE)"
# 验证应用根目录是否有效
if [ ! -d "$APP_ROOT" ]; then
echo "⚠️ 警告: 应用根目录 '$APP_ROOT' 不存在"
echo "将尝试创建该目录..."
mkdir -p "$APP_ROOT" 2>/dev/null || echo "❌ 无法创建目录 '$APP_ROOT'"
fi
# 创建统一的Web组并配置所有用户最高效的权限管理 # 创建统一的Web组并配置所有用户最高效的权限管理
configure_web_users() { configure_web_users() {
@@ -257,8 +309,8 @@ EOF
# 执行权限测试 # 执行权限测试
if [ -f "$test_file" ]; then if [ -f "$test_file" ]; then
echo "使用测试文件: $test_file" echo "使用测试文件: $test_file"
echo "文件权限: $(stat -c '%a %n' "$test_file")" echo "文件权限: $(stat -c '%a %n' \"$test_file\")"
echo "文件所有者: $(stat -c '%U:%G' "$test_file")" echo "文件所有者: $(stat -c '%U:%G' \"$test_file\")"
# 测试所有Web用户的权限通过组权限 # 测试所有Web用户的权限通过组权限
for test_user in "www-data" "www" "apache" "nginx"; do for test_user in "www-data" "www" "apache" "nginx"; do
@@ -335,4 +387,12 @@ fi
echo "=== 启动应用 ===" echo "=== 启动应用 ==="
# 执行原有的启动命令 # 执行原有的启动命令
exec "$@" # 移除第一个参数(应用根目录),只传递后续参数给应用
if [ $# -gt 0 ]; then
# 如果有应用根目录参数,传递剩余参数
shift 1
exec "$@"
else
# 没有额外参数,正常执行
exec "$@"
fi