简体   繁体   中英

Apache httpd doesn't load .bashrc

I'm running Python scripts as a CGI under Apache 2.2. These scripts rely on environment variables set in my .bashrc to run properly. The .bashrc is never loaded, and my scripts fail.

I don't want to duplicate my bashrc by using a bunch of SETENV commands; the configuration files will easily get out of sync and cause hard-to-find bugs.

I'm running apache as my user, not as root. I'm starting/stopping it manually, so the /etc/init.d script shouldn't matter at all (I think).

Given these constraints, what can I do to have my .bashrc loaded when my CGI is called?

Edit: I use /usr/sbin/apache2ctl to do the restarting.

What? Surely you don't mean that your scripts rely on configurations in some account's personal home directory.

Apache config files can export environment variables to CGI scripts, etc.

Maybe your program is too dependent on too many environment variables. How about supporting a configuration file: /etc/mypythonprogram.rc . There can be a single environment variable telling the program to use an alternative config file, for flexibility.

Why are you starting/stopping it manually? It would seem that using the init script would be helpful. You can specify the username under which the httpd process should run in the httpd.conf file, and you can specify environment settings as well. For RedHat/CentOs, this would look like:

Add environment settings into the file /etc/sysconfig/httpd (alternately, you can set up a command to read these environment variables from another file, and also set up the same commands in your .bashrc, if necessary -- so there's only one file that contains the environment settings you need).

Modify the /etc/httpd/conf/httpd.conf file so the user that runs the httpd process is a non-root user (by default it should be apache in RedHat/CentOs).

Once that's done, use /etc/init.d/httpd to start and stop the process, and you should be in good shape.

Updated in response to poster's comment:

You don't want to read from your bashrc... and use /usr/init.d/httpd restart (it's just as easy as apache2ctl)... As for how to do this, put the environment variables in a new file. in the example below, I'll assume you are using the file "/etc/httpd/envconfig". Then add these lines to both your bashrc and /etc/sysconfig/httpd:

if [ -f /etc/httpd/envconfig ]; then . /etc/httpd/envconfig fi

Now both your bash and your httpd script should function with the updated environment variables.

One additional note is that CGI environment variables get handled differently in Apache, you may want to check out this URL:

http://httpd.apache.org/docs/2.2/env.html

In particular, "...Variables may also be passed from the environment of the shell which started the server using the PassEnv directive..."

I believe you'll need to put appropriate "PassEnv" directives in the httpd.conf file specifying the names of the environment variables you wish to pass to the CGI scripts. The good news is that you don't have to specify a value, so as long as you don't change the names of the variables themselves, you shouldn't have to manage two configurations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM