使用apache config和htaccess为开发和生产动态设置配置变量 [英] Using apache config and htaccess to dynamically set config vars for dev and production

查看:93
本文介绍了使用apache config和htaccess为开发和生产动态设置配置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在Apache站点配置中设置一个环境变量,以定义该站点是dev,beta还是生产版本。

I need to be able to set an environment variable in the Apache site config that defines if the site is dev, beta or production.

Web根目录中的htaccess文件将根据Apache配置中的条目定义网站的配置变量(数据库凭据,域等)

The htaccess file in the web root will define config variables for the website (database credentials, domain, etc) based on the entry in the Apache configuration

我还需要在另一个目录下的另一个htaccess中使用网络根目录htaccess中定义的变量之一。

ALSO I need to use one of the variables defined in the web roots htaccess in another htaccess located in another directory.

我想为我的开发人员和Beta环境在其Apache网站配置文件中

I would like to set an environment variable for my dev and beta environments in their Apache site config files

(确定其语法不正确)

SetEnv environment_type dev

-或-

SetEnv environment_type beta


生产站点的apache配置要么根本无法设置,要么可以做到:

The production sites apache config could either not set it at all, or it could do:

SetEnv environment_type prod

但这是可选的


用于数据库连接,域名等的配置变量将在公共Webroot的.htaccess文件中进行设置,如下所示:

The configuration variables used for database connection, domain name and so on, would then be set in the .htaccess file in the public webroot like so:

(同样,只是模型语法)

<If (environment_type exists) && (environment_type == dev)>
    SetEnv base_dir /www/example.com/dev/
    SetEnv db_host localhost
    SetEnv db_name devdb
    SetEnv db_user devuser
    SetEnv db_password devpass
    SetEnv web_domain www.devdomain.com
    SetEnv cookie_domain .devdomain.com
<elseif (environment_type exists) && (environment_type == beta)>
    SetEnv base_dir /www/example.com/beta/
    SetEnv db_host localhost
    SetEnv db_name betadb
    SetEnv db_user betauser
    SetEnv db_password betapass
    SetEnv web_domain www.betadomain.com
    SetEnv cookie_domain .betadomain.com
<else>
    SetEnv base_dir /www/example.com/www/
    SetEnv db_host localhost
    SetEnv db_name proddb
    SetEnv db_user produser
    SetEnv db_password prodpass
    SetEnv web_domain www.proddomain.com
    SetEnv cookie_domain .proddomain.com
</If>


不仅需要在我的php脚本中,而且还要在子目录中的另一个.htaccess文件中使用这些新的配置变量

I need to be able to use these new config variables, not only in my php scripts, but also within another .htaccess file in a subdirectory


我还有另一个。 htaccess文件位于子目录中,其目的是对该区域执行基本的http身份验证。

I have another .htaccess file that resides in a sub-directory, and its purpose is to enforce basic http authentication for that area.

指向.htpasswd文件的AuthUserFile条目必须基于环境变量是动态的。

The AuthUserFile entry that points to the .htpasswd file must be dynamic based on the environment variables.

我需要什么:

AuthName "Admin Area"
AuthType "Basic"
AuthUserFile base_dir + "/web/sam/.htpasswd"
AuthGroupFile "/dev/null"
ErrorDocument 404 /sam/error.shtml    
<Limit GET POST>
require valid-user
</Limit>

当前.htaccess:

AuthName "Admin Area"
AuthType "Basic"
AuthUserFile "/www/example.com/beta/web/sam/.htpasswd"
AuthGroupFile "/dev/null"
ErrorDocument 404 /sam/error.shtml    
<Limit GET POST>
require valid-user
</Limit>


我已经知道这可行

然后,我的php配置文件可以像这样获取配置值

I already know that this works
My php config files could then get the config values like so

<?php
    $db['default']['hostname'] = getenv('db_host');
    $db['default']['username'] = getenv('db_user');
    $db['default']['password'] = getenv('db_password');
    $db['default']['database'] = getenv('db_name');
?>


推荐答案

您基本上是正确的。您可以使用 SetEnv 设置环境变量:

You're basically right. You can set an environment variable using SetEnv:

SetEnv ENVIRONMENT_TYPE "DEV"

,鉴于 htaccess 是通过相关目录从服务器配置向下解析的,直到Apache认为可以从中提供请求的目录为止,您可以在其他 htaccess 文件。

Additionally, given that htaccess is parsed from server configuration down through the relevant directories until the directory that Apache thinks the request can be served from, you can use previously defined environment variables in other htaccess files.

但是,您不能合并/连接环境变量,只能使用 SetEnvIf 指令根据其他环境变量的值生成环境变量。

However, you can't combine/concatenate environment variables, you can only use the SetEnvIf directive to generate environment variables based on the value of other environment variables.

此外,在 SetEnvIf 指令中,您只能检查那些也由 SetEnvIf 创建的:

Additionally, within a SetEnvIf directive, you can only inspect environment variables that have also been created by SetEnvIf:

例如 / etc / apache2 / sites-enabled / 001-project

# This has to be created by SetEnvIf, I've set the regex to match
# anything as we want the environment variable set always
SetEnvIf Host .* ENVIRONMENT_TYPE "Prod"

# Alternatively you could do:
SetEnvIf Host prod\.domain\.com ENVIRONMENT_TYPE "Prod"
SetEnvIf Host dev\.domain\.com ENVIRONMENT_TYPE "Dev"
SetEnvIf Host (www\.)?domain\.com ENVIRONMENT_TYPE "Live"

/www/example.com/.htaccess

SetEnvIf ENVIRONMENT_TYPE "Prod" DB_USER="test"
SetEnvIf ENVIRONMENT_TYPE "Prod" DB_NAME="database"
SetEnvIf ENVIRONMENT_TYPE "Prod" DB_PASS="passwd"
SetEnvIf ENVIRONMENT_TYPE "Live" DB_USER="live"
SetEnvIf ENVIRONMENT_TYPE "Live" DB_NAME="database"
SetEnvIf ENVIRONMENT_TYPE "Live" DB_PASS="passwd"

/www/example.com/prod/web/sam/.htacc中ess ,则可以使用 IF,Else和ElseIf 指令:

AuthName "Admin Area"
AuthType "Basic"
<If "env('ENVIRONMENT_TYPE') == 'Prod'">
    AuthUserFile "/www/example.com/prod/web/sam/.htpasswd"
</If>
<Else>
    AuthUserFile "/www/example.com/beta/web/sam/.htpasswd"
</Else>
AuthGroupFile "/dev/null"
ErrorDocument 404 /sam/error.shtml    
<Limit GET POST>
    Require valid-user
</Limit>

然后在您的PHP中,您可以使用 getenv()或:

Then in your PHP you can access the environment variables using getenv() or:

echo $_SERVER['ENVIRONMENT_TYPE'], "\n";
$db_user = $_SERVER['DB_USER'];

这篇关于使用apache config和htaccess为开发和生产动态设置配置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆