cheng029 发表于 2015-4-27 11:53:11

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.

[*]Follow above link2 to install apache/virtualenv, also install TG2RC1(currently), then get a sample app "myapp"
[*]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.
[*]Modify /etc/apache2/ports.conf to add muti-virtualhost refer to our "myapp"  

#/etc/apache2/ports.conf

NameVirtualHost *:8119
Listen 8119
NameVirtualHost *:8120
Listen 8120  
[*]Modify /etc/apache2/sites-available/myapp to follow above ports  

#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. 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. 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.

Order deny,allow
Allow from all

#7. If you want to use Virtualhost apache settings.
#Sample Virtual Host configuration would look like this:


    Order allow,deny
    Allow from all

NameVirtualHost *: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

NameVirtualHost *:8120

      WSGIDaemonProcess myapp2 threads=10 processes=3
      WSGIProcessGroup myapp2
      WSGIScriptAlias / /usr/local/turbogears/myapp/apache/myapp.wsgi
  
[*]Modify /etc/nginx/nginx.conf to serve static and proxy request to apache appsvr  

#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

#pid      logs/nginx.pid;

events {
    worker_connections1024;
}

http {
    include       mime.types;
    default_typeapplication/octet-stream;
    #log_formatmain'$remote_addr - $remote_user [$time_local] $request '
    #                  '"$status" $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_loglogs/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;
    #gzipon;
   
    upstream myapp {
      server 127.0.0.1:8119;
      server 127.0.0.1:8120;
      # other balance
    }
    server {
      listen       80;
      server_namelocalhost;
      # 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_loglogs/host.access.logmain;

      location / {
         # root   html;
         # indexindex.html index.htm;
         proxy_pass http://myapp;
      }
      #error_page404            /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;
      }

    }
}  
[*]Test the deployment  

$ /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.
页: [1]
查看完整版本: Nginx + Apache, mod_wsgi serve python web application (test with TG2)