Skip to content

Instantly share code, notes, and snippets.

@guilhermesouza
Created August 28, 2012 17:37
Show Gist options
  • Save guilhermesouza/3501233 to your computer and use it in GitHub Desktop.
Save guilhermesouza/3501233 to your computer and use it in GitHub Desktop.
Deploying Django on Heroku
#!/bin/bash
#Create all the database tables:
# heroku run bin/python ATP_Performance_Test/manage.py syncdb
#create_django_heroku_project.sh
# ---------------------------------------------------------------------->
# The "app name" that this will get in the Heroku control panel. Also
# determines directory names and your "PROJECT_NAME.herokuapp.com"
# default domain.
export PROJECT_NAME="my-test-app-guilherme-souza"
# The python module name for your Django site. Separate from above since
# python app names should use underscores rather than dashes.
export PYTHON_APP_NAME="my_test_app_guilherme-souza"
# Set up a heroku-$PROJECT_NAME virtualenv in the ~/Code directory.
cd ~/github
virtualenv --no-site-packages heroku-$PROJECT_NAME
# Modify the `activate` file with some sanity-ensuring defaults, like
# ignoring any system-level PYTHONPATH and DJANGO_SETTINGS_MODULE.
cd heroku-$PROJECT_NAME
echo "export PROJECT_NAME=\"$PROJECT_NAME\"" >> bin/activate
echo "export PYTHON_APP_NAME=\"$PYTHON_APP_NAME\"" >> bin/activate
echo "export PIP_RESPECT_VIRTUALENV=true" >> bin/activate
echo "export PYTHONPATH=\"\$VIRTUAL_ENV/repo/src\"" >> bin/activate
echo "unset DJANGO_SETTINGS_MODULE" >> bin/activate
# Activate the environment.
source bin/activate
# Initialize a git repository in the `repo` subdirectory of this virtualenv
git init repo
cd repo
# Start this git repo with my Python .gitignore of choice.
# See it at https://gist.github.com/1806643/ for notes.
echo "Baixando o .gitignore..."
sleep 5
curl -sLO https://raw.github.com/gist/1806643/.gitignore
git add .gitignore
git commit -m "initial commit, .gitignore"
# Create a `src` directory within our repo.
mkdir src
# Install Django (1.3.X), gunicorn (0.13.X), gevent (0.13.X), and the greenlet
# dependency.
echo "django==1.4" > requirements.txt
echo "gunicorn==0.14.6" >> requirements.txt
echo "gevent==0.13.7" >> requirements.txt
echo "greenlet==0.4.0" >> requirements.txt
pip install -r requirements.txt
# Enter the `src` dir and create a django project
cd $VIRTUAL_ENV/repo/src
$VIRTUAL_ENV/bin/django-admin.py startproject $PYTHON_APP_NAME
cd $VIRTUAL_ENV/repo
# Unlike the gunicorn defined in Heroku's Django example, we're going
# to use one of the async worker classes, "gevent". Using an async worker class
# is recommended when serving traffic directly to gunicorn (which is what
# happens under the Heroku Cedar stack).
echo "web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload src/$PYTHON_APP_NAME/settings.py" > Procfile
# Commit everything we have in here.
git add .
git commit -m "base django site"
# Test out our setup.
echo "Executando o foreman..."
sleep 5
foreman start
# Opening http://127.0.0.1:5000/ in the browser should display the
# standard "It Worked!" page. Now, let’s try to get this running in the cloud:
# Create a Heroku instance for this site
heroku create -s cedar $PROJECT_NAME
# Make sure to add `src` to the PYTHONPATH on our server. (We added this to our
# local activate file, but it needs to be applied to Heroku, too.)
heroku config:add PYTHONPATH=/src
# Deploy this project to Heroku
git push heroku master
echo ""
echo "Tarefa executada com sucesso!"
echo "http://$PROJECT_NAME.herokuapp.com/"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment