【问题标题】:How to define a variable in apache's httpd.conf file?如何在 apache 的 httpd.conf 文件中定义一个变量?
【发布时间】:2011-09-27 23:38:36
【问题描述】:

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

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

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

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

【问题讨论】:

标签: apache


【解决方案1】:

httpd.conf 中,声明您的变量:Define(最好在第一行)
语法Define@ 987654326@variable-value

以这种方式:

#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

【讨论】:

  • mod_define 默认不包含在 Apache 2.2 中
  • 旧原版(兼容 2.0 和 2.2)mod_define 位于 people.apache.org/~rjung/mod_define/mod_define.html
  • 这绝对是在 httpd conf 文件中定义变量的理想方式。我确实希望 apachectl 的人偶然发现这个问题。
  • 在哪里可以找到适用于 windows lib apache 2.2 的 mod_define?
  • @Radon8472 我可能弄错了,但我相信您可以在 2.2 上不使用 mod_define 使用 [define] “命令”(或任何适当的名称)。毕竟,提供的文档也适用于 2.4(及更高版本)。但以防万一我真的,真的错了:people.apache.org/~rjung/mod_define/mod_define.html你有没有在你的服务器上尝试过没有修改?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    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"
    

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

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

    希望对你也有帮助。

    【讨论】:

    • 从管理的角度来看,当您需要删除旧路径时,使用变量来更新和维护服务器会变得更加容易,而不是在 http 的每一行上寻找这些直接路径。 conf(和任何其他适用的地方)以及安全性(当它适用时)
    【解决方案4】:

    这个问题迟到了,但最近遇到了这个问题并像这样修复了它:

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

    然后像这样使用:

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

    注意:在路径中使用\盘符后

    【讨论】:

      【解决方案5】:

      如果您的 apache 项目没有使用我们添加到 bashrc 的系统环境变量, 我们可以直接将变量导出到 /etc/apache2/envvars 文件

      示例:export ADMIN='Bibin'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 1970-01-01
        • 2011-02-19
        • 2019-05-11
        • 1970-01-01
        • 2013-01-04
        • 2021-12-05
        相关资源
        最近更新 更多