Python没有在apache上解释

时间:2015-04-18 22:23:23

标签: python apache

我试图将Python设置为在Mac上的本地apache服务器上运行。

httpd.conf,我

<VirtualHost *:80>
    DocumentRoot "/Users/ptamzz/Sites/python/sandbox.ptamzz.com"
    <Directory "/Users/pritams/Sites/python/sandbox.ptamzz.com">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AddHandler cgi-script .py
        Require all granted
    </Directory>
    DirectoryIndex index.py
    ServerName sandbox.ptamzz.com
    ErrorLog "/var/log/apache2/error-log"
    CustomLog "/var/log/apache2/access-log" common
</VirtualHost>

在我的文档根目录中,我的index.py文件为

#!/usr/bin/python
print "Content-type: text/html"
print "<html><body>Pritam's Page</body></html>"

但是当我在浏览器上加载页面时,python代码按原样返回。 Screenshot

我缺少什么?

2 个答案:

答案 0 :(得分:1)

在Apache(cgi)中执行python

系统:OSX yosmite 10.10.3,默认apache

在http config

中取消注释
LoadModule cgi_module libexec/apache2/mod_cgi.so

虚拟主机条目

<VirtualHost *:80>

    # Hook virtual host domain name into physical location.
    ServerName python.localhost
    DocumentRoot "/Users/sj/Sites/python"

    # Log errors to a custom log file.
    ErrorLog "/private/var/log/apache2/pythonlocal.log"

    # Add python file extensions as CGI script handlers.
    AddHandler cgi-script .py

    # Be sure to add ** ExecCGI ** as an option in the document
    # directory so Apache has permissions to run CGI scripts.

    <Directory "/Users/sj/Sites/python">

    Options Indexes MultiViews FollowSymLinks ExecCGI
    AddHandler cgi-script .cgi
    AllowOverride All
    Order allow,deny
    Allow from all


    </Directory>

</VirtualHost>

index.py文件

#!/usr/bin/env python 
print "Content-type: text/html" 
print 
print "<html><body>Test Page</body></html>"

使用

使文件可执行
chmod a+x index.py

重新启动apache并输出

python in apache

答案 1 :(得分:0)

对于Apache的更高版本(2.4),Sojan V Jose的答案基本上仍然适用,除了我获得了授权失败之外,可以通过替换来解决:

Order allow,deny
Allow from all

具有:

Require all granted