部署django项目没有结果

时间:2013-05-10 10:36:15

标签: django apache web-deployment

现在已经搜索了很多,并且跟着django文档..仍然无法工作但我觉得我很接近..我知道有很多关于这方面的信息但是我甚至试过了一些指南它仍然无法工作,我想我已经测试了其他主题中提出的大多数内容..

我正在尝试在我的服务器上部署我的django站点,这是在开发模式下工作(从shell)。所以当运行python manage.py runserver时没关系,但是当试图让apache2服务器运行django时,首先我得到“请求的URL /在这台服务器上找不到。”,现在我进入了apache的error.log :

[client 192.168.1.49] mod_wsgi (pid=5606): Exception occurred processing WSGI script '/home/Webmod/www/djangojquerycontroller/Server/Server/wsgi.py'.
[client 192.168.1.49] Traceback (most recent call last):
[client 192.168.1.49]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 236, in __call__
[client 192.168.1.49]     self.load_middleware()
[client 192.168.1.49]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
[client 192.168.1.49]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[client 192.168.1.49]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
[client 192.168.1.49]     self._setup(name)
[client 192.168.1.49]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in _setup
[client 192.168.1.49]     self._wrapped = Settings(settings_module)
[client 192.168.1.49]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 134, in __init__
[client 192.168.1.49]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[client 192.168.1.49] ImportError: Could not import settings 'Server.settings' (Is it on sys.path?): No module named Server.settings

所以我安装了https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/

中提到的所有内容

首先,我的网站位于:/ home / Webmod / www / djangojquerycontroller / Server

(该站点的名称是Server ..)所以这里是模板,静态和媒体文件夹以及正在完成大部分工作的app dataManager。 在/ home / Webmod / www / djangojquerycontroller / Server / Server文件夹中,我们找到:

django.wsgi:(这个文件我不得不手动创建,不知道是否正常工作..)

import os
import sys

path = '/home/Webmod/www/djangojquerycontroller/'
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Server.settings")

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

wsgi.py:(从这个文件我可以写错误信息,所以这个文件正在运行(!))

import sys, os
print  >>sys.stderr, "hej from wsgi.py"
sys.path.append('/home/Wedmod/www/djangojquerycontroller/Server')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Server.settings")

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler( )

settings.py:

import warnings
import exceptions
# PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
warnings.filterwarnings("ignore", category=exceptions.RuntimeWarning,  module='django.db.backends.sqlite3', lineno=50)

DEBUG = True
TEMPLATE_DEBUG = DEBUG

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/home/Webmod/www/djangojquerycontroller/Server/Server/db/database',  # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

ALLOWED_HOSTS = []

TIME_ZONE = 'Europe/Stockholm'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

USE_TZ = True

MEDIA_ROOT = '/home/Webmod/www/djangojquerycontroller/Server/media' 

MEDIA_URL = 'media/'

FILE_UPLOAD_PERMISSIONS = 755

STATIC_ROOT = '/home/Webmod/www/djangojquerycontroller/Server/static'  

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    '/home/Webmod/www/djangojquerycontroller/Server/static/',
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

ADMIN_MEDIA_PREFIX = '/admin_media/'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    #'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'Server.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'Server.wsgi.application'

TEMPLATE_DIRS = (
    '/home/Webmod/www/djangojquerycontroller/Server/templates',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dataManager',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django_evolution',
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

然后我在/ etc / apache2中尝试了两个版本的 https.conf

Alias /static/ /home/Webmod/www/djangojquerycontroller/Server/static/

<Directory /home/Webmod/www/djangojquerycontroller/Server/static>
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias / /home/Webmod/www/djangojquerycontroller/Server/Server/django.wsgi

<Directory /usr/local/django/mysite/apache>
    Order deny,allow
    Allow from all
</Directory>

第二次测试:

WSGIScriptAlias / /home/Webmod/www/djangojquerycontroller/Server/Server/wsgi.py
WSGIPythonPath /home/Webmod/www/djangojquerycontroller:/usr/local/lib/python2.7/dist-packages/django
<Location "/Server">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE Server.settings
    PythonOption django.root /Server
    PythonPath "['/home/Webmod/www/djangojquerycontroller/',     '/home/Webmod/www/djangojquerycontroller/Server','/home/Webmod/www/djangojquerycontroller/Server/dataManager', '/home/Webmod/www/djangojquerycontroller/'] + sys.path"
    PythonDebug On
</Location>

/etc/apache2/sites-available/django.conf

<VirtualHost *:80>

WSGIScriptAlias / /home/Webmod/www/djangojquerycontroller/Server/Server/wsgi.py

ServerName localhost

Alias /media/ /home/Webmod/www/djangojquerycontroller/Server/static/
Alias /static/ /home/Webmod/www/djangojquerycontroller/Server/static/

<Directory /home/Webmod/www/djangojquerycontroller/Server/>
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

所以问题是,这有什么问题?为什么不能运行这个东西,因为它应该..如果可能的话,它将是真正的史诗与一些很酷的终端突击队,我可以复制粘贴,然后它的工作像魔术! =)

任何解决这个问题的想法都会非常好!

1 个答案:

答案 0 :(得分:1)

您只需添加settings.py文件所在位置的路径,然后将环境变量设置为settings。对您的wsgi文件进行以下更改。

sys.path.append('/home/Wedmod/www/djangojquerycontroller/Server')
os.environ["DJANGO_SETTINGS_MODULE"]="settings"