python - 使用 Server.app 在 Mac OS X 10.9 上使用 Apache/mod_wsgi 部署 Django

标签 python django macos apache mod-wsgi

过去两天我尝试在运行 Server.app 的 Mac 10.9 上部署带有夹层的 Django 项目。我学到了很多东西,但到目前为止还没有运气。我读过几篇关于这个主题的文章:

https://apple.stackexchange.com/questions/151388/can-i-deploy-my-django-site-to-os-x-server Deploying Django on OS 10.9 Server

但是这些方法对我不起作用。

我一遍又一遍地浏览 djangoproject 上的文档,但找不到解决方案。所以请耐心等待,我确信有解决方案。

我试图尽可能简单地做到这一点,甚至为了Apple的Server.app而省略了virtualenv 当然我在这里遗漏了一些东西......

这是我的设置: Mac OS X 10.9 与服务器应用程序 3 Python 2.7.9(通过brew安装) Django 1.6.10 Apache 2.2.26 mod_wsgi 4.4.8(通过 pip 的快速版本)

项目代码本身位于/Users/_dev/MyProject/MyProject

conf 文件: 我将 wsgi.py 重命名为 MyProject.wsgi,它只是在 Server.app/Advanced 设置中列出了 MyProject,仅此而已。值得一提的是:苹果默认的“hello world python 应用程序”也不起作用,我从来没有见过它,但认为它与苹果提供的默认启动屏幕不同。

/Library/Server/Web/Config/apache2/中的 httpd_wsgi2.conf

WSGIScriptAlias / /Users/_dev/MyProject/MyProject/MyProject.wsgi

<Directory /Users/_dev/MyProject/MyProject>
<Files MyProject.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>

com.apple.webapp.wsgi2.plist 在/Library/Server/Web/Config/apache2/webapps/

/Users/_dev/MyProject/MyProject/MyProject.wsgi<?xml version="1.0" encoding="UTF-7"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>name</key>
        <string>com.apple.webapp.wsgi2</string>
        <key>displayName</key>
        <string>Hot Club Setup at /</string>
        <key>launchKeys</key>
        <array/>
        <key>proxies</key>
        <dict/>
        <key>installationIndicatorFilePath</key>
        <string>/Users/_dev/MyProject/MyProject/MyProject.wsgi</string>
        <key>includeFiles</key>
        <array>
                <string>/Library/Server/Web/Config/apache2/httpd_wsgi2.conf</string>
        </array>
        <key>requiredModuleNames</key>
        <array>
                <string>wsgi_module</string>
        </array>
</dict>
</plist>

至少 Django 和 Mezzanine 在开发服务器上运行良好 python 管理.py runserver

mod_wsgi 似乎已激活 /private/etc/apache2/httpd.conf :LoadModule wsgi_module libexec/apache2/mod_wsgi.so 而且 mod_wsgi-express start-server 工作正常,至少对于 Malt Whyskey 启动项来说是这样。

但是当我输入 mod_wsgi-express start-server MyProject.wsgi 时 我收到一个内部服务器错误 /tmp/mod_wsgi-localhost:8000:507/error_log 处的日志显示:

ImportError: Could not import settings 'MyProject.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named MyProject.settings

比添加

PYTHONPATH="/Users/_dev/MyProject/MyProject/:$PYTHONPATH"
export PYTHONPATH

到/User/_dev/.bash_profile

并且 echo $PYTHONPATH 说

/Users/_dev/MyProject/MyProject/:

我做错了什么?非常欢迎任何帮助。

干杯 约尔格

最佳答案

只是一个简短的说明......在 friend 的大力帮助下,我设法完成了它。这是我的设置:

/Library/Server/Web/Config/apache2/httpd_wsgi2.conf

Alias /robots.txt /Users/_dev/hotclub/hotclub/static/robots.txt
Alias /favicon.ico /Users/_dev/hotclub/hotclub/static/img/favicon.ico

Alias /media/ /Users/_dev/hotclub/hotclub/static/media/
Alias /static/ /Users/_dev/hotclub/hotclub/static/

<Directory /Users/_dev/hotclub/hotclub/static>
Order allow,deny
Allow from all
</Directory>

<Directory /Users/_dev/hotclub/hotclub/static/media>
Order allow,deny
Allow from all
</Directory>

WSGIScriptAlias / /Users/_dev/hotclub/hotclub/hotclub.wsgi

<Directory /Users/_dev/hotclub/hotclub>
Order allow,deny
Allow from all
</Directory>

/Library/Server/Web/Config/apache2/webapps/com.apple.webapp.wsgi2.plist

<?xml version="1.0" encoding="UTF-7"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>name</key>
        <string>com.apple.webapp.wsgi2</string>
        <key>displayName</key>
        <string>Hot Club Setup at /</string>
        <key>launchKeys</key>
        <array/>
        <key>proxies</key>
        <dict/>
        <key>installationIndicatorFilePath</key>
        <string>/Users/_dev/hotclub/hotclub/hotclub.wsgi</string>
        <key>includeFiles</key>
        <array>
                <string>/Library/Server/Web/Config/apache2/httpd_wsgi2.conf</string>
        </array>
        <key>requiredModuleNames</key>
        <array>
                <string>wsgi_module</string>
        </array>
</dict>
</plist>

/Users/_dev/MyProject/MyProject/MyProject.wsgi

future 导入unicode_literals

import os
import sys

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
#PROJECT_ROOT = "/Users/_dev/hotclub/hotclub"
settings_module = "%s.settings" % PROJECT_ROOT.split(os.sep)[-1]
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)

sys.path.append('/Users/_dev/hotclub')
sys.path.append('/Users/_dev/hotclub/hotclub')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

干杯, 约尔格

关于python - 使用 Server.app 在 Mac OS X 10.9 上使用 Apache/mod_wsgi 部署 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553133/

相关文章:

java - NumberFormat 解析方法返回负结果!

django - 为什么不再推荐gunicorn_django?

javascript - 复选框值的嵌套循环,与输入表单和以 json 格式保存的表单值进行比较。以及如何避免复选框组中的重复项

python - Alembic 修订版自动生成错误的配置读取

python - Selenium 驱动程序启动缓慢

python - 如何在 django 中设置 "CSRF verification failed"的默认处理程序?

javascript - 如何从 Javascript 将 Json 保存到 django 数据库中

macos - 为什么 NSSecureTextField 不支持某些语言的字符串?

macos - View 如何在 OSX 10.9 上加载?

python - 访问 Numpy 数组的列?尝试通过转置或通过列访问来执行的错误