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

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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