Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
www-data:x:33:33:www-data:/var/www:/bin/sh

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

1 Answer 1

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

 
discard

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.