在ubuntu服务器16.04上使用Apache2,mod_wgi和django 1.9进行部署

时间:2016-08-30 14:32:50

标签: django apache ubuntu mod-wsgi

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

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

stateChangeSuccess

django网站位于/ var / www / MySite,该文件夹具有以下权限:

<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>

内部权限如下:

drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 MySite

最后进入MySite / 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

wsgy.py文件的内容如下(默认从django生成):

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

最后我的settings.py如下:

"""
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()

我没有使用virtualenv我用pip3安装了所有软件包,我使用的是apache2版本:

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'

和libapache2-mod-wsgy-py3,运行sudo apache2ctl restart后我在error.log上遇到以下错误:

Server version: Apache/2.4.18 (Ubuntu)

当我尝试从Windows主机(通过broswer)到达该站点时,我收到了一条内部服务器错误消息,这些成瘾日志在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'.

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

1 个答案:

答案 0 :(得分:2)

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提供的版本,因为它太旧了。