One of the tried and tested way to get Django into production is to deploying a django app with Apache and mod_wsgi.
mod_wsgi is one of the Apache module which is used to host any python WSGI application,including django.You can find details about how to use mod_wsgi and install here: official mod_wsgi documentation.
Basic configuration
Once you are done with the mod_wsgi installation and activation,you can edit your Apache server’s httpd.conf file and add the following content.If any of you are using apache version older than 2.4 then you can replace the Require all granted in the below code with Allow from all and you can also add one extra line Order deny,allow above it.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py WSGIPythonHome /path/to/venv WSGIPythonPath /path/to/mysite.com <Directory /path/to/mysite.com/mysite> <Files wsgi.py> Require all granted </Files> </Directory>
So let’s go into code each line at once.So the first line WSGIScriptAlias line is the base URL path,which is the path you want to server your application at (/ indicates the root url). The second line is the location of your WSGI file. That file tells Apache to serve any request below the given URL using the WSGI application defined in that file.
You can add the path to virtualenv using WSGIPythonHome,if you have installed your project’s python dependencies inside a virtualenv. For more details you can refer: mod_wsgi virtualenv guide
The line after that in the above code starting form WSGIPythonPath ensures that your project package is available for import on the Python path.
The work of <Directory> is to ensure that Apache can access your wsgi.py file.
In the next line, we are ensuring that wsgi.py
file with a WSGI application object exists.
Serving files
Django is not going to server files itself,it will leave that job to web server chosen by you.We will recommend you to choose a separate web server which is not already running django.Here are some good choices:
- Nginx
- Apache(stripped-down version)
For example,this code below will sets up django at the site root,but serves robots.txt, favicon.ico and anything in the /static/ and /media/ URL space as a static file.All other remaining URLs will be served using mod_wsgi:
Alias /robots.txt /path/to/mysite.com/static/robots.txt Alias /favicon.ico /path/to/mysite.com/static/favicon.ico Alias /media/ /path/to/mysite.com/media/ Alias /static/ /path/to/mysite.com/static/ <Directory /path/to/mysite.com/static> Require all granted </Directory> <Directory /path/to/mysite.com/media> Require all granted </Directory> WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py <Directory /path/to/mysite.com/mysite> <Files wsgi.py> Require all granted </Files> </Directory>
Serving the admin files
The django development server automatically serves the static file of the admin app,when django.contrib.staticfiles is in INSTALLED_APPS.
In the django distribution the admin files live in (django/contrib/admin/static/admin)
To handle all admin,we recommend you to use django.contrib.staticfiles, but here are three other approaches which also you can use:
- We can create a symbolic link to static files from admin to your document root.
- You can use an Alias directive to alias the appropriate URL to the actual location of the admin files.
- You can copy the admin static files so that they live within your Apache document root.
So this is all about how to use apache and mod_wsgi in django.