Deploy Django on Apache?
Asked Answered
V

3

1

I create project using Django 1.8. now ,I want deploy it to server.when I run this command in Ubuntu every thing working find.

python manage.py runserver

Next , I create vertuelhost and enbal key.com site in Apache.

vertualhost file code. ( key.com.conf )

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName key.com
    ServerAlias www.key.com
    WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted

        <Files wsgi.py>
               Require all granted
        </Files>
    </Directory>
</VirtualHost>

the following image show my project folder structure. enter image description here enter image description here

now , I run , key.com url , it gives 500 server internal error. I will check Apache error log fie. I t contain following things.

    [Tue Nov 24 15:16:50.317896 2015] [:error] [pid 12505] [client 127.0.0.1:37918] mod_wsgi (pid=12505): Exception occurred processing WSGI script '/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py'.
[Tue Nov 24 15:16:50.317982 2015] [:error] [pid 12505] [client 127.0.0.1:37918] Traceback (most recent call last):
[Tue Nov 24 15:16:50.318011 2015] [:error] [pid 12505] [client 127.0.0.1:37918]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Tue Nov 24 15:16:50.318117 2015] [:error] [pid 12505] [client 127.0.0.1:37918]     self.load_middleware()
[Tue Nov 24 15:16:50.318131 2015] [:error] [pid 12505] [client 127.0.0.1:37918]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[Tue Nov 24 15:16:50.318236 2015] [:error] [pid 12505] [client 127.0.0.1:37918]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue Nov 24 15:16:50.318249 2015] [:error] [pid 12505] [client 127.0.0.1:37918]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Tue Nov 24 15:16:50.318325 2015] [:error] [pid 12505] [client 127.0.0.1:37918]     self._setup(name)
[Tue Nov 24 15:16:50.318337 2015] [:error] [pid 12505] [client 127.0.0.1:37918]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Tue Nov 24 15:16:50.318355 2015] [:error] [pid 12505] [client 127.0.0.1:37918]     self._wrapped = Settings(settings_module)
[Tue Nov 24 15:16:50.318364 2015] [:error] [pid 12505] [client 127.0.0.1:37918]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Tue Nov 24 15:16:50.318380 2015] [:error] [pid 12505] [client 127.0.0.1:37918]     % (self.SETTINGS_MODULE, e)
[Tue Nov 24 15:16:50.318399 2015] [:error] [pid 12505] [client 127.0.0.1:37918] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings

expect some expert help.

=======================wsgi.py============

    """
WSGI config for test2 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.8/howto/deployment/wsgi/
"""

import os,sys
sys.path.append('/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite')

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

===================setting.py==================================

    """
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-vvzx5(=sztv%*^ljnpkdc&!q+ltws0-%a76^v1e_z9g97-k2%'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'marcador',
    'crispy_forms',
    'bootstrap_pagination',
)

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',
    'django.middleware.security.SecurityMiddleware',
)

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.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/Berlin'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

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


LOGIN_URL = 'mysite_login'
LOGOUT_URL = 'mysite_logout'
LOGIN_REDIRECT_URL = 'marcador_bookmark_list'



CRISPY_TEMPLATE_PACK = 'bootstrap3'
Vange answered 24/11, 2015 at 9:48 Comment(13)
Instead of posting random screenshots of your IDE, please post the contents of your wsgi file. Are you actually doing anything to put the settings file on the path, to fix the error?Magnesia
@DanielRoseman i added that file content.Vange
@Vange Your project path is missing in python sys path, Please refer the below link, https://mcmap.net/q/420471/-importerror-could-not-import-settingsDewitt
@Vange you can add & check the below lines in your wsgi file at the top, import sys sys.path.append('YOUR_PROJECT_PATH')Dewitt
@Dewitt I added above line but still error is same.Vange
@Vange can you post your wsgi file, It should works ..Dewitt
@Dewitt I added it to my question . yes of course ,when I type 'python manage.py runserver' every thing working find. but i want create host and deploy this project in key.com url. still not reslove my trouble.Vange
@uma, I think this will work .. Add the virtualenv path same as project path in wsgi file ... Also restart the apache and check again ...Dewitt
@Dewitt no sir still , when i type key.com in browser give 500 error. error log content same thing. Exception occurred processing WSGI script . i also , do it another my project. it also give same out put.Vange
@uma, Sorry i forgot to add these commands, You can try like below, import os,sys sys.path.insert(0, 'VIRTUAL_ENV_PATH') sys.path.insert(1, 'PROJECT_PATH')Dewitt
@Dewitt sir where should i put this code. It is above on wsgi.py file. ?Vange
@uma, Yes mam, try and check. Please let me know if works or not ?Dewitt
Let us continue this discussion in chat.Vange
V
0

finally I could successfully deploy my project.I enabled Apache this module early.

refer this link

--------------this is my virtual host file----------------------------------

WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName key.com
    ServerAlias www.key.com

    Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
    Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/


    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
           Require all granted
    </Directory>

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
           Require all granted
    </Directory>

    WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
        <Files wsgi.py>
               Require all granted
        </Files>
    </Directory>

</VirtualHost>

-----------------wsgi.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.8/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()

I think this will be help to other Django developers.Thank you all developers those who help me.

Vange answered 26/11, 2015 at 4:41 Comment(0)
M
0

As I said in the comments, you're not doing anything to add your project to the PYTHONPATH so that the WSGI app can actually find it.

It seems from your directory structure that you're using a virtualenv; you need to activate that either within the .wsgi file or in the Apache configuration itself. The Django docs recommend the latter.

Magnesia answered 24/11, 2015 at 11:20 Comment(1)
please , tell me what i should change. I add **wsgipython path" like this #23679567 Question. but still same out put.Vange
V
0

finally I could successfully deploy my project.I enabled Apache this module early.

refer this link

--------------this is my virtual host file----------------------------------

WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName key.com
    ServerAlias www.key.com

    Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
    Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/


    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
           Require all granted
    </Directory>

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
           Require all granted
    </Directory>

    WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py

    <Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
        <Files wsgi.py>
               Require all granted
        </Files>
    </Directory>

</VirtualHost>

-----------------wsgi.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.8/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()

I think this will be help to other Django developers.Thank you all developers those who help me.

Vange answered 26/11, 2015 at 4:41 Comment(0)
S
-2

In your wsgi.py try changing

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

to

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

Since both files are in the same directory/module.

Shari answered 24/11, 2015 at 10:12 Comment(1)
Did you do sudo service apache2 restart? This has to be the issue, since you moved your wsgi.py file you need to alter the variable, otherwise Django will not be able to find your settings file - as indicated by the exception No module named mysite.settings. Post your settings file too, if possible.Shari

© 2022 - 2024 — McMap. All rights reserved.