Django, EC2 and Ubuntu

I just write this post to not forget how I expend a big amount of hours for just running a couple of Django instances within Ubuntu. To not have the same issues in the future, keep it written somewhere will help me and well maybe help others

Help others and they will help you — Adolf Hitler

First thing to know. There is always confussion when talking about Apache in CentOS vs Apache in Debian (Ubuntu inclusive). The main issue is the sites-enables, sites-available option in the Debian distributions.

I have nothing against the sites-way of handling VirtualHosts, but I am used to the CentOS configuration and to work all the time on the httpd.conf file instead on distributed ones. Because of that I decided to go for a single-file configuration.

In Ubuntu 14 you can do it on the /etc/apache2/apache2.conf (As long as I see it works as the httpd.conf of CentOS distributions).

Second thing: I decided to go for a mod_wsgi and not for mod_python, just because I read that this is the right thing to do.

Do the right thing — Every German

So, the main thing to have in mind with wsgi is that in your virtual host you have to specify where the wsgi file of your project is located.

<VirtualHost *:80>
WSGIScriptAlias / /path-to-your-wsgi-file/
<Directory /root-of-your-django-project>
Require all granted

And then you have to fill the file with the right configuration:

import os
import sys
os.environ[“DJANGO_SETTINGS_MODULE”] = “yourproject.settings”
# this two lines where trying to import handlers, that was giving a problem
# apache was reporting errors in the logs caused by it,
# the right configuration is the two lines under
# maybe it is caused because old configurations of wsgi
# i can’t remember what i read, but seems that is because of versions
# import django.core.handlers.wsgi #wrong line
# application = django.core.handlers.wsgi.WSGIHandler() #wrong line
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

As you can see I commented a lot on the last part of the script.

I really don’t remember how or where did I found it, but most of my problems when trying to run Django was caused because of those two last lines.

I believe I read somwhere that it is related with the wsgi version, and maybe I was copy/pasting from an old version.

Besides that, there is nothing else, just include several virtual host and don’t forget to include the wsgi file.

Other sources:

In this article is explained how to run Django using sites-enabled in Ubuntu. As I said, I am used to work everything in one file and therefore I prefered to use apache.conf for the virtualhost. Anyways, the configuration wsgi file I did was a copy from this one.

Another amazing source that explain how to work with virtualenv:

Things to remember:

sudo service apache2 restart

that´s the way to restart apache in Ubuntu

vim /var/log/apache2/error.log

Here is where the apache logs are stored

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.