Table of Contents
URL: https://www.progressiverobot.com/using-mod-wsgi-to-serve-applications-on-ubuntu-12-04/
<span class="warning"><p></p>
<div name="status-deprecated" data-unique="status-deprecated"></div><h2 id="status-deprecated"><strong>Status:</strong> Deprecated</h2>
This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
- Upgrade to Ubuntu 14.04.
- Upgrade from Ubuntu 14.04 to Ubuntu 16.04
- Migrate the server data to a supported version
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
Before starting on this article, be sure that you have gone through the previous 2 in this series. You can find them here:
Creating the Django Application:
First of all, we will navigate to the home directory. Create a new directory and switch into it:
mkdir -p ~/public_html/domain1.com
cd ~/public_html/domain1.com
After that, go ahead and create a project with the help of the django-admin.py tool.
django-admin.py startproject MyTestProject
Creating the Virtual host & WSGI file:
To serve a Django app properly, it is important for Apache to know that it is supposed to forward certain types of requests to mod_wsgi. It is also important to create wsgi file that tells mod_wsgi how to handle these requests. We will setup a virtual host to accomplish these tasks. It will tell Apache the location of wsgi file and setup the file accordingly.
Open up the new virtual host file.
sudo nano /etc/apache2/sites-available/domain1.com
Next, enter below definition for the virtual host:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
WSGIScriptAlias / /home/<i>username</i>/public_html/domain1.com/MyTestProject.wsgi
</VirtualHost>
Once we have instructed apache to use the wsgi file specified above and pass the receiving request to mod_wsgi, we will create the mod_wsgi file itself.
nano ~/public_html/domain1.com/MyTestProject.wsgi
Type in the following configuration:
import os
import sys
sys.path.append('~/public_html/domain1.com/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'MyTestProject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This definition ensures that the necessary modules will be imported. Moreover, it appends the Django project’s path to Python’s path and sets up a number of variables that helps mod_wsgi to work. Once you are done with it, you will need to enable the virtual host and restart Apache.
sudo a2ensite domain1.com
sudo /etc/init.d/apache2 reload
If everything goes as expected, you will be able to see your domain (droplet IP) in the browser, and get the newly created application. Reload Apache in case you receive any NameVirtualHost or port errors.
Static Content
nano /home/<i>username</i>/public_html/domain1.com/MyMyTestProject/settings.py
Find and update the settings below.
MEDIA_ROOT = '/home/<i>username</i>/public_html/domain1.com/static/'
MEDIA_URL = '/static/'
Finally, restart Apache to the changes into effect.
sudo /etc/init.d/apache2 reload
You can access any items that have been placed in the MEDIA_ROOT through http://www.domain1.com/static/path/to/file.
Changes to Django App
It is a good idea to get into the habit of restarting apache every time you make changes to the project.