Skip to content

Instantly share code, notes, and snippets.

@simonv3
Last active September 15, 2015 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonv3/5e9e4b79b7280aaef84b to your computer and use it in GitHub Desktop.
Save simonv3/5e9e4b79b7280aaef84b to your computer and use it in GitHub Desktop.

For some reason it's always a struggle to get a flexible Django install running on Webfaction. This ended up working.

Create a mod_wsgi application - latest version of mod_wsgi and Python probably.

Go into the application and clone this repo:

git clone git@github.com:<gh_username>/<gh_project_name>.git <app_name>

Create a pyenv:

$ mkdir -p ~/.virtualenvs

In your .bash_profile, append this:

# Default Python
# http://docs.webfaction.com/software/python.html#creating-a-python-alias
alias python=python3.4

# Default pyvenv
alias pyvenv=pyvenv-3.4

# Default pip
alias pip=pip3.4

… and then reload your profile:

$ source ~/.bash_profile

create the pyenv:

$ pyvenv ~/.virtualenvs/<app_name>

in apache2/conf/http.conf:

ServerRoot "/home/<username>/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

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

DirectoryIndex index.py
DocumentRoot /home/<username>/webapps/ukloportal/htdocs

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

WSGIPythonPath /home/<username>/webapps/<app_name>:/home/<username>/webapps/<app_name>/<django_app>:/home/.virtualenvs/<virtualenv_name>/lib/python3.4
WSGIDaemonProcess <app_name> processes=2 threads=12 python-path=/home/<username>/webapps/<app_name>:/home/<username>/webapps/<app_name>/<django_app>:/home/.virtualenvs/<virtualenv_name>/lib/python3.4
WSGIProcessGroup <app_name>
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/<username>/webapps/<app_name>/<django_app>/<django_app>/wsgi.py

in <django_app>/<django_app>/wsgi.py:

"""
WSGI config for portal project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os
import sys
import site

lazy_path = lambda p1, p2: os.path.abspath(os.path.join(p1, p2))

# Dynamically resolve paths.
VIRTUALENV_PATH = '.virtualenvs'
SITE_PACKAGES_PATH = lazy_path(VIRTUALENV_PATH, 'uklo/lib/python3.4/site-packages')

WORKING_PATH = os.path.abspath(os.path.dirname(__file__))
ROOT_PATH = lazy_path(WORKING_PATH, '../')

site.addsitedir(SITE_PACKAGES_PATH)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portal.settings')

# check virtualenv site-packages FIRST!
sys.path.insert(0, SITE_PACKAGES_PATH)
sys.path.append(ROOT_PATH)
sys.path.append(lazy_path(ROOT_PATH, 'uklo'))

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Use logging.basicConfig(filename='log_file_name.log', level=logging.DEBUG) for debugging the wsgi.py file.

Then, copy over your local settings:

cp <app_name>/settings_local_sample.py <app_name>/settings_local.py

And edit them as necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment