Skip to content

Instantly share code, notes, and snippets.

@scardine
Last active December 21, 2015 15:29
Show Gist options
  • Save scardine/6326978 to your computer and use it in GitHub Desktop.
Save scardine/6326978 to your computer and use it in GitHub Desktop.
Django + FCGI + Virtualenv on SUSE/RH. I use Ubuntu, but some customers have annoying policies about linux distros. Can't run Python 2.7 with mod_wsgi because the customer already has another python app using 2.6; my solution was resort to FCGI. Kind of like it, performance is not bad at all.
<VirtualHost *:80>
ServerAdmin admin@project_name.com
ServerName project_name.com
ServerAlias www.project_name.com
# DocumentRoot: not the django project root, just to be safe
DocumentRoot /var/app/project_name/fcgi
# Logs
ErrorLog /var/log/apache2/project_name-error_log
CustomLog /var/log/apache2/project_name-access_log combined
# don't loose time with IP address lookups
HostnameLookups Off
# needed for named virtual hosts
UseCanonicalName Off
# configures the footer on server-generated documents
ServerSignature On
# Media
Alias /robots.txt /var/app/project_name/static/robots.txt
Alias /static/ /var/app/project_name/static/
<Directory "/var/app/project_name/static">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Alias /media/ /var/app/project_name/media/
<Directory "/var/app/project_name/media">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
DefaultInitEnv DEBUG 1
DefaultInitEnv DB_HOST your_database_host
DefaultInitEnv DB_USER your_database_username
DefaultInitEnv DB_NAME your_database_name
DefaultInitEnv DB_DRIVER postgresql_psycopg2
DefaultInitEnv DB_PASS your_password
DefaultInitEnv PATH /usr/bin:/bin
<Directory /var/app/project_name/fcgi>
AddHandler fcgid-script .fcgi
Options FollowSymLinks +ExecCgi
Order deny,allow
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(project_name.fcgi)
RewriteRule ^(.*)$ project_name.fcgi/$1 [QSA,L]
</Directory>
</VirtualHost>
#!/var/lib/python-virtualenvs/project_name/bin/python
import sys, os
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/lib/python-virtualenvs/project_name/lib/python2.7/site-packages')
activate_env=os.path.expanduser("/var/lib/python-virtualenvs/project_name/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
# Add a custom Python path.
sys.path.insert(0, "/var/app/project_name")
# Switch to the directory of your project. (Optional.)
os.chdir("/var/app/project_name")
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false", environ=os.environ)
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
def application(environ, start_response):
for var in ('DEBUG', 'DB_DRIVER', 'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS', 'BROKER_URL', 'PHANTOM_URL',
'SECRET_KEY', 'SITE_ID'):
if var not in os.environ:
os.environ[var] = environ[var]
return _application(environ, start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment