首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python没有在apache上解释

Python没有在apache上解释
EN

Stack Overflow用户
提问于 2015-04-18 22:23:22
回答 2查看 2.2K关注 0票数 0

我正在尝试将Python设置为在mac上运行本地apache服务器。

httpd.conf上,我

代码语言:javascript
复制
<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文件作为

代码语言:javascript
复制
#!/usr/bin/python
print "Content-type: text/html"
print "<html><body>Pritam's Page</body></html>"

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

我错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-19 10:37:01

在Apache ( cgi )中执行python

系统: OSX yosmite 10.10.3,默认apache

配置中未注释的

代码语言:javascript
复制
LoadModule cgi_module libexec/apache2/mod_cgi.so

虚拟主机条目

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
#!/usr/bin/env python 
print "Content-type: text/html" 
print 
print "<html><body>Test Page</body></html>"

文件可执行使用

代码语言:javascript
复制
chmod a+x index.py

重新启动apache并输出

票数 2
EN

Stack Overflow用户

发布于 2020-05-15 16:45:16

对于Apache (2.4)的以后版本,Sojan V Jose的答案主要还是适用的,只是我的授权失败了,可以通过替换:

代码语言:javascript
复制
Order allow,deny
Allow from all

通过以下方式:

代码语言:javascript
复制
Require all granted
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29723590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档