Deploying Django Applications to Heroku
These instructions are modified versions of the ones found here I’ve modified them to fit my particular setup, so YMMV.
Prerequisites
- Basic Python knowledge, including installed versions of Python, Virtualenv, and Pip.
- Your application must run on Python v2.7.
- Your application must use Pip to manage dependencies.
- A Heroku user account. Signup here (it’s free).
- An installed version of PostgreSQL.
- An installation of django
- An installation of ruby (the Heroku tools require this).
- An installation of GCC - to create the necessary packages for the virtualenv.
- A copy of git.
On a Fedora machine creating this setup is as simple as:
# yum install python python-virtualenv postgresql postgresql-libs \
python-psycgopg libpqxx libpqxx-devel Django python-pip ruby \
gcc git
Local Workstation Setup
Firstly, set up your local machine with the appropriate heroku toolkit. Download this tarball, unpack it, and add the contents to your $PATH environment variable.
Once done you will be able to use the heroku command from your command shell. Log in using the email address and password you used when creating your Heroku account:
# heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press enter at the prompt to upload your existing ssh key or create a new one - you’ll need this for pushing code later to the heroku platform later on.
Start a Django App Inside a Virtualenv
Create a new django application directory
# mkdir new_app && cd new_app
Create and activate a new virtual environment (I am using v1.3.3)
# virtualenv venv --distribute
# source venv/bin/activate
Install the appropriate dependancies into the new environment:
# pip install Django psycopg2
Create a new django application
# django-admin.py startproject helloworld
Freeze the requirements using pip:
# pip freeze > requirements.txt
Commit to Git
Exclude Virtualenv artifacts from source control tracking:
GitHub provides an excellent Python gitignore file that can be installed system-wide.
You will use Git to deploy code to Heroku. You don’t have to use it as your main source control system; Git can coexist with Mercurial, Subversion, or other revision control systems. Initialize a Git repo and commit the project (if you aren’t already tracking your project with Git):
# git init
Initialized empty Git repository in /Users/adam/hellodjango/.git/
# git add .
# git commit -m ""
Deploy to Heroku
Create an app on the Cedar stack:
$ heroku create --stack cedar
Creating afternoon-sword-29... done, stack is cedar
http://afternoon-sword-29.herokuapp.com/ | git@heroku.com:afternoon-sword-29.git
Git remote heroku added
Deploy your app:
$ git push heroku master
Pretty much that’s it. Simply surf to http://app.herokuapp.com and you should see your “Hello world” app page running!
Other Useful Commands
View the running status | heroku ps |
View the logs | heroku logs |
Visit the site | heroku open |
Sync the database | heroku run python app/manage.py syncdb |
Tweet |
|