Fork me on GitHub

Django Startup Template

Set-up a new Django 1.8 project on a Webfaction server using django-startup-template.

Features

  1. Super easy deployments with Git.
  2. Static files hosting on AWS S3 (via django-storage).
  3. Javascript & CSS compression (via django-compress).
  4. Local Django debugging (via django-debug-toolbar).
  5. Database schema migrations (via south).

Requirements

  1. An account on Webfaction (Non-affiliate link).
  2. An account on Amazon Web Services (AWS) with S3 enabled.
  3. Git - version control and easy deployments.
  4. Pip - a python package manager
  5. virtualenv - python virtual environments
  6. virtualenvwrapper - python virtual environments made easier

Contents


    Project Settings

    Generate personalised instructions by providing some basic details. No data is being collected nor stored.

    eg. my_cool_project

    eg. awesome_dev

    eg. web121.webfaction.com or 75.126.24.79

    Install

    Create a new folder on your system and switch to that folder.

        mkdir  && cd 

    Create and initialize a new virtual environment.

        source /usr/local/bin/virtualenvwrapper.sh
        mkvirtualenv env_
        workon env_

    Download and install the latest stable version of Django (1.8).

        pip install django

    Create a new Django project based on this repo in a new folder.

        django-admin.py startproject --template=https://github.com/rudasn/django-startup-template/zipball/master --extension=py,md,conf,.gitignore 
        cd 

    Install dependencies.

        pip install -r requirements/development.txt

    Services

    Application

    Create a new Django application on webfaction called .

    Go to the New Application page.

    Name should be .

    App Category should be Django.

    App Type should be Django 1.8.

    Click Save

    Once the application is created look for its Port and type it here .

    Database

    Create and set-up a PostgreSQL database.

    Go to the New Database page.

    Name can be .

    Database type should be PostgreSQL.

    Create a new Database Owner.

    Username can be .

    Password can be
    (This was randomly generated. Generate another one.)

    Click Save

    Website & Domain

    Create a website and a domain. It's best to do this now as it takes some time for the domains to kick in. Hopefuly by the time you have finished with the rest of the steps everything should be working.

    Go to the New Website page.

    Name can be .

    Create two new Domains.

    ..webfactional.com and www...webfactional.com.

    For Contents choose Reuse an existing application and select from the list.

    Click Save.

    Git

    Skip this section if you already have a git application running on your server named git.

    Create a Git application to host and deploy our host.

    Go to the New Application page.

    Name should be git.

    App Category should be Git.

    App Type should be Git 1.7.4.1.

    Click Save

    Amazon Web Services S3

    Create a new bucket.

    Go to your AWS S3 console.

    Click on Create Bucket.

    Bucket Name can be .

    Once the bucket is created select it from the list and click on Properties.

    Create a new Permissions entry allowing Everyone to just have View Permissions.

    Create a new user for this project.

    Go to AWS Identity and Access Management page.

    Create a new user named . Make sure the option to "Generate an access key for each User" is checked.

    Once the user is created click on "Show User Security Credentials" and copy & paste them here.

    Assign permissions to the user you just created to manage the project's bucket.

    Go back to the AWS Identity and Access Management page.

    Select the user.

    Click on Permissions (on the tabs at the bottom of the page).

    Click on Attach User Policy and select Custom Policy.

    Policy Name can be something like S3FullBucketPermissions

    Policy Document should be:

    {
        "Version": "2012-10-17",
            "Statement": [{
                "Effect": "Allow",
                "Action": ["s3:*"],
                "Resource": ["arn:aws:s3:::", "arn:aws:s3:::/*"]
            }]
    }

    This grants full access to the user just on the bucket.

    Local Set-up

    Change your apache config file to match your project settings.

        sed 's/__//g' webfaction/apache.conf > webfaction/apache2.conf
        sed 's/00000//g' webfaction/apache2.conf > webfaction/httpd.conf
        rm webfaction/apache.conf
        rm webfaction/apache2.conf

    Move the local settings file to the right place, so that we know when django is running locally or on the server.

        mv /is_local.py /settings/local.py

    Database

    Enter our database credentials in our production settings file. Open the settings/production.py file and change the DATABASE entry with your database credentials to look like this:

        DATABASE = {
            'NAME': '',
            'USER': '',
            'PASSWORD': ''
        }

    Amazon S3

    Enter our Amazon S3 credentials in our production settings file. Open the settings/production.py file and change the AMAZONS3 entry to look like this:

        AMAZONS3 = {
            'ACCESS_KEY_ID': '',
            'SECRET_ACCESS_KEY': '',
            'BUCKET': ''
        }

    Django

    The ALLOWED_HOSTS setting defines on which hosts your application can run. Read more about it on Django docs.

        ALLOWED_HOSTS = [
            # ..com,
            '...webfactional.com'
        ]

    The EMAIL settings define how Django will handle e-mail sending.

        EMAIL_HOST = 'smtp.gmail.com' # Leave this as is if using gmail
        EMAIL_HOST_PASSWORD = ''
        EMAIL_HOST_USER = '' # eg. system@.com

    Git Repo

    Create a new local git repo for our project.

        git init
        git add  requirements webfaction .gitignore manage.py README.md requirements.txt wsgi.py
        git commit -am 'Initial commit'

    Add the remote git repo we created earlier on Webfaction. We'll call it production.

        git remote add production ssh://@.webfactional.com/~/webapps/git/repos/.git

    Test Drive

    Initialize the local database. You will be asked to create an admin user, go ahead and to that.

        python manage.py syncdb

    Start the development server.

        python manage.py runserver

    If all goes well you should be able to go to http://127.0.0.1:8000/ and see "Hello World!".

    Server Set-up

    SSH to your webfaction account.

        ssh @

    Install dependencies

    Skip this section if you already have these installed (eg. if you have created another project on the server previously).

    Make sure we work with Python 2.7. Add some aliases on .bash_profile to reference python2.7 instead of an older version and reload the file.

        echo "alias python=python2.7" >> ~/.bash_profile
        echo "alias pip=pip-2.7" >> ~/.bash_profile
        echo "alias easy_install=easy_install-2.7" >> ~/.bash_profile
        mkdir ~/lib/python2.7
        source ~/.bash_profile

    Install Pip.

        easy_install pip

    Install virtualenv & virtualenvwrapper.

        pip install virtualenv
        pip install --install-option="--user" virtualenvwrapper

    Set Environment Variables.

        echo "PYTHONPATH=~/lib/python2.7" >> ~/.bash_profile
        echo "export PYTHONPATH" >> ~/.bash_profile
        echo "export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7" >> ~/.bash_profile
        echo "export WORKON_HOME=~/.virtualenvs" >> ~/.bash_profile
        echo "export PROJECT_HOME=~/webapps" >> ~/.bash_profile
        echo "export PIP_VIRTUALENV_BASE=$WORKON_HOME" >> ~/.bash_profile
        echo "export PIP_RESPECT_VIRTUALENV=true" >> ~/.bash_profile
        echo "export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'"  >> ~/.bash_profile
        echo "if [ -f ~/bin/virtualenvwrapper.sh ]; then" >> ~/.bash_profile
        echo "    source ~/bin/virtualenvwrapper.sh" >> ~/.bash_profile
        echo "fi" >> ~/.bash_profile
        source ~/.bash_profile

    Virtual Environment

    Create a new virtual environment on the server.

        source ~/bin/virtualenvwrapper.sh
        mkvirtualenv env_
        workon env_

    Setup

    Create a new folder to place your project. It is adviced to keep your code under app as that folder is referenced in other places as well.

        mkdir ~/webapps//app

    Switch to your project's directory.

        cd ~/webapps//app/

    The myproject folder created by Webfaction will not be used so remove it.

        rm -r ~/webapps//myproject

    Then create a new git repo and set up a post-receive hook so that whenever you push to that repo the code is checked out under ~/webapps//app.

        GIT_REPO=$HOME'/webapps/git/repos/.git'
        git init --bare $GIT_REPO
        touch $GIT_REPO/hooks/post-receive
        echo "#!/bin/sh" >> $GIT_REPO/hooks/post-receive
        echo "echo 'Executing post-receive'" >> $GIT_REPO/hooks/post-receive
        echo "APP_DIR=$HOME/webapps/" >> $GIT_REPO/hooks/post-receive
        echo "GIT_WORK_TREE=\$APP_DIR/app git checkout -f" >> $GIT_REPO/hooks/post-receive
        echo "GIT_WORK_TREE=\$APP_DIR/app git reset --hard" >> $GIT_REPO/hooks/post-receive
        echo "rm \$APP_DIR/app//settings/development.py" >> $GIT_REPO/hooks/post-receive
        chmod +x $GIT_REPO/hooks/post-receive

    If this is the first git repo you are setting up then go ahead and delete the default one created by Webfaction.

        rm -r $HOME/webapps/git/repos/proj.git

    Push our code to our remote git repo. The post-commit hook will trigger and all our files will end up where they should.

        git push production master

    Move the apache configuration file to the appropriate place.

        mv ~/webapps//app/webfaction/httpd.conf ~/webapps//apache2/conf

    Install requirements.

        pip install -r requirements.txt

    Initialize the database.

        python manage.py syncdb

    You will be asked to create an admin user. Go ahead and do that.

    Compress our JS & CSS.

        python manage.py compress

    Push our local static files on AWS.

        python manage.py collectstatic --noinput

    Restart the web server.

        ~/webapps//apache2/bin/restart

    If it says that the web server is not running then start it.

        ~/webapps//apache2/bin/start

    If all went well ..webfactional.com should work.

    Maintenance

    Daily Usage

    virtualenv

    ALWAYS work from within a virtual environment.

        source /usr/local/bin/virtualenvwrapper.sh
        workon env_
        source ~/bin/virtualenvwrapper.sh
        workon env_
        cd ~/webapps//app

    Static files

    Compress our JS & CSS.

        python manage.py compress

    Push our local static files on AWS.

        python manage.py collectstatic --noinput

    Apache

        ~/webapps//apache2/bin/start
        ~/webapps//apache2/bin/restart
        ~/webapps//apache2/bin/stop

    Dependencies

    To update dependencies you need to first change the appropriate file under the requrements folder.

    Then you use pip to implement those changes.

        pip install -U -r requirements/development.txt
        pip install -U -r requirements.txt

    Staging Server

    Repeat steps ..