Django Blog Project #2: Django Webfaction deployment



This is my OLD blog. I've copied this post over to my NEW blog at:

http://www.saltycrane.com/blog/2008/06/first-django-webfaction-deployment/

You should be redirected in 2 seconds.



I mentioned in my previous Django post that I'd write about hosting my new Django blog site on Webfaction. As promised, here it is.

I had some problems deploying my first site at Webfaction. First I got an error about pysqlite. I tried using Postgresql, and then I got an error about something else when trying to do a python2.5 manage.py syncdb. I think that they didn't set me up with a current Django installation. Luckily the support was prompt and helpful. I ended up reinstalling the Django application from the Webfaction Control Panel. Here are the steps I took to deploy my less than fledgling site.


Sign up for a shared hosting plan at Webfaction

I signed up for Webfaction's lowest plan at $9.50/month with no commitment. Here is their pricing plans.


Play with new account

I got a welcome email within 24 hours with the details about my account. sofeng.webfactional.com was a domain name which they provided. Visiting this address in my browser showed my the It worked! Django new project page. I also ssh'd into my shell account by using ssh sofeng.webfactional.com. The OS is CentOS5 which, I think, is based on Red Hat. It was a little different than the Ubuntu that I'm used to. I copied some of my configuration files over to make me feel more at home. On my home machine:

$ scp .bashrc sofeng@sofeng.webfactional.com:
$ scp .screenrc sofeng@sofeng.webfactional.com:
$ scp .emacs sofeng@sofeng.webfactional.com:


Reinstall Django

Note, if your Webfaction account is set up properly with Django, you shouldn't need to take these steps. Like I mentioned earlier, I had some problems with my Django installation. So I ended up reinstalling Django. It's not as hard as it sounds-- just a few clicks in the Control Panel.

  1. In my browser, I went to the Webfaction Control Panel at https://panel.webfaction.com/.
  2. Under "> Domains / websites", I selected "Applications".
  3. I removed my current Django installation by clicking the icon with the minus sign.
  4. I clicked the icon with the plus sign to add a new application.
  5. I filled in the following information:
    "Name:": django
    "App type:": Django 0.96.2/mod_python 3.3.1/Python 2.5
    Then I clicked the "Create" button.
  6. Finally, I needed to specify which URL path I want to use with my Django application.
    Under "> Domains / websites", I selected "Websites".
  7. I selected the icon with the pencil to edit the website settings.
  8. In the "Site apps:" section, I selected the icon with the plus sign to add a new App and path. I selected "django" as my "App:" and entered "/" (without the quotes) in the "URL path" field. Then I clicked "Update".
  9. After a couple minutes, I was able to view the Django It worked! page at http://sofeng.webfactional.com.

Upload my project

  1. The welcome message recommends using sftp, but I used rsync instead because I'm more familiar with it:
    sofeng@home:~ $ rsync -avz ~/src/django/myblogsite sofeng@sofeng.webfactional.com:webapps/django

Deploy
  1. I ssh'd into my webfaction shell account:
    sofeng@home:~ $ ssh sofeng@sofeng.webfactional.com
  2. I set the PYTHONPATH. (I actually put this in my .bash_profile)
    sofeng@web36:~ $ export PYTHONPATH=$HOME/webapps/django
  3. sofeng@web36:~ $ cd ~/webapps/django/myblogsite
  4. I tried to do a python2.5 manage.py syncdb, but got a database error:
    Traceback (most recent call last):
      File "manage.py", line 11, in 
        execute_manager(settings)
      File "/home/sofeng/webapps/django/lib/python2.5/django/core/management.py", line 1672, in execute_manager
        execute_from_command_line(action_mapping, argv)
      File "/home/sofeng/webapps/django/lib/python2.5/django/core/management.py", line 1571, in execute_from_command_line
        action_mapping[action](int(options.verbosity), options.interactive)
      File "/home/sofeng/webapps/django/lib/python2.5/django/core/management.py", line 504, in syncdb
        cursor = connection.cursor()
      File "/home/sofeng/webapps/django/lib/python2.5/django/db/backends/sqlite3/base.py", line 58, in cursor
        self.connection = Database.connect(**kwargs)
    sqlite3.OperationalError: unable to open database file
  5. I needed to edit my settings.py file. I changed the following lines:
    DATABASE_NAME = '/home/sofeng/webapps/django/myblogsite/mydatabase.sqlite3'
    TEMPLATE_DIRS = (
        '/home/sofeng/webapps/django/myblogsite/templates',
    )
  6. I tried python2.5 manage.py syncdb again:
    $ python2.5 manage.py syncdb
    Loading 'initial_data' fixtures...
    No fixtures found.
    It worked.
  7. The next step was to set up the Apache configuration.
    $ cd ~/webapps/django/apache2/conf
  8. I edited the following line in httpd.conf:
        SetEnv DJANGO_SETTINGS_MODULE myblogsite.settings
  9. Then I restarted the Apache server:
    $ ~/webapps/django/apache2/bin/restart
  10. In my browser, I went to http://sofeng.webfactional.com/myview1, but got an error:
    ImportError at /myview1/
    No module named blog.models
    Request Method:  GET
    Request URL:  http://saltycrane.com/myview1/
    Exception Type:  ImportError
    Exception Value:  No module named blog.models
    Exception Location:  /home/sofeng/webapps/django/lib/python2.5/django/core/urlresolvers.py in _get_urlconf_module, line 177
  11. I guess I need my project on the Python path. I edited the following line in httpd.conf:
        PythonPath "['/home/sofeng/webapps/django/myblogsite', '/home/sofeng/webapps/django', '/home/sofeng/webapps/django/lib/python2.5'] + sys.path"
  12. Then I went back to http://sofeng.webfactional.com/myview1 and saw my page. Success!
  13. Note, If you want to see the live site, the real location is: http://saltycrane.com/myview1. It's not very exciting now, but hopefully I will be able to add some CSS and more blog metadata soon.

Install Mercurial

Update 6/7/2008: By default Webfaction doesn't come with Mercurial installed, but they do allow you to do a user installation. I decided it would be useful to have Mercurial so I could pull and merge changes with greater control. Here are my steps.This will do a user-wide (as opposed to system-wide) installation of Mercurial in ~/bin and ~/lib.

  1. sofeng@home:~/incoming $ wget http://www.selenic.com/mercurial/release/mercurial-1.0.1.tar.gz
  2. sofeng@home:~/incoming $ scp mercurial-1.0.1.tar.gz sofeng@sofeng.webfactional.com:
  3. sofeng@web36:~ $ mv mercurial-1.0.1.tar.gz ~/tmp
  4. $ cd tmp
  5. $ tar zxvf mercurial-1.0.1.tar.gz
  6. $ cd mercurial-1.0.1
  7. $ make install-home
  8. $ export PYTHONPATH=$HOME/lib/python:$PYTHONPATH
    Note: you will need to put this in your .bashrc instead of your .bash_profile because Mercurial only executes .bashrc on a remote hg push by default.
  9. $ hg version
    Mercurial Distributed SCM (version 1.0.1)
    
    Copyright (C) 2005-2008 Matt Mackall  and others
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Other Errors
  • No module name django.core.management
    $ python manage.py syncdb
    Traceback (most recent call last):
      File "manage.py", line 2, in ?
        from django.core.management import execute_manager
    ImportError: No module named django.core.management
    You need to set the PYTHONPATH:
    $ export PYTHONPATH=$HOME/webapps/django
    You may get this if you use python manage.py syncdb instead of python2.5 manage.py syncdb.

Related posts:
   Django Blog Project #1: Creating a basic blog
   Django Blog Project #3: Using CSS and Template Inheritance
   Django Blog Project #4: Adding post metadata
   Django Blog Project #5: YUI CSS and serving static media

No comments:

About

This is my *OLD* blog. I've copied all of my posts and comments over to my NEW blog at:

http://www.saltycrane.com/blog/.

Please go there for my updated posts. I will leave this blog up for a short time, but eventually plan to delete it. Thanks for reading.