how to use fastcgi and virtualenv?

Asked by Allan Bailey

I'm actually answering this question, since I cannot edit the wiki.

1. Setup your virtualenv directory. I'll use /var/admin/graphite as my graphite root dir, and /var/admin/graphite/PYVIRT for my virtualenv environment directory.

   $ mkdir -p /var/admin/graphite/PYVIRT
   $ virtualenv /var/admin/graphite/PYVIRT

2. activate the environment via: source /var/admin/graphite/PYVIRT/bin/activate
   # note, some shells, like zsh, require running 'rehash' so you can find the proper python and easy_install.

3. install needed packages, etc

    (PYVIRT)$ easy_install Django
    (PYVIRT)$ apt-get install python-cairo python-pyparsing memcached

4. install graphite using --root-dir=/var/admin/graphite, following the standard instructions.
    ( I used 0.9.4 for this example.)
    Note that you'll also need to use 'apt-get install python-mod-python' for the normal install script.

5. create a dispatch.fcgi script like this:

#!/var/admin/graphite/PYVIRT/bin/python

# setup the virtualenv
import os
os.environ.setdefault('PATH', '/bin:/usr/bin')
os.environ['PATH'] = '/var/admin/graphite/PYVIRT/bin:' + os.environ['PATH']
os.environ['VIRTUAL_ENV'] = '/var/admin/graphite/PYVIRT'
os.environ['PYTHON_EGG_CACHE'] = '/var/admin/graphite/PYVIRT/egg_cache'

os.chdir('/var/admin/graphite/webapp')
import sys

# Add a custom Python path.
sys.path.insert(0, "/var/admin/graphite/webapp/web")
sys.path.insert(0, "/var/admin/graphite/webapp")

# Switch to the directory of your project. (Optional.)
#os.chdir("/var/admin/graphite/webapp/web")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

6. create an apache virtual host config like this:

NameVirtualHost *:80

FastCgiServer /var/admin/graphite/dispatch.fcgi -processes 1 -listen-queue-depth 1000 -idle-timeout 3600

<VirtualHost *:80>
        ServerName graphite.foxmarks.com
        DocumentRoot "/var/admin/graphite/webapp"

         ScriptAliasMatch ^(.*)$ /var/admin/graphite/dispatch.fcgi$1

        <Location "/content">
                SetHandler None
        </Location>
        ErrorLog /var/admin/graphite/storage/log/webapp-error.log
        CustomLog /var/admin/graphite/storage/log/webapp-access.log common
</VirtualHost>

7. fix the carbon init script to source the PYVIRT/bin/activate script so that carbon finds proper libs.

that's it. let me know if you have questions or bugs.

-allan

Question information

Language:
English Edit question
Status:
Solved
For:
Graphite Edit question
Assignee:
No assignee Edit question
Solved by:
Allan Bailey
Solved:
Last query:
Last reply:
Revision history for this message
chrismd (chrismd) said :
#1

Thanks Allan, I will post this on the wiki when I get a chance. If you would like access to modify the wiki just go to the "Join This Wiki" link on the wiki's navigation menu.

Revision history for this message
Allan Bailey (zirpu) said :
#2

I did join the wiki. My username is zirpu.

thanks,
-allan

----- Original Message ----
From: chrismd <email address hidden>
To: <email address hidden>
Sent: Tuesday, March 3, 2009 12:57:59 PM
Subject: Re: [Question #62989]: how to use fastcgi and virtualenv?

Your question #62989 on Graphite changed:
https://answers.launchpad.net/graphite/+question/62989

    Status: Open => Answered

chrismd proposed the following answer:
Thanks Allan, I will post this on the wiki when I get a chance. If you
would like access to modify the wiki just go to the "Join This Wiki"
link on the wiki's navigation menu.

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/graphite/+question/62989/+confirm?answer_id=0

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/graphite/+question/62989

You received this question notification because you are a direct
subscriber of the question.

Revision history for this message
chrismd (chrismd) said :
#3

Sorry I simply did not see your request because I don't regularly check my wikidot account notifications. I would've thought that wikidot would email me when people request to join, apparently they don't. Now I know. You should have access now. Thanks.

Revision history for this message
Allan Bailey (zirpu) said :
#4

I'll post these instructions under the FAQ. Posted the initial draft from the above. I'll reformat for the wiki later.
thanks.
-allan

Revision history for this message
Allan Bailey (zirpu) said :
#5

I've created a branch (clone?) that is refactored to be a bit easier to run graphite under fastcgi and from an egg.

The current rev in my branch, 116, is just the 1st working draft. Not docs yet, however if anyone is interested I'll write up
a walk through of how to set it up.