奇怪~Apache找不到实际存在的“bash”执行我的cgi文件~
也许你回答起来太容易了。 我的问题是关于 cgi 和 apache web 服务器。 简单来说,我有一个 html“form.html”,其中包含一个表单。要访问它,请在浏览器中输入“127.0.0.1/form.html”。 在这个html文件中点击“提交”后,应该地址是“127.0.0.1\cgi-bin\cginame.cgi”,“cginame.cgi”的内容如下:
#!/bin/bash
if [ $REQUEST_METHOD="GET" ]
then
data=$QUERY_STRING
else
data='cat'
fi
java mortcal $data
“mortcal”是一个java程序计算并将包含结果的 HTML 页面返回给用户。 我使用的是 apache 2.2 和 ubuntu 10.04。 问题是当我单击“form.html”中的“提交”按钮时,我在错误日志中收到以下内容:
[Sat Sep 24 15:00:20 2011] [error] (2)No such file or directory: exec of '/usr/lib/cgi-bin/mortcgi.cgi' failed
[Sat Sep 24 15:00:20 2011] [error] [client 127.0.0.1] Premature end of script headers: mortcgi.cgi
我知道这是因为 apache 找不到“/bin/bash”来执行 cgi 文件。但我确实有“/bin/bash”。 太奇怪了。请帮帮我。先感谢您。
May be it's too easy for you to answer.
My problem is about cgi and apache web server.
Make it simple, I have a html "form.html" containing a form in it. To access it, typing "127.0.0.1/form.html" in browser.
After clicking "submit" in this html file, it is supposed to adress to "127.0.0.1\cgi-bin\cginame.cgi", the content of "cginame.cgi" is as below:
#!/bin/bash
if [ $REQUEST_METHOD="GET" ]
then
data=$QUERY_STRING
else
data='cat'
fi
java mortcal $data
"mortcal" is a java program calculating and return a HTML page containing results to user.
I'm using apache 2.2 and ubuntu 10.04.
The problem is when I click the "submit" button in "form.html", I got these in error log:
[Sat Sep 24 15:00:20 2011] [error] (2)No such file or directory: exec of '/usr/lib/cgi-bin/mortcgi.cgi' failed
[Sat Sep 24 15:00:20 2011] [error] [client 127.0.0.1] Premature end of script headers: mortcgi.cgi
I know it's because apache can not find "/bin/bash" to execute the cgi file. But I do have "/bin/bash".
It's so weird. Please help me out. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要执行 CGI 脚本,您需要配置 Apache 以允许执行此操作,并且您的脚本必须遵循 HTTP 协议,以正确的格式和正确的权限发送回数据,等等。
这是一个带有示例的很棒的教程: http://httpd.apache.org/docs /2.2/howto/cgi.html
...但是,我需要说:一般来说,通过 Apache 从 shell 脚本中运行 java 程序是一个坏主意。每个请求都会加载 Java 运行时引擎 (JRE)、运行程序,然后卸载它。存在环境、文件所有权等问题——所有这些都是为什么有像 tomcat for java 这样的应用程序服务器。所以如果你只是尝试一些事情,那很好。如果您认为这是在专业生产环境中完成某些工作的好方法,我会重新考虑。
To execute CGI scripts, you need to configure Apache to allow this, and your script has to follow the HTTP protocol by sending back data in the right format, and right permissions, and on and on and on.
Here's a great tutorial with an example: http://httpd.apache.org/docs/2.2/howto/cgi.html
... however, I need to say: running a java program from within a shell script via Apache is a bad idea, in general. Each request loads the java runtime engine (JRE), runs the program, then unloads it. There are issues with environment, file ownership and so on -- all of this is why there are application servers like tomcat for java. So if you're just trying something, that's fine. If you're thinking this is a good way to get something done in a professional production environment, I would reconsider.
如前所述,这似乎是一种糟糕的做法,但是:
As noted, this seems like a poor way to do things, but:
我检查了我的配置文件。他们都还好。所以我继续在网上搜索,最后我看到了这样的内容:
“如果你从 Windows 机器上复制了脚本,你可能会在行尾被 ^M 绊倒。你可以使用 cat -v /usr/ lib/cgi-bin/printenv.pl | head -1 来验证该行末尾没有 ^M ”
我确实从 Windows 复制了我的 cgi 文件!我忘了提及它,因为我认为这没什么大不了的。
现在我已经通过在vi中输入“:%s/^V^M//g”删除了^M。这个问题已经解决了。非常感谢您的回答,Harrison先生和Dark Falcon,谢谢大家。
I checked my configuration files. They are ok. So I kept searching on the web and finally I saw this:
"If you've copied over the script from a Windows machine, you may be being tripped up by ^M at end of line. You can use cat -v /usr/lib/cgi-bin/printenv.pl | head -1 to verify that there isn't a ^M at the end of the line. "
I did copy my cgi file from windows! I forgot to mention it because I did not think it's a big deal.
Now I have removed the ^M by typing this" :%s/^V^M//g in vi. This problem is resolved. Thanks very much for your answer, Mr.Harrison and Dark Falcon, Thank you all.