简体   繁体   English

Apache httpd不会加载.bashrc

[英]Apache httpd doesn't load .bashrc

I'm running Python scripts as a CGI under Apache 2.2. 我在Apache 2.2下将Python脚本作为CGI运行。 These scripts rely on environment variables set in my .bashrc to run properly. 这些脚本依赖于.bashrc中设置的环境变量才能正常运行。 The .bashrc is never loaded, and my scripts fail. .bashrc从未加载,我的脚本失败了。

I don't want to duplicate my bashrc by using a bunch of SETENV commands; 我不想通过使用一堆SETENV命令来复制我的bashrc。 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. 我以用户身份而不是root用户身份运行apache。 I'm starting/stopping it manually, so the /etc/init.d script shouldn't matter at all (I think). 我正在手动启动/停止它,所以/etc/init.d脚本根本不重要(我认为)。

Given these constraints, what can I do to have my .bashrc loaded when my CGI is called? 考虑到这些限制,在调用CGI时如何加载.bashrc?

Edit: I use /usr/sbin/apache2ctl to do the restarting. 编辑:我使用/ usr / sbin / apache2ctl进行重新启动。

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. Apache配置文件可以将环境变量导出到CGI脚本等。

Maybe your program is too dependent on too many environment variables. 也许您的程序过于依赖太多环境变量。 How about supporting a configuration file: /etc/mypythonprogram.rc . 如何支持配置文件: /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. 似乎使用init脚本会有所帮助。 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. 您可以在httpd.conf文件中指定用于运行httpd进程的用户名,也可以指定环境设置。 For RedHat/CentOs, this would look like: 对于RedHat / CentO,这看起来像:

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). 将环境设置添加到文件/ etc / sysconfig / httpd中(或者,您可以设置命令从另一个文件中读取这些环境变量,如果需要,还可以在.bashrc中设置相同的命令-因此只有一个包含所需环境设置的文件)。

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). 修改/etc/httpd/conf/httpd.conf文件,以便运行httpd进程的用户是非root用户(默认情况下,它应该是RedHat / CentOs中的apache)。

Once that's done, use /etc/init.d/httpd to start and stop the process, and you should be in good shape. 完成后,使用/etc/init.d/httpd启动和停止该过程,您的状态应该会很好。

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. 您不想从bashrc中读取...并使用/usr/init.d/httpd restart(就像apache2ctl一样简单)...至于如何执行此操作,请将环境变量放入一个新文件中。 in the example below, I'll assume you are using the file "/etc/httpd/envconfig". 在下面的示例中,我假设您正在使用文件“ / etc / httpd / envconfig”。 Then add these lines to both your bashrc and /etc/sysconfig/httpd: 然后将这些行添加到您的bashrc和/ etc / sysconfig / httpd中:

if [ -f /etc/httpd/envconfig ]; 如果[-f / etc / httpd / envconfig]; then . 然后 。 /etc/httpd/envconfig fi / etc / httpd / envconfig fi

Now both your bash and your httpd script should function with the updated environment variables. 现在,您的bash和httpd脚本都应该与更新的环境变量一起使用。

One additional note is that CGI environment variables get handled differently in Apache, you may want to check out this URL: 还有一点需要注意的是,CGI环境变量在Apache中的处理方式有所不同,您可能需要查看以下URL:

http://httpd.apache.org/docs/2.2/env.html 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..." 特别是,“ ...变量也可以从使用PassEnv指令启动服务器的外壳环境中传递...”

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. 我相信您需要在httpd.conf文件中放入适当的“ PassEnv”指令,以指定要传递给CGI脚本的环境变量的名称。 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. 好消息是,您不必指定值,因此,只要您不更改变量本身的名称,就不必管理两个配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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