Devin Dixon
Devin Dixon

Reputation: 12413

Apache Server Variable ENV is different on the command line when using Drush

This problem is occuring with Drupal but can be attributed to a general Apache - PHP configuration. In my configuration file I am determing which database to use by a server variable.

if($_SERVER['ENV'] == 'development') {
//Use connection A
} else {
//Use connection B
}

In apache I am setting the env like so:

<VirtualHost *:80>
//Other normal Apache variables set
SetEnv ENV development
</VirtualHost>

In my normal website setting, this works perfectly fine with PHP is accessed through the webserver. But I am trying to run a command using drush, which is on the command line, and it always connects to the wrong database. The problem really is command line php always connects to connection B and I don't think its necessarily drush.

How do I set the ENV to work both on apache on and the command line? The OS is ubuntu.

Upvotes: 1

Views: 1989

Answers (3)

Ricky Levi
Ricky Levi

Reputation: 8007

A generic answer,

Apache has its own environment vs the command-line ENV ( run exportto see the difference ). Apache also include headers/ENV vars that are meaningful only to a web server ( like HTTP_REFERER & DOCUMENT_ROOT ) which are meaningless via command-line ( you don't really have a DOCUMENT_ROOT when using the shell on a regular basis )

The problem in my opinion, starts when you're adding some SetEnv variables in your Apache configuration that you need to also include when you're testing the file via command-line ( such as custom PATH(s) etc .. or a LD_LIBRARY_PATH for Oracle i.e ... )

The bigger problem is that sometimes you can SetEnv variables in your some-local-websiteX.conf and some SetEnv were defined under the global httpd.conf

So I usually do this

1. Print the ENV variables that Apache is using

in my document root I add this small script ( it's in Python but you can adjust it to do the same in PHP )

File: www/env.cgi

#!/usr/bin/python

print "Content-Type: text/html; charset=utf-8\n"


import os
for e in os.environ:
    print "export {key}='{value}' <br />".format(key=e, value=os.environ[e])

2. Creating a custom bash env file

I then copy paste the output in the browser to a local file ~/.bash_apache_env

3. Running my website via commandline

Every terminal window will reset the environment to the defaults ( unless you have some settings in your ~/.bashr i.e ofc ), so I'm running the following in each window I need.

source ~/.bash_apache_env

# then I can execute
www/index.cgi
www/admin.cgi

# etc ..

The #1 & #2 are usually just a one-time thing

Hope it helps...

Upvotes: 0

SpacePope
SpacePope

Reputation: 1423

The --uri switch will inform Drush of what domain to use in the case that HTTP_HOST is unset at runtime.

example: drush --uri=http://uri-to-development cc all

You can add it to the drushrc on each machine so that you don't have to type it every time.

Upvotes: 1

John C
John C

Reputation: 8415

Drush doesn't use Apache to run commands, rather it loads and runs the PHP files directly. To set the variable globally on the server you can edit /etc/environment and add

ENV=development

You can read more about setting environment variables in Ubuntu, check out this help article.

Upvotes: 1

Related Questions