5

I have an httpd.conf file that contains the following:

<IfModule unixd_module>
<If "env('OS') == 'Darwin'">
    User daemon
    Group admin
</If>
<Else>
    User www
    Group scm
</Else>
</IfModule>

What I'm trying to do is set the user id for the httpd instance in my development environment on OSX to one user, but a different user when it is deployed to Ubuntu. I'm receiving a syntax error message:

AH00526: Syntax error on line 32 of /usr/local/apps/apache2/conf/httpd.conf:
User not allowed here

I looked at the If directive and it is allowed here. If I comment out the User/Group I do not get the syntax error. Without the IF/Else, and only using one user, this works. If I try to use the condition with the user/group I receive an error. Is this possible in the httpd.conf? If so, then how? Is there a different way to accomplish the same thing? The "OS" environment variable is set in each environment with "export OS=uname". This is in Apache httpd 2.4.4.

4

1 回答 1

5

我终于找到时间更深入地检查它,这是因为 Override 行为。我已经简要检查了 2.4.4 源代码,在我看来,基本上只有当前目录或位置上下文中“覆盖”列表中的指令可以用 If/Else 覆盖。

由于“用户”和“组”应该为整个安装设置一次,而不是由“.htaccess”修改或取决于目录/位置上下文,因此它们不在某些默认的“AllowOverride”列表中,并且您不能简单地将其置于这样的上下文中以使它们可被覆盖。不过,强制用户/组选项仅在 apache 内部“配置树”之上有效是有道理的。

为了以合理的方式实现您需要的行为,正如我在评论中简短解释的那样,您应该使用“envvars”机制,该机制在 Debian(可能也是 Ubuntu)中默认可用。

简而言之,有 /etc/apache2/envvars 文件包含例如

unset HOME
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
    SUFFIX="-${APACHE_CONFDIR##/etc/apache2-}"
else
    SUFFIX=
fi
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=/var/run/apache2/apache2$SUFFIX.pid
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
export APACHE_LOCK_DIR=/var/lock/apache2$SUFFIX
export APACHE_LOG_DIR=/var/log/apache2$SUFFIX
export LANG=C
export LANG

它由 /etc/init.d/apache2 启动脚本获取/继承,因此在 httpd 主配置文件(例如 apache2.conf)中可以使用以下部分

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

对 /etc/apache2/envvars 的简单修改将让你得到你需要的东西。您还可以在 OSX 上使用的用户/组的编译设置中更改用户/组的一些默认值,并仅在 Ubuntu 端使用脚本。

于 2013-06-29T21:13:44.553 回答