使用apache config和htaccess为dev和production动态设置配置变量

时间:2022-10-06 08:08:18

Short Summary

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.

我需要能够在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

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

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

我还需要在位于另一个目录中的另一个htaccess中使用web根htaccess中定义的变量之一。

Site Configuration

Apache config (dev/beta)

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

我想在他们的Apache站点配置文件中为我的dev和beta环境设置一个环境变量

Something like this:

(Im sure its not the correct syntax)

(我确定它不是正确的语法)

SetEnv environment_type dev

- or -

- 要么 -

SetEnv environment_type beta


Apache Config (Production)

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

生产站点apache config可能根本不设置它,或者它可以:

SetEnv environment_type prod

But this would be optional

但这是可选的


.htaccess (Set site config vars)

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:

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

(Again, just mockup syntax)

(再次,只是模型语法)

<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>


Using Defined Settings

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

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


Other .htaccess File

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

我有一个驻留在子目录中的另一个.htaccess文件,其目的是为该区域强制执行基本的http身份验证。

The Problem

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

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

What I need:

我需要的:

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>

Current .htaccess:

目前.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>


config.php

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

我已经知道这有效我的php配置文件可以获得配置值,如此

<?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');
?>

1 个解决方案

#1


11  

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

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

SetEnv ENVIRONMENT_TYPE "DEV"

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.

此外,鉴于htaccess是从服务器配置向下解析到​​相关目录,直到Apache认为可以提供请求的目录,您可以在其他htaccess文件中使用先前定义的环境变量。

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指令根据其他环境变量的值生成环境变量。

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

此外,在SetEnvIf指令中,您只能检查也由SetEnvIf创建的环境变量:

E.g. /etc/apache2/sites-enabled/001-project

例如。 / 001项目的/ etc / apache2的/启用的站点 -

# 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"

And in /www/example.com/.htaccess

在/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"

And in /www/example.com/prod/web/sam/.htaccess you can use the IF, Else and ElseIf directives:

在/www/example.com/prod/web/sam/.htaccess中,您可以使用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>

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

然后在PHP中,您可以使用getenv()或:访问环境变量:

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

#1


11  

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

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

SetEnv ENVIRONMENT_TYPE "DEV"

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.

此外,鉴于htaccess是从服务器配置向下解析到​​相关目录,直到Apache认为可以提供请求的目录,您可以在其他htaccess文件中使用先前定义的环境变量。

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指令根据其他环境变量的值生成环境变量。

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

此外,在SetEnvIf指令中,您只能检查也由SetEnvIf创建的环境变量:

E.g. /etc/apache2/sites-enabled/001-project

例如。 / 001项目的/ etc / apache2的/启用的站点 -

# 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"

And in /www/example.com/.htaccess

在/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"

And in /www/example.com/prod/web/sam/.htaccess you can use the IF, Else and ElseIf directives:

在/www/example.com/prod/web/sam/.htaccess中,您可以使用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>

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

然后在PHP中,您可以使用getenv()或:访问环境变量:

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