subcritical


Running a Django Server at Home

 Eric Williams | 26 April, 2016
 Comments
linux sysadmin django 

This is a quick run-down of how to host a Django website on your home broadband connection.

Why in the world would you want to do this? It has some advantages: Broadband has decent bandwidth these days, and cloud VMs generally can't compete with run-of-the-mill PC hardware in terms of memory, disk throughput, and CPU power.

There are disadvantages too, obviously, but for non-production systems running your own server can be a lot of fun!

Install Ubuntu Server

Ubuntu Server is fine and stable, and a brand new version just came out.

This post refers to 14.04 (Trusty), since I haven't gotten around to setting up a new version as of yet. 14.04 still has a few years of support left, so don't be afraid to deploy with it.

Install Apache

I know all the cool kids like Nginx, but Apache has modwsgi and modpython, so I find it suits my needs.

sudo apt-get install apache2-server libapache-mod-wsgi

Create a project directory

You can put your Django projects anywhere, but make sure it's readable / writable by the user or group that's running Apache: www-data.

sudo mkdir -p /srv/projects/
sudo chown myuser:www-data
cd /srv/projects

Create a virtual environment to host your Django stuff.

sudo apt-get install python-virtualenv
virtualenv .
. bin/activate
pip install django
django-admin startproject website

Now, setup your website Django project as you like.

Configure Apache

Setup a conf file for your website, /etc/apache/sites-available/website.conf

WSGIPythonPath /srv/projects/website/:/srv/projects/lib/python2.7:/srv/projects/lib/python2.7/plat-x86_64-linux-gnu:/srv/projects/lib/python2.7/lib-tk:/srv/projects/lib/python2.7/lib-old:/srv/projects/lib/python2.7/lib-dynload:/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/srv/projects/local/lib/python2.7/site-packages:/srv/projects/lib/python2.7/site-packages


<VirtualHost *:80>
    ServerName internal.server.com
    ServerAlias subcritical.org
    ServerAlias www.subcritical.org
    ServerAlias media.subcritical.org
    SetEnv DJANGO_SETTINGS_MODULE website.settings

    WSGIScriptAlias / /srv/projects/subc/subc/wsgi.py

    Alias /static/ /var/www/static/
    Alias /media/ /var/www/media/

    <Directory "/srv/projects/website/website/">
        Require all granted
    </Directory>

    <Location /static/>
        Order allow,deny
        Allow from all
        SetHandler none
    </Location>
    <Location /media/>
        Order allow,deny
        Allow from all
        SetHandler none
    </Location>
</VirtualHost>

Enable the website

sudo a2dissite 00-default 
sudo a2ensite website 
sudo service apache2 restart

Check the website internally

wget http://internal.server.com

Forward your Router

Make sure to forward port 80 and 443 from your router to your internal server.

Setup Dynamic DNS

If you have a website on a dynamic IP address, which is usually the case with home IP connections, you need to have some way of keeping that IP address known.

PyDDNS can help with this, as long as your domain is hosted on Amazon's Route 53. Otherwise, you can use Dynamic DNS services like No-IP to take care of that.