How to use the setEnv variable in apache? - 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!'

Related

How to set and use global variables in httpd.conf under MacOSX

In Linux/Ubuntu etc we have a file /etc/apache/envvars where we can define variables to be used across the Apache config but I cannot find such in MacOSX
What i'm trying to do is - create a variable - and use it inside a vhost.
# httpd.conf
SetEnv MY_PATH="/my/path/to/bin"
IncludeOptional="/home/user/website.conf"
# website.conf
<VirtualHost *:80>
SetEnv PATH "./current/app/bin:${MY_PATH}:${PATH}"
^^ ^^
not working works
</VirtualHost>
Since i have many websites - I want them all to use a "base" variable + app specific variables.
But the variable I created in httpd.conf can be used only the webapp itself ( os.getenv() i.e ) - it cannot be use with a ${MY_PATH} inside Apache.
I found somewhere that people edit this file - but it is not editable even with sudo, so i don't think it's the right choice:
/System/Library/LaunchDaemons/org.apache.httpd.plist
How can I create global variables ? or export variables like envvars file in other OS's ?

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?

Apache ElseIf block entered even though condition is false

Back Story:
I have a copy of all my website files located on two different file servers. I have a two web servers, mimir2 and mimir4, with maps to access the files from either of these file servers. The unique value in the file path to these file servers is either dtg_devel_ah or dtg_devel_nr.
What I am trying to do:
I am editing my httpd.conf file to dynamically set, on server start, which file server to use based on the web servers name. I am doing this by using Apaches Define directive to create the variable dtg_devel_path which will get used in directory paths throughout the rest of the httpd.conf file.
My Code to accomplish this (Note: There are SetEnv declrations scattered throughout only for my debugging purposes):
# Define which dtg_devel_xx to use based on server name. This is so that
# the server is accessing its local files instead of across the country
# Default to dtg_devel which will point to which ever server site is active
Define dtg_devel_path dtg_devel
# If running on mimir2 use dtg_devel_nr
# else if run on mimir4 use dtg_devel_ah
<If "env('HOSTNAME') =~ /mimir2/i">
Define dtg_devel_path dtg_devel_nr
SetEnv dtg_devel_m2 ${dtg_devel_path}
SetEnv dtg_devel_hostname_m2 ${HOSTNAME}
</If>
<ElseIf "env('HOSTNAME') =~ /mimir4/i">
Define dtg_devel_path dtg_devel_ah
SetEnv dtg_devel_m4 ${dtg_devel_path}
SetEnv dtg_devel_hostname_m4 ${HOSTNAME}
</ElseIf>
SetEnv dtg_devel_final_set_path ${dtg_devel_path}
SetEnv dtg_devel_hostname ${HOSTNAME}
When I run this the values getting set are:
My Problem:
The value of dtg_devel_path should be dtg_devel_nr. I am running this on server mimir2 which you can see is confirmed from the above picture. The if part of the if/elseif statement is returning true (HOSTNAME equals mimir2) and in turn setting dtg_devel_path, dtg_devel_hostname_m2, and dtg_devel_m2. Strangely though somehow dtg_devel_path then gets changed to the value dtg_devel_ah even though the elseif block never gets entered. This is confirmed by the value of the dtg_devel_final_set_path variable and the lack of values for dtg_devel_m4 and dtg_devel_hostname_m4. This code block is the only location that the string dtg_devel_ah appears so this has to be where it is getting set.
Does anyone see the mistake that I have in my code? Does anyone have any suggestions? Any help would be greatly appreciated!! If any additional information is needed please let me know.
I could not figure out how to solve this completely through Apache so I ended up going a slightly different path. My web server is running on a RHEL 6.4 machine fyi.
I basically pulled the if/else function that I had in the http.conf file and put it into three places; /etc/profile.d/dtg_deve_path.<sh,csh> and /etc/init.d/httpd. These files check the host name and create a DTG_DEVEL_PATH environment variable based on that value. I then access that value in httpd.conf with a Define dtg_devel_path ${DTG_DEVEL_PATH} which then lets me accomplish what I was trying to do, use ${dtg_devel_path} through the rest of the httpd.conf file to dynamically specify which file server to use.