Recently I deployed a django application to WebFaction (this blog!). While this wasn't the first app I've deployed there, I did forget a few steps along the way which required a bit of research and experimentation on my part. To avoid this in the future, I've documented the steps I took to deploy the app here.
I'm going to assume that you have a django app working for you locally. If you don't have one, feel free to use this sample project available for download in bitbucket.
Webfaction Control Panel
After logging into the WebFaction control panel, find the Domains/websites link. Clicking on it opens up another list underneath. Click Applications. Create a new application by clicking the small icon on the bottom right of the screen.
Name your application. The name is going to be the app name on the sever, so make sure that it's unix compliant. My preference is to replace spaces/dashes with underscores and lowercase everything. So "Example Blog" becomes "example_blog". The page form will validate this so that no errors are made in naming convention.
Next, you'll choose the type of application you'd like to deploy. Choose the App category as Django. Next choose the App type. There are quite a few options available, some with differing versions of python. Some are listed as as insecure and shouldn't be used (unless you have a really good reason and know what you are doing). My preference is to use the latest and greatest django/python versions for new applications.
Once you hit create, WebFaction will create the environment on their servers so that you can deploy your application. This includes python, django, and apache pre-configured to work out of the box.
- Do this by cloning your project into a location on the WebFaction server. WebFactions default is to place the project under the ~/webapps/
. I find this long path cumbersome and usually just use my home directory. One thing I recommend is creating a symlink linking to the project. Use this symlink for any path references. This way if you want to iterate versions of code by checking out a specific tags, you need only to change the symlink.
cd ~ hg clone ssh://email@example.com/jcosta71/example-webfaction-deploy example_project ln -s example_project example
- Apache and mod_wsgi configuration. WebFaction creates a default apache2 configuration file and project wsgi file. For the sake of this example, I've pulled them into the example project. All that should be required is to correct the pathing in these two files, create symlinks to the appropriate locations, and restart apache.
cd ~/webapps/example_application ln -s ~/example/apache/conf/example.wsgi example.wsgi cd ~/webapps/example_application/apache2/conf mv httpd.conf httpd.conf.bak ln -s ~/example/apache/conf/httpd.conf httpd.conf
Because of my preference to checkout the applications into the root. I’ve tweeked the wsgi and httpd.conf files slightly. The changes include:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
WSGIPythonPath /home/johncosta/webapps/example_application:/home/johncosta/webapps/example_application/lib/python2.6 WSGIScriptAlias / /home/johncosta/webapps/example_application/myproject.wsgi
WGIPythonPath /home/johncosta/example/sample_project/:/home/johncosta/webapps/example_application/lib/python2.6 WSGIScriptAlias / /home/johncosta/webapps/example_application/example.wsgi
The next step is to generate and map the static files to your application. Django has added in 1.3 static directory support. See the documentation here. To map your static files to the application you'll first need to publish them in a known location.
- Set the STATIC_ROOT value in your local_settings.py file to the location you want all the static files collected to.
- Change your directory to the location of your manage.py file.
- Run the collect static management command. You may have to add django to your python path.
cd ~/example_project/sample_project</li> PYTHONPATH=/home/johncosta/example/sample_project/:/home/johncosta/webapps/example_application/lib/python2.6/:$PYTHONPATH python2.6 manage.py collectstatic
Create a new application for your static content by going back into the WebFaction Control Panel. Name your new application and select symbolic link for a category. You'll use the default symbolic link to static/cgi/php app type. In the extra info, enter the absolute path to the path you set your STATIC_ROOT.
This last step maps your application and static content to a domain. Log back into WebFaction control panel, choose Websites under Domains/websites. I've already created a domain in the control panel that I'd like to map my application to. Webfaction has a how-to guide on creating domains here.
I've chosen to name the example_projects as example. Choose the subdomain for your application, and map your site app (we call this example_application) to "/". Map the static app (this was named example_application_static) to "/static". This value should be the same value as STATIC_URL found in your local_settings.py file.
- You should now be able to access your domain! Here's my example. http://example.johncosta.webfactional.com