使用Apache2提供python CGI脚本

时间:2016-05-31 16:37:13

标签: python apache2 cgi

我正在尝试从网页调用Python脚本,而apache2不会对脚本执行操作。文件是:

test.py

#!/usr/bin/env python
print "Content-type: text/html\n\n"
print "<h1>Hello World</h1>"

的index.html

<form action="./test.py" method="POST">
    <input type="text" size="70" name="data" value=""><br>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
</form>

我已启用a2enmod cgi并编辑了apache设置,如下所示:

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
...
<directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py
    Require all granted
</directory>
</VirtualHost>

网页文件所有权为:

# ls -lh /var/www/html/
total 16K
-rw-r--r-- 1 root root  342 May 30 13:34 index.html
-rwxr-xr-x 1 root root   88 May 30 14:11 test.py

正在运行apachectl -M会显示一个包含行cgid_module (shared)的列表。

当我打开页面时,我可以看到表单,但是当我点击“提交”时,浏览器会提示下载test.py(FF)或查看(Chrome)。知道我在这里弄错了吗?

1 个答案:

答案 0 :(得分:0)

正如评论中所讨论的那样。此目录配置看起来像一个示例中的普通副本,您的Web根目录可能位于其他位置。

<VirtualHost *:80>
    <directory "/var/www/htdocs">
        AllowOverride None Options
        +ExecCGI -MultiViews 
        +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all 
        AddHandler cgi-script .py
        Require all granted
    </directory>
</VirtualHost>

例如,目录更改的位置。这通常是默认路径,如果您的/var/www/html相应更改。

我之前已经知道了这个问题但是这个问题在serverfault上也可能会更好,因为这与apache的配置方式有很大关系。

相关问题