Deploying Django app in Webfaction with virtual environment

Just like in development environment, it is equally beneficial to use virtual environments on production server, if you ever plan to deploy more than one app on the same machine. However, setting up a virtual environment in production server requires some additional configuration steps to get everything working. The exact workflow can be very different with different service providers.

This post describes my own workflow with a Webfaction server using their web control panel.

Create application

Although it will be a Django application, the selected type in Webfaction control panel should not be a Django app, but modwsgi. (This post is written using modwsgi 4.4.13/Python 3.4 with Django 1.8).

Create virtual environment

See my other post on setting up a Django project with virtual environment. Use the same steps to create the virtual environment on your server.

Configure Apache

Move the default httpd.conf to e.g. httpd.conf.org and check some values for the new httpd.conf template below.

ServerRoot "/home/<user_name>/webapps/<app_name>/apache2"

LoadModule authz_core_module modules/mod_authz_core.so  
LoadModule dir_module        modules/mod_dir.so  
LoadModule env_module        modules/mod_env.so  
LoadModule log_config_module modules/mod_log_config.so  
LoadModule mime_module       modules/mod_mime.so  
LoadModule rewrite_module    modules/mod_rewrite.so  
LoadModule setenvif_module   modules/mod_setenvif.so  
LoadModule wsgi_module       modules/mod_wsgi.so  
LoadModule unixd_module      modules/mod_unixd.so

Listen <port_number>  
KeepAlive Off  
SetEnvIf X-Forwarded-SSL on HTTPS=1  
ServerLimit 1  
StartServers 1  
MaxRequestWorkers 5  
MinSpareThreads 1  
MaxSpareThreads 3  
ThreadsPerChild 5

WSGIRestrictEmbedded On  
WSGILazyInitialization On

<VirtualHost *>
    ServerName <domain, e.g. www.app.com>
    ServerAlias <domain_alias(optional), e.g. app.com>

    # Logging configuration
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    CustomLog /home/<user_name>/logs/user/access_<app_name>.log combined
    ErrorLog /home/<user_name>/logs/user/error_<app_name>.log

    # Django WSGI settings
    WSGIDaemonProcess <app_name> processes=5 python-path=/home/<user_name>/webapps/<app_name>/<django_app>:/home/<user_name>/webapps/<app_name>/<app_env>/lib/python3.4/site-packages:/home/<user_name>/webapps/<app_name>/<app_env>/lib/python3.4 threads=1
    WSGIProcessGroup <app_name>
    WSGIScriptAlias / /home/<user_name>/webapps/<app_name>/<django_app>/<path_to_wsgi>/wsgi.py
</VirtualHost>

Webfaction might modify the default configuration in the future. The important part here is the <VirtualHost> tag. Fill in your values and use this httpd.conf file.

comments powered by Disqus