【问题标题】:Python not interpreted on apachePython 未在 apache 上解释
【发布时间】:2020-05-16 00:45:16
【问题描述】:

我正在尝试将 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 代码会按原样返回。

我错过了什么?

【问题讨论】:

  • 你用 chmod 设置了执行权限吗?
  • 是的。我已将其设置为 755
  • 未注释 LoadModule cgi_module libexec/apache2/mod_cgi.so ?
  • 刚刚取消注释它。现在正在发生一些事情。而是引发内部服务器错误!我在错误日志上没有看到任何错误! ://
  • sudo apachectl -t 检查错误

标签: python apache


【解决方案1】:

在 Apache (cgi) 中执行 python

系统:OSX yosmite 10.10.3,默认apache

在 http 配置中未注释

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并输出

【讨论】:

  • 谢谢,终于成功了!我chmod index.py 正如你在这里提到的,并修改了两个AddHandler 行。如果我把AddHandler cgi-script .py 放在&lt;directory&gt; 里面和外面有什么区别?
  • @ptamzz 实际上我并没有故意这样做:P。实际上它不应该有所作为。我认为 chmod 部分是你的问题。
【解决方案2】:

对于更高版本的 Apache (2.4),Sojan V Jose 的答案大多仍在适用,除了我遇到授权失败,可以通过替换来解决:

Order allow,deny
Allow from all

与:

Require all granted

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 2017-10-03
    • 2015-09-04
    • 2018-08-13
    • 1970-01-01
    • 2019-08-14
    • 2011-04-09
    • 1970-01-01
    • 2013-12-06
    相关资源
    最近更新 更多