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

Sat*_*tya 59 apache

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

例如:变量static_path = C:\codebase\snp_static

我想在httpd.conf需要的地方使用这个变量(static_path).

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

Oma*_*mar 115

httpd.conf中,用以下内容声明您的变量:( Define最好在第一行)
语法:Define variable-name variable-value

以这种方式:

#The line below creates the variable [static_path]
Define static_path C:/codebase/snp_static
Run Code Online (Sandbox Code Playgroud)

您可以稍后使用此变量:

ServerRoot = ${static_path}
...
DocumentRoot = ${static_path}
...
<Directory ${static_path}>
...etc.
Run Code Online (Sandbox Code Playgroud)

您甚至可以组合多个变量:

#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.
Run Code Online (Sandbox Code Playgroud)

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

  • mod_define默认情况下不包含在Apache 2.2中 (8认同)
  • 这绝对是在httpd conf文件中定义变量的理想方式.我希望阿帕奇人能够绊倒这个问题. (2认同)

max*_*olk 6

如果你想要的只是 httpd.conf 中的简单变量替换,那么为运行 Apache 的用户定义一个普通的 shell 环境变量,然后使用 ${ENVVAR} 语法在你的 httpd.conf 文件中引用它,请参阅Apache 文档


小智 5

Apache2.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"
Run Code Online (Sandbox Code Playgroud)

有人告诉我,在向 Internet 提供某些内容时,永远不要使用直接的 HARD 路径访问任何内容,始终使用变量来帮助保护您的系统。

我发现这是真的。现在我终于想出了如何为我使用的所有与 Apache 打交道的服务设置变量。

希望对你也有帮助。