我已经使用 django 构建了一个 webapp。为了托管它,我正在尝试使用 openshift,但无法正常工作。似乎缺乏这方面的步骤。到目前为止,我的 git 工作正常,该应用程序可在本地开发环境中运行,并且我已成功在 openshift 上创建了一个应用程序。
按照创建后的 openshift 上的 URL,我只会看到“欢迎使用您的 Openshift 应用程序”的标准页面。
我关注了这个 https://developers.openshift.com/en/python-getting-started.html#step1尝试更改 wsgi.py 文件。将其更改为 hello world,推送它,但我仍然获得 openshift 默认页面。
是否有一个很好的综合资源可以让本地 Django 应用程序在 Openshift 上启动和运行?我在谷歌上可以找到的大部分内容只是示例应用程序,因为我已经构建了我的应用程序,所以它们并不是那么有用。
最佳答案
编辑 :请记住,这是一个依赖于平台的答案,并且由于为 Django 提供服务的 OpenShift 平台可能会发生变化,因此该答案可能无效。截至 2016 年 4 月 1 日,此答案在整个范围内仍然有效。
很多时候这种情况发生在我身上,因为我必须至少挂载 5 个应用程序,所以我必须创建自己的生命周期:
yourproject
和...# git clone yourrepo@rhcloud.com:app.git yourproject <- replace it with your actual openshift repo address
yourproject/
+---wsgi.py
+---setup.py
*---.openshift/ (with its contents - I omit them now)
pip
安装 Django以及您需要的所有依赖项(例如一个新的 Pillow 包、MySQL 数据库包,...)。在那里创建一个 django 项目。说,你的djproject。 编辑 在旁边创建一个 wsgi/static
带有空的虚拟文件的目录(例如 .gitkeep
- 名称只是约定:您可以使用任何您想要的名称)。 #assuming you have virtualenv-wrapper installed and set-up
mkvirtualenv myenvironment
workon myenvironment
pip install Django[==x.y[.z]] #select your version; optional.
#creating the project inside the git repository
cd path/to/yourproject/
django-admin.py startproject yourjdproject .
#creating dummy wsgi/static directory for collectstatic
mkdir -p wsgi/static
touch wsgi/static/.gitkeep
yourproject/
+---wsgi/
| +---static/
| +---.gitkeep
+---wsgi.py
+---setup.py
+---.openshift/ (with its contents - I omit them now)
+---yourdjproject/
| +----__init__.py
| +----urls.py
| +----settings.py
| +----wsgi.py
+---+yourapp/
+----__init__.py
+----models.py
+----views.py
+----tests.py
+----migrations
+---__init__.py
#!/usr/bin/python
import os
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
from yourdjproject.wsgi import application
构建钩子(Hook)
#!/bin/bash
#this is .openshift/action/hooks/build
#remember to make it +x so openshift can run it.
if [ ! -d ${OPENSHIFT_DATA_DIR}media ]; then
mkdir -p ${OPENSHIFT_DATA_DIR}media
fi
ln -snf ${OPENSHIFT_DATA_DIR}media $OPENSHIFT_REPO_DIR/wsgi/static/media
######################### end of file
部署钩子(Hook)
#!/bin/bash
#this one is the deploy hook .openshift/action_hooks/deploy
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR
echo "Executing 'python manage.py migrate'"
python manage.py migrate
echo "Executing 'python manage.py collectstatic --noinput'"
python manage.py collectstatic --noinput
########################### end of file
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'wsgi', 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'wsgi', 'static', 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'yourjdproject', 'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'yourjdproject', 'templates'),)
local_settings.py
,如果文件存在,则可选择导入,但我将省略该部分并放入所有内容都在同一个文件中)。 请谨慎阅读此文件,因为诸如 之类的内容yourlocaldbname 是您必须相应设置的值 :"""
Django settings for yourdjproject project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ON_OPENSHIFT = False
if 'OPENSHIFT_REPO_DIR' in os.environ:
ON_OPENSHIFT = True
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '60e32dn-za#y=x!551tditnset(o9b@2bkh1)b$hn&0$ec5-j7'
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'yourapp',
#more apps here
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'yourdjproject.urls'
WSGI_APPLICATION = 'yourdjproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
if ON_OPENSHIFT:
DEBUG = True
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'youropenshiftgenerateddatabasename',
'USER': os.getenv('OPENSHIFT_MYSQL_DB_USERNAME'),
'PASSWORD': os.getenv('OPENSHIFT_MYSQL_DB_PASSWORD'),
'HOST': os.getenv('OPENSHIFT_MYSQL_DB_HOST'),
'PORT': os.getenv('OPENSHIFT_MYSQL_DB_PORT'),
}
}
else:
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #If you want to use MySQL
'NAME': 'yourlocaldbname',
'USER': 'yourlocalusername',
'PASSWORD': 'yourlocaluserpassword',
'HOST': 'yourlocaldbhost',
'PORT': '3306', #this will be the case for MySQL
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'yr-LC'
TIME_ZONE = 'Your/Timezone/Here'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'wsgi', 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'wsgi', 'static', 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'yourdjproject', 'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'yourdjproject', 'templates'),)
cd path/to/yourproject/
git add .
git commit -m "Your Message"
git push origin master # THIS COMMAND WILL TAKE LONG
# git enjoy
[未经测试!] 您可以编辑部署 Hook 并在命令
cd $OPENSHIFT_REPO_DIR
后添加命令,像这样:pip install -r requirements.txt
,假设您的项目中存在 requirements.txt 文件。 pip
应该存在于您的 virtualenv 中,但如果不存在,您可以查看下一个解决方案。或者,setup.py 是 OpenShift 上已经提供的方法。我做了很多次 - 假设 requirements.txt 文件存在 - 是:
strip
前导和尾随空格。 install_requires=
setup
中的关键字参数调用 setup.py 文件。 对不起,我之前没有在教程中包含这个! 但是您需要在服务器中实际安装 Django。也许是一个明显的建议,每个 Python 开发人员都可以事先知道这一点。但是捕获这个机会我说:在requirements.txt(或setup.py 取决于您是否使用requirements.txt 文件)中包含适当的Django 依赖项,因为您包含任何其他依赖项。
这应该可以帮助您挂载 Django 应用程序,并且花了我很多时间来标准化这个过程。享受它,如果出现问题,请不要犹豫,通过评论与我联系
编辑 (对于有同样问题的人,不希望在本帖的评论中找到答案):请记住,如果您在 Windows 下编辑构建或部署 Hook 文件并推送文件,它们将飞到具有 0644 权限的服务器, 由于 Windows 不支持 Unix 具有的此权限方案,并且无法分配权限,因为这些文件没有任何扩展名。 您会注意到这一点,因为在部署 时您的脚本不会被执行。 .所以尝试部署这些文件 只有来自基于 Unix 的系统。
编辑 2 :您可以使用 git hooks(例如 pre_commit)来设置某些文件的权限,例如管道脚本(构建、部署等)。请参阅此答案中@StijndeWitt 和@OliverBurdekin 的评论,以及 this question更多细节。
关于python - 使用 openshift 部署本地 django 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871381/