Nginx + Apache, mod_wsgi serve python web application ,test with TG2

Ref:

http://wiki.jmoiron.net/MigratingFromApacheToNginx

http://turbogears.org/2.0/docs/main/Deployment/modwsgi+virtualenv.html

http://wiki.nginx.org/NginxConfiguration

Req:

Debian os, python2.5, apache2, libapache2-mod-wsgi

Nginx is designed to do best to serve static files and proxying request to it's actual processor, so our deployment will use nginx as a front end, actually act as a web server, also do some load balance job; apache act as a application server role in our deployment, we don't use the mod_wsgi(atually apache-mod_wsgi,not that nginx's patch) in a embeded mode, which code executed in the apache's worker process and cause apache do much work that it shouldn't, daemon mode is like FastCGI and apache proxying task to other worker process, like wsgi handers.

  1. Follow above link2 to install apache/virtualenv, also install TG2RC1(currently), then get a sample app "myapp"
  2. Follow above link2 to do a apache+mod_wsgi deployment for the "myapp", best to deploy to /usr/local/turbogears/ for convenient, make sure it's run ok.
  3. Modify /etc/apache2/ports.conf to add muti-virtualhost refer to our "myapp"
  4. #/etc/apache2/ports.conf

    NameVirtualHost *:8119

    Listen 8119

    NameVirtualHost *:8120

    Listen 8120

  5. Modify /etc/apache2/sites-available/myapp to follow above ports
  6. #Apache configuration File

    #Read README.txt

    #1. This file should be added to your apache config folder; On Debian copy to /etc/apache2/sites-available/

    #Default location for tg2 project is /usr/local/turbogears/myapp. That is where you should put your project. This folder should be outside of apache location. Your package should not be installed into python site-package.

    #2. Alias/Allow apache to serve static content.

    #Alias /myapp/images /usr/local/turbogears/myapp/myapp/public/images

    #Alias /myapp/css /usr/local/turbogears/myapp/myapp/public/css

    #Alias /myapp/javascript /usr/local/turbogears/myapp/myapp/public/javascript

    #Embeded mode (http://example.com/myapp).For big websites with a lot of memory and visitors.

    #WSGIScriptAlias /myapp /usr/local/turbogears/myapp/apache/myapp.wsgi

    #3. Test if modwsgi is working. Uncomment below line, and go to http://localhost/test:

    #WSGIScriptAlias /test /usr/local/turbogears/myapp/apache/test.wsgi

    #4. [Optional] Sitewide (http://example.com/)

    #Alias /images /usr/local/turbogears/myapp/myapp/public/images

    #Alias /css /usr/local/turbogears/myapp/myapp/public/css

    #Alias /javascript /usr/local/turbogears/myapp/myapp/public/javascript

    #WSGIScriptAlias / /usr/local/turbogears/apache/myapp.wsgi

    #5. [Optional] Deamon mode with 10 threads and 3 processes. For small to medium website.

    #WSGIDaemonProcess myapp threads=10 processes=3

    #WSGIProcessGroup myapp

    #WSGIScriptAlias / /usr/local/turbogears/myapp/apache/myapp.wsgi

    #6. Directory Permission.

    <Directory /usr/local/turbogears/myapp/apache>

    Order deny,allow

    Allow from all

    </Directory>

    #7. [Optional] If you want to use Virtualhost apache settings.

    #Sample Virtual Host configuration would look like this:

    <Directory /usr/local/turbogears/myapp/apache>

    Order allow,deny

    Allow from all

    </Directory>

    NameVirtualHost *:8119

    <VirtualHost *:8119>

    #WSGIScriptAlias / /usr/local/turbogears/myapp/apache/myapp.wsgi

    WSGIDaemonProcess myapp1 threads=10 processes=3

    WSGIProcessGroup myapp1

    WSGIScriptAlias / /usr/local/turbogears/myapp/apache/myapp.wsgi

    </VirtualHost>

    NameVirtualHost *:8120

    <VirtualHost *:8120>

    WSGIDaemonProcess myapp2 threads=10 processes=3

    WSGIProcessGroup myapp2

    WSGIScriptAlias / /usr/local/turbogears/myapp/apache/myapp.wsgi

    </VirtualHost>

  7. Modify /etc/nginx/nginx.conf to serve static and proxy request to apache appsvr
  8. #user nobody;

    worker_processes 1;

    #error_log logs/error.log;

    #error_log logs/error.log notice;

    #error_log logs/error.log info;

    #pid logs/nginx.pid;

    events {

    worker_connections 1024;

    }

    http {

    include mime.types;

    default_type application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] $request '

    # '"$status" $body_bytes_sent "$http_referer" '

    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile on;

    #tcp_nopush on;

    #keepalive_timeout 0;

    keepalive_timeout 65;

    #gzip on;

    upstream myapp {

    server 127.0.0.1:8119;

    server 127.0.0.1:8120;

    # other balance

    }

    server {

    listen 80;

    server_name localhost;

    # site_media - folder in uri for static files

    location ~ ^/\/(images|javascript|js|css|flash|media|static)/ {

    root /usr/local/turbogears/myapp/public;

    access_log off;

    expires 30d;

    }

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {

    # root html;

    # index index.html index.htm;

    proxy_pass http://myapp;

    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

    #

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

    root html;

    }

    }

    }

  9. Test the deployment
  10. $ /etc/init.d/apache2 restart

    $ nginx -t -c /etc/nginx/nginx.conf

    $ nginx -c /etc/nginx/nginx.conf &

    #open browser to browse the http://your_debian_host/ check your deploment

    this is very simple config, for the production enviroment, you should check the more doc to get the best suit for you.

We get the perfectly 3-tiers infrastructure for our n-tiers-oriented application, but it compares to the nginx+nginx-mod_wsgi style may has little speed limitation, no benchmark data now, maybe someone can do a test to get the actual performance data.