我正在尝试在具有Ubuntu 14.04.1 LTS的服务器上部署我的第一个Django应用程序。 我安装了apache和其他东西:
apt-get install apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
然后我安装了wsgi模块:
apt-get install libapache2-mod-wsgi
我编辑了ports.conf以侦听端口8080,因为我已经在端口80上有另一台服务器,我在那里托管html + js + css资源(它们正确可用)。 在准备步骤之后,我将Django应用程序移动到新的Apache服务器上,特别是移动到html目录中。然后通过添加以下内容编辑apache2.conf文件:
WSGIScriptAlias / /var/www/html/MyProjectName/wsgi.py
WSGIPythonPath /var/www/html/MyProjectName
<Directory /var/www/html/MyProjectName>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
配置指令的其余部分是我使用给定命令安装Apache后的默认配置。
然后,我注意到在mods-available
文件夹中,我有wsgi模块,但不在mods-enabled
文件夹中,所以我在最后一个文件夹中创建了一个符号链接。
当我启动Apache时,如果我打开localhost:8080,则会出现内部服务器错误。访问error.log文件,我看到以下内容:
[Mon Mar 09 16:38:20.730865 2015] [mpm_event:notice] [pid 24379:tid 140525609281408] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Mon Mar 09 16:38:20.731257 2015] [core:notice] [pid 24379:tid 140525609281408] AH00094: Command line: '/usr/sbin/apache2'
[Mon Mar 09 16:38:28.673948 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] mod_wsgi (pid=24383): Target WSGI script '/var/www/html/MyProjectName/wsgi.py' cannot be loaded as Python module.
[Mon Mar 09 16:38:28.674120 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] mod_wsgi (pid=24383): Exception occurred processing WSGI script '/var/www/html/MyProjectName/wsgi.py'.
[Mon Mar 09 16:38:28.674240 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] Traceback (most recent call last):
[Mon Mar 09 16:38:28.674299 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] File "/var/www/html/MyProjectName/wsgi.py", line 13, in <module>
[Mon Mar 09 16:38:28.674438 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] from django.core.wsgi import get_wsgi_application
[Mon Mar 09 16:38:28.674480 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] ImportError: No module named django.core.wsgi
我从哪里开始了解问题? 我的理解是Apache WSGI模块无法正常工作。
为了完整性,我指向的wsgi.py文件如下所示:
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProjectName.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
sys.path.append('/var/www/')
sys.path.append('/var/www/html/')
sys.path.append('/var/www/html/MyProjectName')
设置文件如下:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = '<some_key_here>'
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'anothercustomapp',
)
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',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'MyProjectName.urls'
WSGI_APPLICATION = 'MyProjectName.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MyDatabaseName',
'USER' : '<username>',
'PASSWORD' : '<password>',
}
}
SERIALIZATION_MODULES = {
'json': 'wadofstuff.django.serializers.custom_json'
}
LANGUAGE_CODE = 'it-IT'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
在路径/ var / www / html /我有以下结构:
===== EDIT =====
使用以下命令安装Django和python-mysqldb之后:
apt-get install python-setuptools
easy_install django
apt-get install python-mysqldb
我最终得到以下error.log:
[Mon Mar 09 16:52:13.610937 2015] [mpm_event:notice] [pid 24789:tid 139911380785024] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Mon Mar 09 16:52:13.611288 2015] [core:notice] [pid 24789:tid 139911380785024] AH00094: Command line: '/usr/sbin/apache2'
[Mon Mar 09 16:52:18.056083 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] mod_wsgi (pid=24792): Target WSGI script '/var/www/html/MyProjectName/wsgi.py' cannot be loaded as Python module.
[Mon Mar 09 16:52:18.056256 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] mod_wsgi (pid=24792): Exception occurred processing WSGI script '/var/www/html/MyProjectName/wsgi.py'.
[Mon Mar 09 16:52:18.056302 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] Traceback (most recent call last):
[Mon Mar 09 16:52:18.056346 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/var/www/html/MyProjectName/wsgi.py", line 14, in <module>
[Mon Mar 09 16:52:18.056485 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] application = get_wsgi_application()
[Mon Mar 09 16:52:18.056514 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Mar 09 16:52:18.072789 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] django.setup()
[Mon Mar 09 16:52:18.072882 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/__init__.py", line 20, in setup
[Mon Mar 09 16:52:18.088060 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Mar 09 16:52:18.088166 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 46, in __getattr__
[Mon Mar 09 16:52:18.097590 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] self._setup(name)
[Mon Mar 09 16:52:18.097632 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 42, in _setup
[Mon Mar 09 16:52:18.097692 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] self._wrapped = Settings(settings_module)
[Mon Mar 09 16:52:18.097725 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 98, in __init__
[Mon Mar 09 16:52:18.097778 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] % (self.SETTINGS_MODULE, e)
[Mon Mar 09 16:52:18.097833 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] ImportError: Could not import settings 'MyProjectName.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named MyProjectName.settings