Migrating to Heroku

To cut some personal costs and simply because I was interested in it, I recently moved this blog to Heroku, the popular cloud hosting platform.

Since 2011 Heroku officially supports Python deployments. Running on a single dyno it is even for free. Following the official guide to deploy a Django application, the transition from my VPS to Heroku was pretty straight-forward and easy. Additionally Heroku enforces good practices, like using virtualenv, and supports more and more popular technologies, like the Python WSGI server Gunicorn.

Following a short general guide of what had to be done from my side to do the transition:

  • Create a fixture file from your database via the dumpdata command.
  • Since I didn't want to add any billing information to Heroku yet, I had to rewrite everything related to sending emails from my web server, to save the incoming contact request in the database instead. That was pretty much just the creation of a new model and a one-liner to save it to the DB instead of sending an email,
  • Already using virtualenv, the rest was simply adding or changing requirements. In addition of removing the MySQL bindings for python I added the following libaries:

    dj-database-url==0.2.1
    django-heroku-memcacheify==0.4
    django-pylibmc-sasl==0.2.4
    gevent==0.13.8
    greenlet==0.4.0
    gunicorn==0.17.2
    psycopg2==2.4.6
    pylibmc==1.2.3
    

  • Static files are served via Django directly for the time being, since a Web-Dyno using Python can not serve them the normal way. I will move them to be served by a solution like Amazon S3 in the future. Therefore I added the following to my urlconf:

    urlpatterns += patterns(
        '',
        url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )
    

  • Running the dyno via Gunicorn with gevent support was a single line in the Procfile:

    web: gunicorn -k gevent wsgi:application

  • Finally:

    git push heroku master

So far I am quite happy with Heroku. I didn't recognize any differences to previous VPS hosting and I don't have to deal with server maintenance anymore, even it was an experience I don't want to miss. Goodbye VPS, welcome Heroku!

Write a comment

You can use reStructeredText syntax to write a comment.

captcha
1 year, 7 months ago (2013-02-18)
 by Johnny

You could also use the SendGrid addon (https://addons.heroku.com/sendgrid) for sending emails, it starts at 6k emails per month for free. :-)

1 year, 6 months ago (2013-02-24)
 by Torsten

Hi Johnny,

thanks for the feedback. I am aware of the SendGrid addon, but didn't want to use it yet. I simply didn't want to add any billing information to Heroku yet and just port my blog there. Most likely I will use it later though.

1 year, 6 months ago (2013-03-13)
 by Andrey

It's cool. So you on Heroku now, glad to hear -)

Right now, I'm looking at Octopress (http://octopress.org/) with Github Pages (http://pages.github.com/) for my purpose.

I think It's really good solution to write in blog, when you are a programmer. Especially, when you not leaving terminal and you like Markdown.

PS CAPTCHA quite annoying

1 year, 6 months ago (2013-03-16)
 by Torsten

Sounds great.

Btw. tried to do it without captcha before but got some spam. I was thinking about third party app for comments like disqus but dont't want to change for now.

1 year, 5 months ago (2013-04-12)
 by Mia

Hey,

I'm trying to setup my application your way. But I have a problem with Heroku reading my settings.py file, I keep getting ImportError. Could you post your wsgi.py and settings.py files?

1 year, 5 months ago (2013-04-15)
 by Torsten

Can you tell me what the exactly the ImportError is about (can't import settings?).

In my wsgi.py I do os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings").

My settings.py file is in the root of my django project.

Make sure you always use from django.conf import settings, instead of import settings. Except this its a pretty standard settings.py file I think.

3 months, 2 weeks ago (2014-06-06)
 by Sarah

Well, - unless we start to think for ourselves, provide for ourselves and be accountable - not a single thing is going to improve. My ex-husband was like that - inactive, childish and demanding, now he is sitting on reddit all-day upvoting puns about jerking off. If you don't strive, you lose it. Excellent post. xOxOx Sarah- http://phytoceramidesreviewstv.com/ Sarah http://phytoceramidesreviewstv.com/