Setup a slice with apache+wsgi+nginx
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.
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.