About the Blog

Its about anything and everything. I, Steven Hancock started this blog for a variety of reasons. I want to start documenting my life and sharing that with others, whether that's family, friends, strangers or my future self. I also want to start sharing my experiences with others in hopes that others can learn from me. Perhaps I can help someone set up an Ubuntu server, write a Django Web Application, or setup a Phonegap Mobile App.

That's it. I'm hear to share. Nothing more, nothing less. I will be covering a wide variety of topics so feel free to browse for the blog entries that interest you most.

Django Setup Tutorial

May 27, 2013

Django is a Python web framework. I was looking for some sort of technology to run this blog. I didn't want to re-invent the wheel with developing a minimal framework that would only make sense to me and at the same time I didn't want to just copy what I've already been doing (mostly php development). I haven't done a ton of Python development, but the little work that I have done with I really enjoyed it. I know there are lots of applications that use Python as their server side scripting. SO... if I'm going to try something new and create a blog, I might as well try something new from a developer stand point as well. Python was the language and Django was the framework. Why Django? I've heard a bit about it and, to be brutal honest, it was the first thing that I came across when looking for a Python web framework.

The setup for Django was mostly straightforward and the documentation available from Django is terrific. I did run into a few hiccups though so I thought I would share my experience.

Setup Tutorial

This setup tutorial is a guide for setting up Django, Python and Mysql on an Ubuntu (12.04) server.

Update system's ATP's database

$ sudo apt-get update

Install mysql if you already don't have it (Django can also be configured to run with other Databases such as postgreSQL & SQLite).

$ sudo apt-get install mysql-server

While not mandatory, it is highly recommended that you set a password for the MySQL administrative "root" user.  Install python requirements and Django. Django has great documentation. See https://docs.djangoproject.com/en/1.5/intro/install/ for install help. Ubuntu comes with Python already installed so don't worry about it, but we may need to include some Python packages.

$ sudo apt-get install python-django python-mysqldb python-flup python-yaml libapache2-mod-wsgi

Check to see if Django is working.

$ python -c "import django; print(django.get_version())"

The version of Django that was installed was 1.3.1 I want to update that to the latest release, so I installed pip (a tool for installing python packages). FYI the reason why 1.3.1 was installed is that the Ubuntu repository for Django is behind the latest release (happens with most packages). Remove the old Django app.

$ sudo rm -R /usr/lib/python2.7/dist-packages/django/

Install the up to date one.

$ sudo pip install Django --upgrade

Check Django version again. Now I've got 1.5.1

$ python -c "import django; print(django.get_version())"

Make a new directory to store your django projects in (don't use /var/www).

$ mkdir /path/to/djangoprojects

Create your first Django project.

$ cd /path/to/djangoprojects
$ django-admin.py startproject myproject

Django has a great tutorial to help you get started on your first project. I suggest you try it out.

To setup your server we will need to create a new virtual host. I'm already running Apache so I will use its wsgi engine and also use Apache to host my static files. Collect the static elements for Django admin after setting the static directory in settings.py.

$ sudo python ./manage.py collectstatic 

Create new virtual host for yourdomain.com

$ sudo vi /etc/apache2/sites-available/yourdomain.com.conf

Place the following, substituting for your own values

WSGIPythonPath /path/to/djangoprojects/proj
<VirtualHost *:80>
  ServerName randomscribes.com
  ServerAlias www.randomscribes.com

  WSGIScriptAlias / /path/to/djangoprojects/proj/proj/wsgi.py
  <Directory /path/to/djangoprojects/proj/proj>
    <Files wsgi.py>
      Order deny,allow
      Allow from all

Create new virtual host for static.yourdomain.com

$ sudo vi /etc/apache2/sites-available/static.yourdomain.com.conf

And place the following text in the file.

<VirtualHost *:80>
    DocumentRoot /var/www/static/
    ServerName static.yourdomain.com
    <Directory "/var/www/static">
        Allow from all

Restart apache and you should be good to go!

$ sudo service apache2 restart