You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Tom Hoefakker <tw...@iw.net> on 2006/01/19 02:37:05 UTC

Re: [users@httpd] Using environment variable in httpd.conf

As far as I know, it's an undocumented "feature".  But, in my 
experience, it does actually work quite well.  While I vaguely recall a 
note on the development list questioning whether or not this should be 
removed, I would love to see this become a full-fledged documented 
feature as it is very useful in situations such as yours.

In your httpd.conf file you can use environment variables like this:
${varname}

In our environment, we use environment variables for quite a few things:
ServerRoot
Port
DocumentRoot
module paths - both bundled and custom modules
User & group  (different users in development vs. production)
path to piped log program
log file path
etc...

Hope this helps.
Tom



Khai Doan wrote:
> I am a mod_perl programmer, and I also manage my company web server.  We 
> are growing and constantly adding new features to our products.  I find 
> myself making change to the httpd.conf very frequent.  Sometimes, I 
> change httpd.conf on the development server, and later forget to apply 
> the change to httpd.conf on staging server, and production servers.  I 
> wish to have the httpd.conf in my company cvs repository and get pushed 
> out along with the rest of the application code.  Unfortunately quite a 
> few things in httpd.conf stand in the way, such as hardcoded paths, and 
> port numbers.  (We are in an environment where all developers have to 
> develop on one development server, and each developer is assigned a port 
> number, and run a separate apache process listening on that port.)  I 
> also want other developers to be able to check in changes to the 
> httpd.conf file. 
>  
> If I set up environment variables to hold these hardcoded paths, and 
> port numbers, can I use these environment variables in httpd.conf? 
>  
> If I am to write a module in C to handle this, where can I start?  What 
> book do you recommend?
>  
> Khai
> --------------------------------------------------------------------- 
> The official User-To-User support forum of the Apache HTTP Server 
> Project. See for more info. To unsubscribe, e-mail: 
> users-unsubscribe@httpd.apache.org " from the digest: 
> users-digest-unsubscribe@httpd.apache.org For additional commands, 
> e-mail: users-help@httpd.apache.org

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by Tom Hoefakker <tw...@iw.net>.
looks like you've gotten additional very useful info. To answer your 
earlier questions, we're currently running 2.0.54 on AIX 5.2

We're also using a heavily modified apachectl script which is reads 
delimited instance parameters from records in a "table" file (simple 
grep/awk).  This allows us to associate a profile with each instance 
which can export environment variables as needed by that respective 
instance.

Tom

httpd2@karsites.net wrote:
> Of course Joshua.
> 
> But it is nice to know that th functionality is there if 
> it's needed.
> 
> Keith
> 
> In theory, theory and practice are the same;
> In practice they are not. 
> 
> On Thu, 19 Jan 2006, Joshua Slive wrote:
> 
>> To: users@httpd.apache.org
>> From: Joshua Slive <jo...@slive.ca>
>> Subject: Re: [users@httpd] Using environment variable in httpd.conf
>>
>> If you are already using mod_perl, then this is a legitimate
>> possibility.  But if you aren't using mod_perl, adding it would be way
>> overkill for this problem.
>>
>> Joshua.
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 
> 

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by ht...@karsites.net.
Of course Joshua.

But it is nice to know that th functionality is there if 
it's needed.

Keith

In theory, theory and practice are the same;
In practice they are not. 

On Thu, 19 Jan 2006, Joshua Slive wrote:

> To: users@httpd.apache.org
> From: Joshua Slive <jo...@slive.ca>
> Subject: Re: [users@httpd] Using environment variable in httpd.conf
>
> If you are already using mod_perl, then this is a legitimate
> possibility.  But if you aren't using mod_perl, adding it would be way
> overkill for this problem.
> 
> Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by Joshua Slive <jo...@slive.ca>.
On 1/19/06, httpd2@karsites.net <ht...@karsites.net> wrote:
>
> Well, I have just found something interesting while reading
> through Pro Apache 3rd edition, by Peter Wainwright.
>
> Apparently, using mod_perl, you can use the full power of
> the pearl interpreter INSIDE httpd.conf.

If you are already using mod_perl, then this is a legitimate
possibility.  But if you aren't using mod_perl, adding it would be way
overkill for this problem.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by ht...@karsites.net.
Well, I have just found something interesting while reading 
through Pro Apache 3rd edition, by Peter Wainwright.
 
Apparently, using mod_perl, you can use the full power of 
the pearl interpreter INSIDE httpd.conf.

Quote from the book page 449:

"Embedded Perl appears in Apache's configuration inside a 
<Perl>...</Perl> container, also known as a Perl section.
Anything inside this container is executed by mod_perl when 
Apache starts. You specify configuration directives simply 
by assigning a package variable of the same name. For 
example;

<Perl>
 $ServerAdmin="webmaster@alpha-complex.com";
</Perl>

In Perl, this is a basic assignment of a string to a scalar 
variable. In a Perl section, it becomes a configuration 
directive because the variable corresponds to the name of a 
configuration directive understood by Apache."

Peter Wainwright also provides some very capable examples 
in his book, using this technique.

One example of this starts with:

...
#Now write the Perl script and embed it into httpd.conf in 
# a <Perl> container

<Perl>

# generate virtual hosts on the fly with Perl
 ...
 ...
 ...
# back to httpd.conf
</Perl>

# ...rest of httpd.conf...

and,

<Perl>
# note the following line calls an external perl interpreter
#!/usr/bin/perl -w

# rest of Perl script - embedded in htpd.conf

# back to httpd.conf
<Perl>

#rest of httpd.conf file

He concludes this section of the chapter by stating,

"Of course you could use any scripting language to create a 
configuration file this way because Apache no longer needs 
to know how to interpret it (ie the script) itself."

So, in theory (please see disclaimer below - LOL) it should 
be possible to use an external php interpreter to generate 
httpd.conf files and configuration directives this way, by 
embedding the call to the php interpreter into a 
<Perl>...</Perl> section.

The above was extracted from the full sample chapter 7, 
'Hosting more than one website', available for download from 
Apress's website, at:

http://www.apress.com/book/supplementDownload.html?bID=275&sID=2134

The download link is not underlined, and is the text 
'Download Sample Chapter' which just looks like a 
sub-heading to me.

Pro Apache, Third Edition
Download Sample Chapter <-- 

This is an excellent book, packed with practical 
explanations of how to master Apache. I have struggled with 
the online documentation for years. I cannot recommend this 
book highly enough!

HTH

Keith

In theory, theory and practice are the same;
In practice they are not. 

On Thu, 19 Jan 2006 httpd2@karsites.net wrote:

> To: users@httpd.apache.org
> From: httpd2@karsites.net
> Subject: Re: [users@httpd] Using environment variable in httpd.conf
> 
> 
> Thanks for the info Joshua.
> 
> I was just curious to know if it was possible to set 
> variables in httpd.conf . Probably won't need to 
> use such a feature at the moment.
> 
> Keith
> 
> 
> In theory, theory and practice are the same;
> In practice they are not. 
> 
> On Thu, 19 Jan 2006, Joshua Slive wrote:
> 
> > To: users@httpd.apache.org
> > From: Joshua Slive <jo...@slive.ca>
> > Subject: Re: [users@httpd] Using environment variable in httpd.conf
> > 
> > On 1/19/06, httpd2@karsites.net <ht...@karsites.net> wrote:
> > >
> > > Well Tom, I'm using apache 2.0.50 under SuSE 9.2 pro and
> > > just added the following to httpd.conf, after my LoadModule statements:
> > >
> > > SetEnv ErrLogPath /var/log/apache2/error_log
> > > ErrorLog ${ErrLogPath}
> > > LogLevel debug
> > >
> > > All this does for my setup is create an error_log file under
> > > /srv/www/ called ${ErrLogPath}.
> > >
> > > Am I missing something?
> > 
> > The undocumented env-variable-in-config-file feature looks at
> > variables in the server environment at time of startup.  SetEnv
> > doesn't actually make real environment variables until a cgi script is
> > launched.  See:
> > http://httpd.apache.org/docs/2.2/env.html
> > 
> > What you want sounds more like the features provided by mod_macro. 
> > See http://modules.apache.org/.
> > 
> > But the standard advice in cases like this is: httpd.conf is not a
> > programming language, it is a config file.  If you need more advanced
> > features, then use your favorite preprocess (m4, etc) to generate your
> > config files.
> > 
> > Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by ht...@karsites.net.
Thanks for the info Joshua.

I was just curious to know if it was possible to set 
variables in httpd.conf . Probably won't need to 
use such a feature at the moment.

Keith


In theory, theory and practice are the same;
In practice they are not. 

On Thu, 19 Jan 2006, Joshua Slive wrote:

> To: users@httpd.apache.org
> From: Joshua Slive <jo...@slive.ca>
> Subject: Re: [users@httpd] Using environment variable in httpd.conf
> 
> On 1/19/06, httpd2@karsites.net <ht...@karsites.net> wrote:
> >
> > Well Tom, I'm using apache 2.0.50 under SuSE 9.2 pro and
> > just added the following to httpd.conf, after my LoadModule statements:
> >
> > SetEnv ErrLogPath /var/log/apache2/error_log
> > ErrorLog ${ErrLogPath}
> > LogLevel debug
> >
> > All this does for my setup is create an error_log file under
> > /srv/www/ called ${ErrLogPath}.
> >
> > Am I missing something?
> 
> The undocumented env-variable-in-config-file feature looks at
> variables in the server environment at time of startup.  SetEnv
> doesn't actually make real environment variables until a cgi script is
> launched.  See:
> http://httpd.apache.org/docs/2.2/env.html
> 
> What you want sounds more like the features provided by mod_macro. 
> See http://modules.apache.org/.
> 
> But the standard advice in cases like this is: httpd.conf is not a
> programming language, it is a config file.  If you need more advanced
> features, then use your favorite preprocess (m4, etc) to generate your
> config files.
> 
> Joshua.
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 
> 

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by Joshua Slive <jo...@slive.ca>.
On 1/19/06, httpd2@karsites.net <ht...@karsites.net> wrote:
>
> Well Tom, I'm using apache 2.0.50 under SuSE 9.2 pro and
> just added the following to httpd.conf, after my LoadModule statements:
>
> SetEnv ErrLogPath /var/log/apache2/error_log
> ErrorLog ${ErrLogPath}
> LogLevel debug
>
> All this does for my setup is create an error_log file under
> /srv/www/ called ${ErrLogPath}.
>
> Am I missing something?

The undocumented env-variable-in-config-file feature looks at
variables in the server environment at time of startup.  SetEnv
doesn't actually make real environment variables until a cgi script is
launched.  See:
http://httpd.apache.org/docs/2.2/env.html

What you want sounds more like the features provided by mod_macro. 
See http://modules.apache.org/.

But the standard advice in cases like this is: httpd.conf is not a
programming language, it is a config file.  If you need more advanced
features, then use your favorite preprocess (m4, etc) to generate your
config files.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Using environment variable in httpd.conf

Posted by ht...@karsites.net.
Well Tom, I'm using apache 2.0.50 under SuSE 9.2 pro and 
just added the following to httpd.conf, after my LoadModule statements:

SetEnv ErrLogPath /var/log/apache2/error_log
ErrorLog ${ErrLogPath}
LogLevel debug

All this does for my setup is create an error_log file under 
/srv/www/ called ${ErrLogPath}.

Am I missing something?

What version of Apache are you using?

Do you have any concrete examples of usage please?

Keith

In theory, theory and practice are the same;
In practice they are not. 

On Wed, 18 Jan 2006, Tom Hoefakker wrote:

> To: users@httpd.apache.org
> From: Tom Hoefakker <tw...@iw.net>
> Subject: Re: [users@httpd] Using environment variable in httpd.conf
> 
> As far as I know, it's an undocumented "feature".  But, in my experience,
> it does actually work quite well.  While I vaguely recall a note on the
> development list questioning whether or not this should be removed, I
> would love to see this become a full-fledged documented feature as it is
> very useful in situations such as yours.
> 
> In your httpd.conf file you can use environment variables like this:
> ${varname}
> 
> In our environment, we use environment variables for quite a few things:
> ServerRoot
> Port
> DocumentRoot
> module paths - both bundled and custom modules
> User & group  (different users in development vs. production)
> path to piped log program
> log file path
> etc...
> 
> Hope this helps.
> Tom

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org