【问题标题】:Running Python locally on Mountain Lion在 Mountain Lion 上本地运行 Python
【发布时间】:2013-09-11 02:07:41
【问题描述】:

我的本​​地环境设置为运行 MySQL 和 PHP。我开始涉足其他语言并尝试让 Java、Python 和 Ruby 在我的本地 Web 服务器上运行。所有语言都已正确安装,但 apache 无法识别它们或给我权限问题。

我从 Python 开始,在让 apache 识别 .mod 出现许多问题之后,当我转到该页面时,我现在收到 403 错误。

我不确定下一步是如何让它正常工作。

403 禁止

禁止

您无权访问此服务器上的 /。

<VirtualHost *:80>
          DocumentRoot "/Library/Webserver/Documents/python-mn"
          ServerName python.local
          ServerAlias www.python.local
          WSGIScriptAlias / /Library/WebServer/Documents/python-mn/wsgi.py
          <Directory /Library/Webserver/Documents/python-mn>
                #Options Indexes FollowSymLinks MultiViews
                Options Indexes FollowSymLinks Includes ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
          </Directory> 
</VirtualHost>

检查了 Apache 错误日志:

tail -f /var/log/apache2/error_log
[Fri Sep 06 15:30:51 2013] [error] [client 127.0.0.1] mod_hfs_apple: Mis-cased URI or unacceptable Unicode in URI: /Library/WebServer/Documents/python-mn/wsgi.py, wants: /Library/Webserver/Documents/python-mn/

Ping 本地环境:

ping python.local
PING python.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.041 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.153 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.159 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.130 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.134 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.054 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.073 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.135 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.111 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.130 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.131 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.133 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.136 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.132 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.140 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.136 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.105 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.074 ms

MacBook Pro(Retina,15 英寸,2013 年初)、OS X Mountain Lion (10.8.4)

---编辑---

wsgi.py 文件:

import bottle
import os

def application(environ, start_response):
    data = "Hello World!"
    start_response("200 OK", [
            ("Content-Type", "text/plain"),
            ("Content-Length", str(len(data)))
            ])
    return iter([data])

---编辑 9/9/13 上午 11:46 ---

现在出现新错误:

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请联系服务器管理员 you@example.com 并告知他们错误发生的时间,以及您所做的任何可能导致错误的事情。

服务器错误日志中可能会提供有关此错误的更多信息。

apache 错误日志

[Mon Sep 09 11:43:08 2013] [notice] caught SIGTERM, shutting down
[Mon Sep 09 11:43:08 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
httpd: Could not reliably determine the server's fully qualified domain name, using Micheles-MacBook.local for ServerName
[Mon Sep 09 11:43:08 2013] [warn] mod_wsgi: Compiled for Python/2.6.1.
[Mon Sep 09 11:43:08 2013] [warn] mod_wsgi: Runtime using Python/2.6.7.
[Mon Sep 09 11:43:08 2013] [notice] Digest: generating secret for digest authentication ...
[Mon Sep 09 11:43:08 2013] [notice] Digest: done
[Mon Sep 09 11:43:08 2013] [notice] Apache/2.2.22 (Unix) mod_wsgi/3.3 Python/2.6.7 PHP/5.5.3 mod_ssl/2.2.22 OpenSSL/0.9.8x DAV/2 configured -- resuming normal operations
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7888): Target WSGI script '/Library/WebServer/Documents/python-mn/wsgi.py' cannot be loaded as Python module.
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7888): Exception occurred processing WSGI script '/Library/WebServer/Documents/python-mn/wsgi.py'.
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1]   File "/Library/WebServer/Documents/python-mn/wsgi.py", line 1, in <module>
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1]     import bottle
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] ImportError: No module named bottle
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7888): Target WSGI script '/Library/WebServer/Documents/python-mn/wsgi.py' cannot be loaded as Python module.
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7888): Exception occurred processing WSGI script '/Library/WebServer/Documents/python-mn/wsgi.py'.
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1]   File "/Library/WebServer/Documents/python-mn/wsgi.py", line 1, in <module>
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1]     import bottle
[Mon Sep 09 11:43:18 2013] [error] [client 127.0.0.1] ImportError: No module named bottle

这是我期望得到的,它的文件结构和代码与我的 AppFog 安装相同:http://python-mn.aws.af.cm/

---编辑 2013 年 9 月 9 日下午 1:03---

我执行了一个终端命令来安装瓶子...不确定如何确保 wsgi.py 或 vhosts 知道它的位置。显然,AppFog 服务器本身就有这个功能,这就是为什么我不需要任何额外的东西来让它运行。

easy_install -U bottle
Searching for bottle
Reading https://pypi.python.org/simple/bottle/
Best match: bottle 0.11.6
Downloading https://pypi.python.org/packages/source/b/bottle/bottle-            0.11.6.tar.gz#md5=0bafdc4e13ea2b1a3bddf36b5af108c4
Processing bottle-0.11.6.tar.gz
Writing /var/folders/hc/dtnjvkvn1dd931bkkxty_05w0000gn/T/easy_install-QS4GZM/bottle-0.11.6/setup.cfg
Running bottle-0.11.6/setup.py -q bdist_egg --dist-dir /var/folders/hc/dtnjvkvn1dd931bkkxty_05w0000gn/T/easy_install-QS4GZM/bottle-0.11.6/egg-dist-    tmp-pQrFpa
zip_safe flag not set; analyzing archive contents...
bottle: module references __file__
bottle: module references __path__
Adding bottle 0.11.6 to easy-install.pth file
Installing bottle.py script to /usr/local/bin

Installed /usr/local/lib/python2.7/site-packages/bottle-0.11.6-py2.7.egg
Processing dependencies for bottle
Finished processing dependencies for bottle

【问题讨论】:

  • 能否包含您的 wsgi.py 文件?如果您没有 #! 在顶部,我特别好奇。
  • @Foon 它是一个“Hello World”脚本。它适用于我的 AppFog 帐户。编辑了上面的帖子以将其包含在最后。
  • 我的错误...我的错误印象是您的 modwsgi 文件需要 shebang 行(例如 #!/usr/bin/python),并且您的错误是其他一些奇怪的交互解释器(例如 bash)正在做一些意想不到的事情。
  • 好像找不到bottle。确保它在系统 Python 路径上。
  • @luminous 我进行了安装并将瓶子添加到我的本地。显然,我的 AppFog 服务器本身已经拥有它。我不确定如何将其添加到系统 python 路径中。我添加了上面安装的终端输出。

标签: python macos apache permissions localhost


【解决方案1】:

试试这个(或者可能将 WebServer 切换到 webserver(我改变的是让路径名都是 WebServer;你有一些 Webserver(小写 S)和一些 WebServer

<VirtualHost *:80>
          DocumentRoot "/Library/WebServer/Documents/python-mn"
          ServerName python.local
          ServerAlias www.python.local
          WSGIScriptAlias / /Library/WebServer/Documents/python-mn/wsgi.py
          <Directory /Library/WebServer/Documents/python-mn>
                #Options Indexes FollowSymLinks MultiViews
                Options Indexes FollowSymLinks Includes ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
          </Directory> 
</VirtualHost>

根据Apple's mod_hfs_guide,您的错误消息是由于 Apple 试图保护您免受有人利用 HFS 不区分大小写来访问应该通过使用有趣的大小写版本名称来保护的内容。

【讨论】:

  • 我现在收到一条新的错误消息。我粘贴了上面的消息和错误日志。它似乎找不到“瓶子”。我很困惑,因为这在我的 AppFog 应用上运行良好。
【解决方案2】:

我看你想通了,但供将来参考......

看起来bottle 不是您的 Python 系统路径的一部分。要将其添加到您的路径中,您有两种选择:

  1. 通过在此处下载将bottle 手动添加到您的系统 Python 路径:

    https://pypi.python.org/pypi/bottle/0.11.6

    下载后运行:

    tar xvf bottle-0.11.6.tar.gz
    cd bottle-0.11.6/
    sudo python setup.py install 
    
  2. 使用包管理器为您提取它。您可以从这里安装 pip(也可以使用 easy_install、brew 或 macports):

    https://pypi.python.org/pypi/pip/1.4.1

    然后,一旦安装(类似于上面),运行:

    pip install bottle
    

【讨论】:

  • 我完成了您的步骤,一切似乎都已正确安装。我也必须安装 pip(我遵循了这些说明pip-installer.org/en/latest/installing.html)。我仍然收到 500 内部服务器错误。我也试过重启apache,还是不行。
  • 我现在收到 brew doctor 警告消息。但不确定它与它有什么关系:“警告:“config”脚本存在于您的系统或 Homebrew 目录之外。./configure 脚本通常会查找 *-config 脚本以确定是否安装了软件包,以及要添加哪些附加标志在编译和链接时使用。如果配置脚本覆盖系统或 Homebrew 提供的同名脚本,则路径中的附加脚本可能会混淆通过 Homebrew 安装的软件。我们发现以下“配置”脚本:/Users/michele/.rbenv /shims/passenger-config"
  • 是的,你可能应该做一个或另一个,而不是两个。您从 Apache 得到的错误仍然相同吗?还缺瓶子吗?
  • 嗯,很有趣。如果您在系统终端中运行python -c 'import bottle',它会抛出任何错误吗?
  • 该命令在终端或 apache 错误日志中没有任何错误。
猜你喜欢
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多