This commit is contained in:
2025-12-25 16:51:19 +08:00
parent 72f6b341a1
commit 46135002f9
2 changed files with 10 additions and 87 deletions

View File

@@ -1,47 +1,12 @@
#!/bin/bash
# 移除 set -e 以便更好的错误控制
# 显示帮助信息
show_help() {
cat << EOF
=== Web应用权限初始化脚本 ===
用法: $0 [--help|-h]
参数:
--help, -h 显示此帮助信息
优先级:
1. 环境变量 PHP_APP_ROOT
2. 默认值 /var/www/html
示例:
$0 # 使用环境变量或默认值
PHP_APP_ROOT=/custom $0 /var/www # 参数优先,忽略环境变量
EOF
}
echo "=== Web应用权限初始化 ==="
# 默认值
APP_ROOT="/var/www/html"
SOURCE="默认值"
# 定义应用根目录,优先使用环境变量,否则使用默认值
APP_ROOT="${PHP_APP_ROOT:-/var/www/html}"
if [ -n "${PHP_APP_ROOT}" ]; then
# 其次使用环境变量
APP_ROOT="${PHP_APP_ROOT}"
SOURCE="环境变量 PHP_APP_ROOT"
fi
echo "使用应用根目录: $APP_ROOT (来源: $SOURCE)"
# 验证应用根目录是否有效
if [ ! -d "$APP_ROOT" ]; then
echo "⚠️ 警告: 应用根目录 '$APP_ROOT' 不存在, 程序退出"
exit 1
fi
echo "使用应用根目录: $APP_ROOT"
# 创建统一的Web组并配置所有用户最高效的权限管理
configure_web_users() {
@@ -103,9 +68,9 @@ configure_web_users() {
if id "$web_user" &>/dev/null; then
echo "📝 处理Web用户: $web_user"
# 获取用户当前组信息(安全的变量处理)
current_groups=$(id -Gn "$web_user" 2>/dev/null | tr '\n' ' ' | sed 's/ *$//')
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
@@ -292,8 +257,8 @@ EOF
# 执行权限测试
if [ -f "$test_file" ]; then
echo "使用测试文件: $test_file"
echo "文件权限: $(stat -c '%a %n' \"$test_file\")"
echo "文件所有者: $(stat -c '%U:%G' \"$test_file\")"
echo "文件权限: $(stat -c '%a %n' "$test_file")"
echo "文件所有者: $(stat -c '%U:%G' "$test_file")"
# 测试所有Web用户的权限通过组权限
for test_user in "www-data" "www" "apache" "nginx"; do
@@ -370,12 +335,4 @@ fi
echo "=== 启动应用 ==="
# 执行原有的启动命令
# 移除第一个参数(应用根目录),只传递后续参数给应用
if [ $# -gt 0 ]; then
# 如果有应用根目录参数,传递剩余参数
shift 1
exec "$@"
else
# 没有额外参数,正常执行
exec "$@"
fi
exec "$@"