django - 在 ubuntu 服务器 16.04 上使用 Apache2、mod_wgi 和 django 1.9 部署

标签 django apache ubuntu mod-wsgi

我正在尝试在 ubuntu 服务器 16.04 上为我的 django 网站设置部署环境。当尝试连接到服务器时,我收到内部服务器错误消息。

我的/var/apache2/site-available/000-default.conf 的内容如下:

<VirtualHost *:80>
   ServerAdmin mymailaddress@pippo.com
   DocumentRoot /var/www/MySite

   Alias /static /var/www/MySite/static
   <Directory /var/www/MySite/static>
       Require all granted
   </Directory>

   Alias /media /var/www/MySite/media
   <Directory /var/www/MySite/media>
       Require all granted
   </Directory>

   WSGIScriptAlias / /var/www/MySite/MySite/wsgy.py
   <Directory /var/www/MySite/MySite>
       <Files wsgy.py>
          Require all granted
       </Files>
   </Directory>
   WSGIDaemonProcess MySite python-path=/var/www/MySite:/usr/lib/python3.5/dist-packages
   WSGIProcessGroup MySite

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log
</VirtualHost>

django 网站位于/var/www/MySite 并且该文件夹具有以下权限:
drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 MySite

内部权限如下:
total 44
drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 .
drwxr-xr-x  4 root root     4096 Aug 29 17:44 ..
drwxr-xr-x  3 root www-data 4096 Aug 30 04:44 MySite
-rw-r--r--  1 root www-data    0 Aug 29 17:39 __init__.py
-rwxr-xr-x  1 root www-data  251 Aug 29 17:39 manage.py
drwxr-xr-x  3 root www-data 4096 Aug 29 17:39 media
drwxr-xr-x  4 root www-data 4096 Aug 29 18:28 navigation
drwxr-xr-x  5 root www-data 4096 Aug 29 18:28 projects
drwxr-xr-x  4 root www-data 4096 Aug 29 18:28 public_interfaces
drwxr-xr-x  6 root www-data 4096 Aug 29 17:39 static
drwxr-xr-x  3 root www-data 4096 Aug 29 17:39 templates
drwxr-xr-x  4 root www-data 4096 Aug 29 18:28 user_extended

最后进入 MySite/Mysite:
total 32
drwxr-xr-x  3 root www-data 4096 Aug 30 04:44 .
drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 ..
-rw-r--r--  1 root www-data    0 Aug 29 17:39 __init__.py
drwxr-xr-x  2 root www-data 4096 Aug 29 18:38 __pycache__
-rw-r--r--  1 root www-data 4408 Aug 29 19:12 settings.py
-rw-r--r--  1 root www-data 1440 Aug 29 17:39 urls.py
-rw-r--r--  1 root www-data   95 Aug 29 17:39 views.py
-rw-r--r--  1 root www-data  393 Aug 30 04:44 wsgi.py

wsgy.py 文件的内容如下(默认从 django 生成):
"""
WSGI config for MySite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

最后我的settings.py如下:
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = **************

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['www.mysite.com', 'mysite.com']


# Application definition

INSTALLED_APPS = [
    'crispy_forms',
    'haystack',
    'projects.apps.ProjectsConfig',
    'navigation.apps.NavigationConfig',
    'public_interfaces.apps.PublicInterfacesConfig',
    'user_extended.apps.UserExtendedConfig',
    'django.contrib.admin',
    'django.contrib.sites',
    'registration',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    '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 = 'MySite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'MySite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':'mysitedb',
        'USER': 'citro',
        'PASSWORD': *****************,
        'HOST': 'localhost',
        'PORT': '',
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')

# Email settings
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.email.com'
EMAIL_HOST_USER = 'pippo@email.com'
EMAIL_HOST_PASSWORD = *******
EMAIL_PORT = ********

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value.
REGISTRATION_FORM = 'user_extended.forms.MySiteRegistrationForm'

CRISPY_TEMPLATE_PACK = 'bootstrap3'

我没有使用 virtualenv 我用 pip3 安装了所有软件包,我使用的是 apache2 版本:
Server version: Apache/2.4.18 (Ubuntu)

和 libapache2-mod-wsgy-py3,在运行 sudo apache2ctl restart 后,我​​在 error.log 上收到以下错误:
[Tue Aug 30 07:11:41.452762 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00494: SIGHUP received.  Attempting to restart AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Tue Aug 30 07:11:41.515933 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Aug 30 07:11:41.515946 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Runtime using Python/3.5.2.
[Tue Aug 30 07:11:41.516784 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Aug 30 07:11:41.516816 2016] [core:notice] [pid 4217:tid 140136362780544] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 30 07:11:41.592253 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:11:41.592282 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.

当我尝试从 Windows 主机(通过浏览器)访问该站点时,我收到一条内部服务器错误消息,并且这些令人上瘾的日志记录在 error.log 中:
[Tue Aug 30 07:11:41.452762 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00494: SIGHUP received.  Attempting to restart AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Tue Aug 30 07:11:41.515933 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Aug 30 07:11:41.515946 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Runtime using Python/3.5.2.
[Tue Aug 30 07:11:41.516784 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Aug 30 07:11:41.516816 2016] [core:notice] [pid 4217:tid 140136362780544] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 30 07:11:41.592253 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:11:41.592282 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.
[Tue Aug 30 07:17:54.414936 2016] [wsgi:error] [pid 10223:tid 140136256911104] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:17:54.414984 2016] [wsgi:error] [pid 10223:tid 140136256911104] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.
[Tue Aug 30 07:17:55.464287 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] mod_wsgi (pid=10223): Target WSGI script '/var/www/MySite/MySite/wsgi.py' cannot be loaded as Python module.
[Tue Aug 30 07:17:55.464350 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] mod_wsgi (pid=10223): Exception occurred processing WSGI script '/var/www/MySite/MySite/wsgi.py'.
[Tue Aug 30 07:17:55.483045 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] Traceback (most recent call last):
[Tue Aug 30 07:17:55.483118 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/var/www/MySite/MySite/wsgi.py", line 16, in <module>
[Tue Aug 30 07:17:55.483123 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     application = get_wsgi_application()
[Tue Aug 30 07:17:55.483129 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Aug 30 07:17:55.483132 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     django.setup()
[Tue Aug 30 07:17:55.483141 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup
[Tue Aug 30 07:17:55.483144 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Aug 30 07:17:55.483150 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__
[Tue Aug 30 07:17:55.483152 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     self._setup(name)
[Tue Aug 30 07:17:55.483157 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup
[Tue Aug 30 07:17:55.483160 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     self._wrapped = Settings(settings_module)
[Tue Aug 30 07:17:55.483164 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__
[Tue Aug 30 07:17:55.483167 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Aug 30 07:17:55.483181 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Tue Aug 30 07:17:55.483184 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Aug 30 07:17:55.483189 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Tue Aug 30 07:17:55.483194 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Tue Aug 30 07:17:55.483199 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Tue Aug 30 07:17:55.483203 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Tue Aug 30 07:17:55.483208 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Tue Aug 30 07:17:55.483213 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Tue Aug 30 07:17:55.483217 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Tue Aug 30 07:17:55.483237 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] ImportError: No module named 'MySite'

任何建议表示赞赏。谢谢。

最佳答案

FWIW。您的日志文件与您所说的配置所说的不完全匹配。

首先,尝试删除 /usr/lib/python3.5/dist-packages来自 python-path选项。它不应该是必需的,添加第二条路径可能会触发 Python 3 下的问题,该问题已在一段时间前在 mod_wsgi 中修复。不幸的是,Ubuntu 在 2 年前发布了一个 mod_wsgi 版本,即使在最近的 Ubuntu 版本中也是如此。它们比最新版本落后 30 多个版本。一般建议不要使用 Ubuntu 提供的 mod_wsgi 包,自己从源代码编译 mod_wsgi。不支持 Ubuntu 提供的版本,因为它太旧了。

关于django - 在 ubuntu 服务器 16.04 上使用 Apache2、mod_wgi 和 django 1.9 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230359/

相关文章:

c++ - Apache 交叉编译错误./gen_test_char : cannot execute binary file

python - 找不到页面 (404)

python - uWSGI 异步函数不知道 Django 的日志设置

python - Django 模板 FOO_set.all 返回空白

python - 如何将.html文件输出到.doc文件django

php - 要启用扩展,请验证它们是否已在这些 .ini 文件中启用 - Vagrant/Ubuntu/Magento 2.0.2

php - 根据 url 上的国家/地区将用户重定向到正确的数据库,我使用 i18n for php,我使用 kohana 框架

php - Windows 8(64 位)中的 WAMP 不起作用

ruby - 由于 gpg 错误,无法在 Ubuntu 16.04 上安装 Ruby rvm

linux - 使用 make 仅复制已更改的文件