SetEnv APPLICATION_ENV development - .htaccess interacting with Zend Framework? - apache

I have the following on my htaccess.
SetEnv APPLICATION_ENV development
When I pass this file to prodution, I will change it to:
SetEnv APPLICATION_ENV production
This
development
and
production
are set on Zend Framework application.ini correct ?
How does Zend and Apache communicate here? How does Zend knows about that htaccess instruction?
Thanks.

SetEnv, used in Apache's configuration (be it a .htaccess file, or a VirtualHost), defines an environment variable.
From PHP, you can read environment variables either :
using the getenv() function.
Or in the $_SERVER or $_ENV superglobal variables
Taking a look at the given index.php in Zend Frameworks QuickStart, you'll see it uses that environment variable the define the PHP constant called APPLICATION_ENV :
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
And that constant is later used to initialize the application :
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);

The flow of communication, as you call it, is the followoing:
If you use
SetEnv APPLICATION_ENV production
in your .htaccess, the environment you set there, will be used. Why?
The following piece of code from your index.php doesn't define the constant, if it has been defined already, which is the case, if you use SetEnv in your .htaccess.
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
If your .htaccess doesn't define the constant, the value provided in the index.php will be used. If I were you, I would still keep it in sync. Because you may make mistakes like forgetting to set AllowOverride for your vhost which would result in a situation where the environment is set by the index.php even though the .htaccess is present.

Related

SetEnv inherit from Host htaccess

With my htaccess file, I'm setting this environment variable
SetEnv CI_ENV development
But I actually want to inherit the environment variable from the host. Something like
SetEnv CI_ENV %{CI_ENV}
I've tried:
SetEnv CI_ENV %{ENV:CI_ENV}
But that doesn't seem to work.
Reason: I'm using docker with a PHP/Apache container. Trying to pass configuration value into it so I'd like to not have anything hardcoded in the htaccess file if possible.
My current workaround is to have a .htaccess.config file with $CI_ENV in the file, and then using a script to substitute the env values, but would be nice to not have to do that.
You don't need to redeclare env variables if you use mod_setevif.
Use this directive in your root .htaccess:
SetEnvIf Host ^ CI_ENV=development
Then use it anywhere in sub-directories without any need of re-delcaring it.
EDIT: Based on comments below, it seems OP is trying to pass system (shell) env variables to Apache.
For that PassEnv directive can be used like this:
# pass system env CI_ENV to Apache
PassEnv CI_ENV
Now CI_ENV will be available as env variables in various Apache directives.

Allow cPanel to manage SetEnv parameters

We have a application that makes use of an Apache environment parameter to determine what config it should use. Around 5 instances of the application are is hosted on one cPanel / WHM development server.
We could use the .htaccess to set the right environment, but that results in uncommitted changes. cPanel should handle this variable via the vhost file, basically each vhost should have a different value:
SetEnv APP_ENV dev
SetEnv APP_ENV test
SetEnv APP_ENV trans
So, how can we let cPanel handle environment variables? It doesn't seem that this is possible, given the SetEnv can't be found in the vhost template.
https://blog.rac.me.uk/2013/05/29/new-relic-cpanel-apache-and-fastcgi-php/
We can do this with custom php.ini file (most of the hosting provider give option to create custom php.ini file, create custome php.ini file for each domain )
Add configuration in custom php.ini file
APP_ENV="dev"
form php code we can use get_cfg_var() function to retrieve
<?php
echo "current env is : ".get_cfg_var('APP_ENV');
?>
Output

Apache httpd-vhost SetEnv variable not working in application environment

In my application I am trying to set the env variabel in the httpd-vhost config instead of insde .htacces file. If I put the setEnv variabel inside the .htaccess variable it works fine but not handy to keep .htaccess sync with production. I've tried as can be seen in my screenshot to set it inside the httpd config. I've restarted apache to load the new configuration.
Any advice or tips are welcome and thanks in advance.
When I set the setEnv variabel outside the it seems to work?

How to access rewrite rule environment variables set as SetEnv?

I want to access an environment variable inside the rewrite rules which is previously set using SetEnv
e.g.,
SetEnv intl UK
How do I access this "intl" inside the rules later (not in PHP or any other script)?
Sorry, you can't.
The SetEnv directive runs late during request processing meaning that
directives such as SetEnvIf and RewriteCond will not see the variables
set with it.
Please refer to this previous question.
RewriteCond with SetEnv

How to use the setEnv variable in apache?

I need to set my apache environment to 'foobar'
I know I need to set in in my vhost, but what should I type there and where?
SetEnv sets a particular variable to some value, so you need something like
SetEnv varname varvalue
If this is for a specific virtual host, and you have access to the Apache configuration files, this would go inside the <VirtualHost> directive for that virtual host.
If you don't have control of the config files, you'll need to put it in a .htaccess file. But for this to work, SetEnv must be allowed in .htaccess files, which is specified using the AllowOverride directive.
I came here because I configured a python/django WSGI environment.
Loading modules and SetEnv'ing all day long.
Did not work. The vars would just not show up.
What did work was this: https://gist.github.com/GrahamDumpleton/b380652b768e81a7f60c
Graham describes how to basically clone your wsgi.py file and then use a different one for each environment (production, test, dev) and setting your environment vars in there with os.environ['whatever'] = 'you like!'