In this guide we will talk about how to deploy a django project using Apache and it’s module, mod_wsgi. This two have a main role in the deploying process.
We will handle the client requests using Apache, which will be the one of delegating the requests that need logic to the Django app through mod_wsgi.
Setting a virtual environment
First, we will install pip
$ sudo easy_install pip
Next we need to install the virtualenv command to create these environments.
$ sudo pip install virtualenv
When virtualenv gets installed we can create a Python virtual environment by typing:
$ virtualenv exampleve
Then, to be able to use it, we will have to activate it:
$ source exampleve/bin/activate
Once we activate our virtual environment we are ready to add our Django project. All the dependencies we install will be located on our "exampleve" folder.
Setting static files directives
Once our Django project is created, we need to specify the directory for both static and media files on the settings project and then generate them. First we are going to specify the directory where the files are going to be generated. We will do that by editing the settings.py file from our project as following:
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') MEDIA_URL = '/media/' STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Next, we are going to collect our static files by typing the following command:
$ python manage.py collectstatic
A folder with the static files should show up on our project
Once we have finished developing our django project we are ready to deploy it. First, we need to install the apache module mod_wsgi. If you are on MAC, be sure that XCode is updated. If you are on Linux (in this guide we show how to do it specifically in debian distributions), be sure that apache is installed. You can install it by typing the following command:
sudo apt-get update & sudo apt-get install apache2
Then we are going to install mod_wsgi. We will use pip for that:
$ sudo pip install mod_wsgi
Once it gets installed we will need to include it into apache’s modules by manually editing the httpd.conf file located in /etc/apache2/httpd.conf.
To do that we are going to run:
Take the path that outputs and modify Apache configuration (httpd.conf) so it has a LoadModule line for wsgi_module with that path. Here is an example of the line we have to include:
LoadModule wsgi_module /Library/Python/2.7/site-packages/mod_wsgi/server/mod_wsgi-py27.so
Then we can check if mod_wsgi is successfully loaded by looking into apache’s loaded modules:
$ apachectl -t -D DUMP_MODULES | grep wsgi
wsgi_module should show up on the output.
Once we get mod_wsgi installed and activated, we have to take a look at our Apache server’s httpd.conf file. At the end of the file we can see that it imports all the .conf files from “/etc/apache2/other/”. To take advantage of this, we will create a new file there, which can be called django.conf for example, with the following configuration:
WSGIScriptAlias / /path/to/wsgi.py WSGIDaemonProcess example python-home=/path/to/virtualenviroment/ python-path=/path/to/project/ WSGIProcessGroup example <directory> <files wsgi.py=""> Require all granted </files> </directory> Alias "/static/" "/Users/andres/Desktop/example/static/" <directory> Require all granted Options +Indexes </directory>
- WSGIScriptAlias indicates the path to the directory to our wsgi.py file on our django project
- WSGIDaemonProcess indicates the path to our virtual environment and the path to our project. Both grouped under the WSGIProcessGroup name
Finally, we just have to restart the apache server one more time to implement the changes made and that’s it!
$ sudo apachectl restart
Everything should be set now. Go to <your_ip>