Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I asked a question on dba.se about how I can give my Perl CGI script the password it needs to access the MySQL database it needs to query. I have received only one answer but that one suggests I save the password as an environmental variable.

OK, apache2 runs as www-data and according to /etc/passwd, the user's $HOME is /var/www:

$ grep www /etc/passwd

I tried creating /var/www/.profile with this line

export db_pass="password"

And then had my Perl script print $ENV{"db_pass"} but that failed, the variable is empty. I checked with $ENV{PATH} and that was printed as expected.

So, how can I set an arbitrary environmental variable for this user?

The server is an Ubuntu Server running on a VM (which runs on Windows, not my choice, don't ask).

$ apache2 -version
Server version: Apache/2.2.14 (Ubuntu)
Server built:   Feb 14 2012 16:42:25
share|improve this question
up vote 4 down vote accepted

You use Apache environment variable manipulation.

So for example:

SetEnv db_pass swordfish

This can be done in httpd.conf or in .htaccess. You can also set env vars in Rewrite Rules like this:

RewriteRule someurl - [E=dbpass:swordfish]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.