使用 apache config 和 htaccess 为开发和生产动态设置配置变量 [英] Using apache config and htaccess to dynamically set config vars for dev and production
问题描述
我需要能够在 Apache 站点配置中设置一个环境变量,用于定义站点是开发、测试还是生产.
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 中使用 web 根 htaccess 中定义的变量之一.
ALSO I need to use one of the variables defined in the web roots htaccess in another htaccess located in another directory.
我想在他们的 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/.htaccess
中,您可以使用 IF、Else 和 ElseIf 指令:
And in /www/example.com/prod/web/sam/.htaccess
you can use the IF, Else and ElseIf directives:
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屋!