Introduction
About the mongrel2 manual
Installing mongrel2
Installing a mongrel2 WSGI handler
pip search mongrel2sudo pip install m2wsgi
Setting up mongrel2
Let’s set up mongrel2 in /var/www/mongrel2/. Feel free to change that path if you don’t like it. Note that the folder and all its content should be owned by your user, not root.
mkdir /var/www/mongrel2
cd /var/www/mongrel2
mkdir run logs tmp
Mongrel2 uses sqlite3 for configuration. Let’s use the m2sh tool to generate it from a text file. First create and open /var/www/mongrel2/mongrel2.conf with your favorite editor, and put this into it:
content = Dir(base=’/content/’,
index_file=”,
default_ctype=’text/plain’)
wsgi_handler = Handler(send_spec=’tcp://127.0.0.1:9999′,
send_ident=’ebe4ee7d-6a47-42dd-9acd-1707add81835′,
recv_spec=’tcp://127.0.0.1:9998′, recv_ident=”)
routes={
‘/’: wsgi_handler,
‘/content’: content
}
localhost = Host(name=”localhost”, routes=routes)
localip = Host(name=”127.0.0.1″, routes=routes)
main = Server(
uuid=”31bf6b07-a147-466c-87b5-961481b99201″,
access_log=”/logs/access.log”,
error_log=”/logs/error.log”,
chroot=”/var/www/mongrel2/”,
pid_file=”/run/mongrel2.pid”,
default_host=”localhost”,
name=”main”,
port=6767,
hosts=[localhost, localip]
)
settings = {“zeromq.threads”: 1}
servers = [main]
- The static content/ directory refers to /opt/graphite/webapp/content/. I’ll come back to that in a minute.
- The wsgi_handler entry will talk with m2wsgi over zeromq. We’ve specified the ports 9999 and 9998 but you can set these to anything you want.
- The server will only respond to requests with http Host header localhost and 127.0.0.1 for now. Add more hosts to the hosts array in Server if you want to be able to refer to the server with something other than localhost when you’re testing it.
cd /var/www/mongrel2m2sh load -config mongrel2.conf -db config.sqlitels -l config.sqlite-rw-r–r– 1 murray murray 45K Apr 14 17:54 config.sqlite
Start the graphite WSGI app
cd /opt/graphite/conf/cp graphite.wsgi{.example,}
import os, syssys.path.append(‘/opt/graphite/webapp’)os.environ['DJANGO_SETTINGS_MODULE'] = ‘graphite.settings’import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler()from m2wsgi.io.standard import WSGIHandler, Connectionconn = Connection(send_sock=”tcp://127.0.0.1:9999″,recv_sock=”tcp://127.0.0.1:9998″)handler = WSGIHandler(application, conn)handler.serve()
The static content folder
cp -a /opt/graphite/webapp/content/ /var/www/mongrel2/cd /opt/graphite/webappln -s /var/www/mongrel2/content/
Running
- Start the python wsgi application. It will just run without outputting anything for now (graphite might output some warnings if you haven’t set up all its config files). Note: my python 2 binary is called python2, you might have to use that instead, depending on your distro and python setup.
cd /opt/graphite/confpython graphite.wsgi
- Start mongrel2. It should output several lines about what it’s doing, with the last message being “All loaded up, time to turn into a server”, before daemonizing.
cd /var/www/mongrel2m2sh start -every -sudo
Testing
m2sh running -db config.sqlite -name mainmongrel2 at PID 26089 running.ps ax | grep mongrel226089 ? Ssl 0:00 mongrel2 config.sqlite 31bf6b07-a147-466c-87b5-961481b99201sudo netstat -lnp# I removed irrelevant lines here…tcp 0 0 127.0.0.1:9998 0.0.0.0:* LISTEN 26089/mongrel2tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 26089/mongrel2tcp 0 0 0.0.0.0:6767 0.0.0.0:* LISTEN 26089/mongrel2
Final thoughts
- You’re gonna want to check out m2wsgi and its different configuration options, and change /opt/graphite/conf/graphite.wsgi to suit your needs.
- Starting the wsgi application manually is probably not an option. Mongrel2 offers a user space process manager called procer which may be just what you need. Note that the manual also mentions a few alternatives) (scroll down to Note 5), and also offers some deployments tips which might be worth checking out.
- In the apache vhost example included with graphite (graphite-web-0.9.8/examples/example-graphite-vhost.conf) there’s a media folder pointed to some media folder in Django and a reference to “The Django admin site media”. I haven’t really used that myself so I don’t know what it is, but to make that work you would probably have to do something similar to what I did with the /opt/graphite/webapp/content/ directory above.
2 Responses to Deploying graphite on mongrel2
Leave a Reply Cancel reply
About Me
Hi, my name is Spike Morelli and this is my thinking lab. Over the past 13 years of career in the tech industry I've been a developer, a system engineer, a devops person, a manager and a startup owner. I've taken the best from each experience and brought it into the next, innovating and focusing on delivering value. I have a passion for sociology and communication, but above all I care about making people happy, it's incredibly rewarding and happy folks do the best work.
Most of us wouldn't have done what we have done if we didn't have people around us to learn from, their experiences is what helped us grow, their passion our fuel. If that's also your experience let's make that circle bigger, reach out to me at fsm@spikelab.org or on twitter





Hello, nice post!
I’m the author of a mongrel2 WSGI handler also. I’ts named wsgid (http://wsgid.com).
Among other features, wsgid can load django apps out of the box, can manage your worker processes (including re-spawning dead workers) and can be told to load at boot time.
Take a look.
Thanks.
Cool stuff, will definitely check it out, thank you.