在Openshift上安装/创建Django应用

0 投票
2 回答
983 浏览
提问于 2025-04-18 11:59

我正在把一个应用程序迁移到OpenShift上,但我找不到太多(几乎没有)关于如何让应用程序在这个平台上运行的文档。一个主要的问题是我不知道该如何处理创建应用时默认生成的wsgi.py文件,这里是Python3.3的版本。

这是我做的步骤:

  1. 通过rhc命令创建一个Python3.3的应用。
  2. 克隆这个代码库。
  3. 编辑setup.py文件,取消对Django依赖的注释(具体可以参考下面的链接)。
  4. 尝试下面链接中关于wsgi.py的建议(根据Python的版本进行修改)。

如何在OpenShift上配置Django?

现在我可以看到在推送后,Django作为一个依赖被拉取进来了(在setup.py中有变化)。但是我还是搞不清楚wsgi.py文件里应该放什么内容——即使是一个基本的、可以用作模板的应用也会非常有用,但我就是找不到。使用以下内容作为wsgi.py会出现以下错误:

#!/usr/bin/python
import os, sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECTNAME.settings'
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi',
    'PROJECTNAME'))

virtenv = os.environ['APPDIR'] + '/virtenv/'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python3.3/site-packages')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')

try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass

#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#
from django.core.handlers import wsgi
application = wsgi.WSGIHandler()

这是错误信息:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Apache/2.2.15 (Red Hat) Server at name-name.rhcloud.com Port 80

这是我得到的日志文件:

[Thu Jul 03 13:15:59 2014] [error] [client 127.8.250.1]     % (self.SETTINGS_MODULE, e)
[Thu Jul 03 13:15:59 2014] [error] [client 127.8.250.1] ImportError: Could not import settings 'name.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'name'
80.241.77.253 - - [03/Jul/2014:13:15:59 -0400] "GET /favicon.ico HTTP/1.1" 500 622 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"

一个基本的settings.py文件也会很有帮助!

2 个回答

0

在我的 wsgi.py 文件中有以下内容:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.prod")

application = get_wsgi_application()

而且在我的项目根目录下有一个 requirements.txt 文件,这就足够让我的 Django 应用正常运行了。

2

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECTNAME.settings' -> 你需要把 PROJECTNAME 改成你自己项目文件夹的名字,后面提到的也是一样。

顺便说一下,可以看看这个链接,看看是否能帮到你: http://django.zone/posts/3,如果你遇到更多错误的话。

如果你需要一个基础的模板应用,可以看看这个 GitHub 仓库 https://github.com/argaen/djangozone,这是一个简单的博客,足够让你理解所有内容。你也可以看看这个仓库: https://github.com/openshift/django-example

撰写回答