如何在Apache的httpd.conf文件中定义变量?

74

我想在Apache服务器的httpd.conf配置文件中定义一个变量。

例如:变量静态路径 = C:\codebase\snp_static

我希望在httpd.conf中任何需要的地方使用这个变量(静态路径)。

请告诉我如何在httpd.conf文件中定义一个变量?


1
请检查以下示例的响应:http://stackoverflow.com/questions/6343621/conditions-in-apache/6346407#6346407 - regilero
这个问题应该移动到ServerFault吗?https://serverfault.com/help/on-topic - Sybille Peters
另请参阅http://httpd.apache.org/docs/trunk/mod/mod_macro.html-这通常是解决问题的更好方法,并且自2.4.5以来已包含在默认发行版中。 - dualed
5个回答

148
httpd.conf 文件中,使用以下命令声明您的变量:Define(最好在第一行)
语法Define 变量名 变量值 以此方式进行声明:
#The line below creates the variable [static_path]
Define static_path C:/codebase/snp_static

你可以稍后像这样使用这个变量:
ServerRoot = ${static_path}
...
DocumentRoot = ${static_path}
...
<Directory ${static_path}>
...etc.

您甚至可以将多个变量结合起来:

#Below, I am going to combine variables [server_space] and [static_path]
Define server_space c:/
Define static_path codebase/snp_static
...
ServerRoot = ${server_space}${static_path}
...
DocumentRoot = ${server_space}${static_path}
...
<Directory ${server_space}${static_path}>
...etc.

文档: http://httpd.apache.org/docs/2.4/mod/core.html#define

这是一个关于Apache HTTP服务器核心模块的文档链接。

11
Apache 2.2 默认未包含 mod_define 模块。 - craigrs84
1
旧的原始(已与2.0和2.2兼容)mod_define位于http://people.apache.org/~rjung/mod_define/mod_define.html。 - reallynice
2
这绝对是在httpd conf文件中定义变量的理想方式。我希望apachectl的开发人员能够看到这个问题。 - Spencer Williams
@Radon8472 我可能错了,但我认为你可以在2.2上使用[define]“命令”(或者无论正确的名称是什么)而不需要mod_define。毕竟,提供的文档也适用于2.4(及以上版本)。但以防万一我真的非常错误:https://people.apache.org/~rjung/mod_define/mod_define.html 你有没有尝试在你的服务器上不进行修改地使用它? - Omar
@Omar,我认为创建这样的问题只会导致很多“自己编译源代码”的答案。我希望有人能提供预编译文件的下载链接。 - Radon8472
显示剩余12条评论

9
如果您只需要在httpd.conf中进行简单的变量替换,则为运行Apache的用户定义一个普通的shell环境变量,然后使用${ENVVAR}语法在httpd.conf文件中引用它,参见Apache文档

5

Apache 2.4 我进行了研究,这是我找到并测试成功的解决方法,使用 httpd_z.exe -t -D DUMP_RUN_CFG 进行测试。

   RESULTS:::
    ServerRoot: "C:/path/core/apache2"
    Main DocumentRoot: "C:/path/apache/htdocs"
    Main ErrorLog: "C:/path/core/apache2/logs/error.log"
    Mutex rewrite-map: using_defaults
    Mutex default: dir="C:/path/core/apache2/logs/" mechanism=default
    PidFile: "C:/path/core/apache2/logs/httpd.pid"
    Define: DUMP_RUN_CFG
    Define: US_ROOTF=C:/path   **THIS IS THE ROOT PATH VARIABLE I JUST MADE**
    Define: php54


  #<IfDefine TEST>
    #  Define servername test.example.com
    #</IfDefine>
    #<IfDefine !TEST>
    #  Define servername www.example.com
    #  Define SSL
    #</IfDefine>
    #DocumentRoot /var/www/${servername}/htdocs
<IfDefine US_ROOTF>
 Define US_ROOTF C:/PATH   **The path i want the variable for**
</IfDefine>
<IfDefine !US_ROOTF>
 Define US_ROOTF C:/PATH    **The path i want the variable for**
#  Define SSL
</IfDefine>
#DocumentRoot /var/www/${servername}/htdocs  OPTIONS ON HOW TO USE

EXAMPLE of use 
ServerRoot = ${US_ROOTF}
<IfDefine php54>
  LoadFile "${US_ROOTF}/core/php54/icudt53.dll"
PHPIniDir "${US_ROOTF}/core/php54/php_production.ini"

有人曾经告诉过我,在将内容提供给互联网时,永远不要使用直接的硬路径,而应该使用变量来帮助保护您的系统。

我通过自己的痛苦经历证实了这一点。现在,我终于找到了如何为处理 Apache 的所有服务设置变量的方法。

希望对您有所帮助。


1
从管理角度来看,使用变量更新和维护服务器会更加容易,需要删除旧路径时,而不是在http.conf的每行以及任何其他适用的地方查找这些直接路径,同时也要考虑安全性(如果适用)。 - Omar

4

虽然来晚了,但最近我遇到了这个问题,并且像这样解决了它:

DEFINE path "C:\path/to the/directory"

稍后可以这样使用:

DocumentRoot ${path} <Directory ${path}>

注意:在路径中,在驱动器字母后使用 \


1
如果您的Apache项目没有使用我们添加到bashrc的系统环境变量,我们可以直接将变量导出到/etc/apache2/envvars文件中。
例如:export ADMIN='Bibin'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接