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:
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.
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
Before starting on this article, be sure that you have gone through the previous 2 in this series. You can find them here:
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
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/username/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.
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.
There is a caveat to virtual host definitions. Static content is not supported. To serve the static content properly, you can update few settings in the file MyTestProject/settings.py and use following definition of virtual host.
<VirtualHost *:80> ServerName domain1.com ServerAlias www.domain1.com WSGIScriptAlias / /home/username/public_html/domain1.com/MyTestProject.wsgi Alias /static/ /home/username/public_html/domain1.com/static/ <Location "/static/"> Options -Indexes </Location> </VirtualHost>
The Alias Directive lets Apache know that it should not allow Django or mod_wsgi to handle anything located under the /static/ directory of your domain. You can use any directory but make sure that it is available under /home/username/public_html/domain1.com/. In our example, the name of the directory is static. Update settings.py by setting the variables for MEDIA_ROOT and MEDIA_URL.
Find and update the settings below.
MEDIA_ROOT = '/home/username/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.
It is a good idea to get into the habit of restarting apache every time you make changes to the project.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.