mengu on web programming.

Deploying TurboGears 2.1 Application With Nginx And uWSGI

I have developed a [TurboGears 2.1](http://www.turbogears.org) application and I was asked to deploy it today. I was planning to use Apache and mod_wsgi however on CentOS 5.6 both are painful. I was getting some errors that I really did not want to deal with so I have decided to use [nginx](http://www.nginx.org) and [uWSGI](http://projects.unbit.it/uwsgi/). Great combination I believe. The thing is I had to deploy this application to 3 web servers as we are using a load balancer so I have created a deployment script to use on all 3 web servers but surely I won't let you figure out how to deploy from this script. Let's cut the chase and get to the point. Installing Python 2.6, Setuptools And Virtualenv ---------- CentOS 5.6 is using Python 2.4 and as TurboGears will stop supporting it I wanted to install Python 2.6 from the beginning and in order to install Python 2.6 on CentOS we need to add an external repository. I did prefer EPEL. rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm yum install python26 python26-setuptools python26-virtualenv Creating Virtual Environment and Installing TurboGears 2.1 ---------- Installing TurboGears 2.1 is really easy. All we need is creating a virtual environment and doing an easy_install. cd /var/www/html virtualenv-2.6 --no-site-packages tg2env cd tg2env bin/easy_install -i http://www.turbogears.org/2.1/downloads/current/index tg.devtools That's it! We now have TurboGears 2.1 installed on our server. Installing UWSGI ---------- Now let's go to our home directory and install uwsgi and then nginx. cd /root wget http://projects.unbit.it/downloads/uwsgi-0.9.8.1.tar.gz tar xf uwsgi-0.9.8.1.tar.gz cd uwsgi-0.9.8.1 make -f Makefile.Py26 Installing Nginx ---------- We have uWSGI installed, now let's install nginx. cd /root wget http://nginx.org/download/nginx-1.0.4.tar.gz tar xf nginx-1.0.4.tar.gz cd nginx-1.0.4 ./configure make make install As I was deploying on CentOS 5.6 nginx did throw an exception telling me about it needed `pcre` library for its http rewrite module. If you ever get an error like that go ahead and install `pcre` and `pcre-devel`: yum install pcre pcre-devel Deploying Your Application To Production ---------- The next step is sending over your TurboGears application to production. If you are using a version control system, you can checkout your application. In my case I was using mercurial as I had a private repository on bitbucket.org. After you have set a production.ini file, go on and check out your application. If you are not using a version control system, you can zip your application and send it with `scp` to your production server. cd /var/www/html/tg2env hg/git clone your_repository_uri # install your application. bin/python setup.py develop # create database tables and bootstrap db. bin/paster setup-app production.ini Configuring Nginx ---------- We are very close to a full deployment. We only have two steps left. How should we configure nginx? My nginx configuration is based on the default configuration and the information I got from [Tony Landis' weblog](http://tonylandis.com/python/deployment-howt-pylons-nginx-and-uwsgi/). You can find your nginx.conf at `/usr/local/nginx/conf/nginx.conf`. cd /usr/local/nginx/conf # open nginx.conf and paste the following lines. worker_processes 4; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #keepalive_timeout 0; keepalive_timeout 65; server { listen 80; server_name www.yourdomain.com; root /var/www/html/tg2env/tgapp/tgapp/public; location ~*/(img|js|iepng|css)/ { root /var/www/html/tg2env/tgapp/tgapp/public; expires max; add_header Cache-Control "public"; break; } charset utf-8; location / { uwsgi_pass 0.0.0.0:3031; include uwsgi_params; uwsgi_param SCRIPT_NAME ""; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } I had this configuration in my mercurial repository already so all I did was copying the conf. :) Anyway, now there is only one step left. Starting uWSGI and nginx ---------- The final step, before stopping apache if it's still running, is to start uwsgi and nginx. Here's how I did it: /root/uwsgi-0.9.8.1/uwsgi --paste config:/var/www/html/tg2env/tgapp/production.ini --socket :3031 -H /var/www/html/tg2env \ --processes 8 --threads 4 -d /var/log/uwsgi.log /usr/local/nginx/sbin/nginx Ta da! Now you have your TG 2.1 application up and running. Congratulations! Special Thanks ---------- I'd like to tell thank you to TurboGears 2.1 team, nginx and uWSGI teams for the software and docs and Tony Landis for his notes on nginx and uWSGI.
Did you enjoy this post? You should follow me on twitter here.

Comments

roberto said on 15/07/2011 00:39 AM
warning, --processes and --workers do the same thing, probably you mean --processes 8 and --threads 4

Joost Molenaar said on 15/07/2011 02:53 AM
Nginx is in EPEL, I'm wondering if you wouldn't prefer that in case an update comes along. I'm sure you don't have time to check the nginx mailing list every day for security fixes and recompile nginx. Or does the EPEL version ship without wsgi support?

Mengu Kagan said on 15/07/2011 03:49 AM
@roberto, nope, i did not mean threads because i've read the docs wrong. :) thanks for the correction. @joost, i didn't know how to check if uwsgi is included in the nginx version which is in the epel repo so i just did go ahead and compiled it.

alind said on 24/06/2012 18:27 PM
@Mengu @joost nginx -V |grep uwsgi and you will know it.

Leave a Response

No HTML allowed. You can use markdown.
Name*:
E-Mail* (not published):
Web site:
Response: