Deploy Django on Apache

Source: Internet
Author: User

Currently, Apache and mod_python are the most robust combinations for deploying Django on production servers. Mod_python is an Apache plug-in embedded with python in Apache. It loads Python code into the memory when the server starts.

Django requires support for apaceh 2.x and mod_python 3.x.

For Apache configuration, see: http://www.djangoproject.com/r/apache/docs/

 

Deploy using mod_python

1. to configure Django Based on mod_python, install Apache with the available mod_python module.

2. There should be a loadmodule command in the Apache configuration file. It looks like this:

LoadModule python_module /usr/lib/apache2/modules/mod_python.so

3. Configure Apache to locate the request URL to the Django application:

<VirtualHost *:80>
ServerName www.example.com

<Location "/mysite1">
SetHandler python‐program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite1.settings
PythonAutoReload Off
PythonDebug Off
PythonPath "['/var/www/html/mysite1'] + sys.path"
PythonInterpreter mysite1
</Location>

<Location "/mysite2">
SetHandler python‐program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite2.settings
PythonAutoReload Off
PythonDebug Off
PythonPath "['/var/www/html/mysite2'] + sys.path"
PythonInterpreter mysite2
</Location>

[......]
</VirtualHost>

It tells Apache that any URL after the path/mysite is processed using Django's mod_python. It passes the value of django_settings_module to know which configuration should be used at this time.

View the mod_python document to obtain a detailed command list.

4. Restart Apache and check http: // www.example.com/mysite:

/etc/init.d/apache2 restart
Deploy with mod_wsgi

1. download and install the mod_wsgi module and generate mod_wsgi.so and wsgi. conf.

2. Load the module in Configuration:

LoadModule python_module /usr/lib/apache2/modules/mod_wsgi.so

3. Modify the Apache configuration file httpd. conf.

<VirtualHost *:80>
ServerName www.example
DocumentRoot /var/www/html/mysite
WSGIScriptAlias / /var/www/html/mysite/apache/django.wsgi
<Directory />
Order deny,allow
Allow from all
</Directory>
<Directory /apache>
Allow from all
</Directory>
</VirtualHost>

4. Create and configure the wsgi configuration file:

# filename:mysite.apache.django.wsgi
import os, sys

#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
print >> sys.stderr, sys.path
shell>chmod a+x django.wsgi

5. modify the configuration file settings. py of the Django project:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'admin',
'PASSWORD': 'admin123',
'HOST': '127.0.0.1',
'PORT': '3306', }
}

TEMPLATE_DIRS = (
'/var/www/html/mysite/templates',
)

6. Restart Apache to access http://www.example.com/mysite

/etc/init.d/apache2 restart

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.