问题 如何从Web服务器调用本地shell脚本?


我正在运行Ubuntu 11,我想设置一个简单的Web服务器,通过使用GET或POST参数调用本地脚本来响应http请求。这个脚本(已经编写)做了一些事情并创建了一个文件。此文件应在URL处可用,然后Web服务器应向另一个服务器发出http请求,告知其下载创建的文件。

我该如何设置呢?我不是Linux的初学者,但我不会说我也很清楚。

我应该使用什么网络服务器?如何授予脚本访问本地资源以创建相关文件的权限?我不太关心安全性或任何东西,这是个人实验(我控制所有涉及的计算机)。我之前使用过apache,但是我从来没有设置过它。

任何帮助,将不胜感激..


5661
2017-12-04 20:06


起源



答案:


本教程看起来不错,但它有点简短。

我安装了apache。如果你不这样做: sudo apt-get install apache2

cd /usr/lib/cgi-bin

# Make a file and let everyone execute it
sudo touch test.sh && chmod a+x test.sh 

然后将一些代码放在文件中。例如:

#!/bin/bash
# get today's date
OUTPUT="$(date)"
# You must add following two lines before
# outputting data to the web browser from shell
# script
 echo "Content-type: text/html"
 echo ""
 echo "<html><head><title>Demo</title></head><body>"
 echo "Today is $OUTPUT <br>"
 echo "Current directory is $(pwd) <br>"
 echo "Shell Script name is $0"
 echo "</body></html>"

最后打开浏览器并输入 HTTP://localhost/cgi-bin/test.sh

如果一切顺利(就像它对我而言)你应该看到......

今天是太阳12月4日......
  当前目录是/ usr / lib / cgi-bin Shell
  Shell脚本名称为/usr/lib/cgi-bin/test.sh


12
2017-12-04 20:23



我们还必须启用 mod_cg一世 a2enmod cgi 如果尚未启用 - Varun Agrawal
在我的系统上,我使用以下命令来激活mod_cgi: sudo a2enmod cgi  sudo service apache2 restart - Nicolas Jean
你如何阅读发布数据? - Tono Nam
@TonoNam我建议问另一个问题并链接到这个问题。我不知道答案。 - FakeRainBrigand
@Daniel我建议将其作为一个单独的问题 - FakeRainBrigand


答案:


本教程看起来不错,但它有点简短。

我安装了apache。如果你不这样做: sudo apt-get install apache2

cd /usr/lib/cgi-bin

# Make a file and let everyone execute it
sudo touch test.sh && chmod a+x test.sh 

然后将一些代码放在文件中。例如:

#!/bin/bash
# get today's date
OUTPUT="$(date)"
# You must add following two lines before
# outputting data to the web browser from shell
# script
 echo "Content-type: text/html"
 echo ""
 echo "<html><head><title>Demo</title></head><body>"
 echo "Today is $OUTPUT <br>"
 echo "Current directory is $(pwd) <br>"
 echo "Shell Script name is $0"
 echo "</body></html>"

最后打开浏览器并输入 HTTP://localhost/cgi-bin/test.sh

如果一切顺利(就像它对我而言)你应该看到......

今天是太阳12月4日......
  当前目录是/ usr / lib / cgi-bin Shell
  Shell脚本名称为/usr/lib/cgi-bin/test.sh


12
2017-12-04 20:23



我们还必须启用 mod_cg一世 a2enmod cgi 如果尚未启用 - Varun Agrawal
在我的系统上,我使用以下命令来激活mod_cgi: sudo a2enmod cgi  sudo service apache2 restart - Nicolas Jean
你如何阅读发布数据? - Tono Nam
@TonoNam我建议问另一个问题并链接到这个问题。我不知道答案。 - FakeRainBrigand
@Daniel我建议将其作为一个单独的问题 - FakeRainBrigand