Setup a slice with apache+wsgi+nginx
Thursday, 4th December, 2008 // 12:14 p.m.Well there are bunch of posts on setting up a slice with django. I just wanted to write one for myself so that I can setup a slice in future without wasting much time searching. When you get a brand new slice you need to setup SSH access into your slice.
I recommend to go with Ubuntu 8.10 Intrepid.
Follow this tutorial to get the SSH up. Dont follow the entire tutorial, just get the SSH up and set up a user account other than root. If possible change the port for SSH for security.
Lets install a bunch of stuff. (Mostly taken from Will Larson's article which is the best guide to setup a slice to setup django).
sudo apt-get install apache2 subversion php5 sudo apt-get install gcc libc6-dev build-essential python-dev python-setuptools sudo apt-get install python-egenix-mxdatetime memcached postfix sudo apt-get install libpcre3-dev nginx sudo apache2ctl restart
Ok we got the nginx installed let's setup nginx to proxy requests to apache. Shut down apache for now so that we can make nginx run on 80. Here is my nginx.conf for your reference. Make sure you change the 'server_name', 'proxy_pass', 'location /site_media/' to your own settings. Start nginx.
sudo /etc/init.d/nginx start
Coming to apache. Run apache on any custom port of your choice. Lets say we run apache on 8000. Here is my ports.conf for your reference. Create a directory as shown for access and error logs.
sudo mkdir /var/log/apache2/yourdomain.com sudo emacs /etc/apache2/sites-available/yourdomain.com
Here is my yourdomain.com config file for reference. Make sure you set email, path to wsgi file, path to error and access logs. Now off to the mod_wsgi installation. We need to install mod_wsgi from sources. So create a directory /src so that you can download mod_wsgi and install it.
cd ~/src wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz tar zxvf mod_wsgi-2.3.tar.gz cd mod_wsgi-2.3 ./configure make sudo make install
This install mod_wsgi. Create a file to load mod_wsgi.so. (extracted from here)
sudo -i echo "LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so" > /etc/apache2/mods-available/wsgi.load sudo a2enmod wsgi
Here is my project.wsgi for reference. Make sure you put this wsgi file a one directory level above the project.
/home/yashh/project.wsgi /home/yashh/project/settings.py
Thats how the directory listing should be. Also I wsgi does n't allow your python application to write into sysout. That means print statement in your python code will raise a IOError. To escape from this we need to set sys.stdout = sys.stderr in the wsgi file.
I use mysql as database. You can follow this tutorial to install mysql and setup user account for the project. Make sure you create a database run syncdb. After this make sure you create a mysql user account with sufficient privileges to the database.
I use git for version control. I prefer to install git from sources rather than apt-get.
cd ~/src wget http://kernel.org/pub/software/scm/git/git-1.6.0.tar.bz2 sudo apt-get build-dep git-core tar xjf git-1.6.0.tar.bz2 cd git-1.6.0/ ./configure make sudo make install
Follow this link to install memcache and pythonmemcached. I could n't install cmemcache which is a better option. If you have some success with cmemcache let me know. Start memcache with 32 MB of ram as we have limited resources on a 256MB slice.
memcached -u www-data -p 11211 -m 32 -d
Other than this installing django and setting up a project is quite easy and there are a bunch of tutorials on that. I wanted to share the wsgi setup.
Comment Form
5 comments:
001// Jiri Barton// Friday, 5th December, 2008, 2:06 a.m.
Psst, you can use
https://launchpad.net/~statik/+archive
for mod-wsgi (Interpid only). I have not tried but it may still be easier than compiling it from the source. Plus you get updates, support, and everything :)
002// Harro// Friday, 5th December, 2008, 2:45 a.m.
sudo apt-get install apache2 subversion php5Why install subversion when you use git?
Otherwise.. thanks.. I'm bookmarkign this for future reference.
003// Yashh// Friday, 5th December, 2008, 7:50 a.m.
@Jiri Barton I did n't know that. May be I'll try it next time. However installing from sources is quite easy. Thanks for the tip.
@Harro I install subversion because of the projects on the web use subversion. Ex: django. So I install subversion for a quick svn co. We could use git-svn but I have n't used it. Anyway it's your choice.
004// Dan Hitlon// Saturday, 3rd January, 2009, 2:53 a.m.
I had to install the development headers for apache to compile mod_wsgi from source. Just issue: sudo apt-get install apache2-prefork-dev
And that should sort out any errors relating to not finding 'apxs'
Thanks for the article!
005// daaku// Wednesday, 28th January, 2009, 7:15 p.m.
If you dont use PHP or any other event-mpm incompatible apache modules, you'll get a nice memory reduction with your apache processes by switching to event-mpm.