mod_wsgi

最後更新: 2021-09-27

目錄

 

  • mod_wsgi 介紹
  • mod_wsgi 設定
  • py Code
  • Sessions
  • Daemon mode
  • Doc

介紹

wsgi (Web Server Gateway Interface)

mod_wsgi (http://code.google.com/p/modwsgi/)

它與 CGI 有差不多的功能

mod_wsgi 支援常見的兩種 Apache Mode

* single threaded 'prefork'
* multithreaded 'worker'

Performance

written in C code directly against the internal Apache and Python

mod_wsgi 有兩個 mode 行 python

Embedded (similar way to mod_python)(technically perform better)
     within the context of the normal Apache child processes

Daemon ( FASTCGI/SCGI solutions ) (generally be the safest)
    distinct user ensuring that WSGI applications cannot interfere with each other

Python 標準

standard interface between web servers and Python web applications or frameworks:

http://www.python.org/dev/peps/pep-0333/

新的標準是  PEP 3333


Load module

 

/etc/httpd/conf.d/wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so

 


行 py script

 

設定檔: mywebsite.conf

WSGISocketPrefix /var/run/wsgi
WSGIDaemonProcess mailpanel user=mailpaneluser
WSGIProcessGroup mailpanel

AddType text/html .py

..................

方法1: File 內 SetHandler

<Files code.py>
    SetHandler wsgi-script
    Options ExecCGI FollowSymLinks
</Files>

方法2: Directory 內 SetHandler

<Directory /var/www/iredadmin/>
    Order allow,deny
    Allow from all
    SetHandler wsgi-script
    Options ExecCGI
</Directory>

方法3: WSGIScriptAlias

it marks the target directory as containing WSGI scripts, or marks the specific file-path as a script,

that should be processed by mod_wsgi's wsgi-script handler.

http://www.example.com/wsgi/scriptname => /home/web/wsgi/scriptname

WSGIScriptAlias  /wsgi/ /home/web/wsgi/

相當於

Alias /wsgi/ /home/web/wsgi/
<Location /wsgi-scripts>
  SetHandler wsgi-script
  Options +ExecCGI
</Location>

P.S.

WSGIScriptAlias /wsgi/ /usr/local/wsgi/scripts/

!! 不同於 !!

WSGIScriptAlias /wsgi/ /usr/local/wsgi/scripts

方式4: WSGIScriptAliasMatch

WSGIScriptAliasMatch ^/wsgi/([^/]+) /usr/local/wsgi/scripts/$1.wsgi

 


py Code

 

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

有問題時

# get an "ImportError: No module named web"

解決

import sys, os
abspath = os.path.dirname(__file__)
sys.path.append(abspath)
os.chdir(abspath)
import web

 

Accessible at 'http://x' instead of 'http://x/code.py/'

.htaccess

<IfModule mod_rewrite.c>      
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_URI} !^/icons
  RewriteCond %{REQUEST_URI} !^/favicon.ico$
  RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
  RewriteRule ^(.*)$ code.py/$1 [PT]
</IfModule>

* If the code.py is in the subfolder myapp/, adjust the RewriteBase to RewriteBase /myapp/

 


Webpy 的 Sessions

 

app = web.application(urls, globals())
curdir = os.path.dirname(__file__)
session = web.session.Session(app, web.session.DiskStore(os.path.join(curdir,'sessions')),)
application = app.wsgifunc()

 


Daemon mode

 

WSGISocketPrefix

Each application can be delegated to its own dedicated daemon process running just the WSGI application

WSGISocketPrefix /var/run/wsgi

# Defines the directory and name prefix to be used for the UNIX domain sockets used by mod_wsgi
# to communicate between the Apache child processes and the daemon processes.
# 會自動建立 wsgi.12547.0.1.sock

WSGIDaemonProcess

WSGIDaemonProcess process_group_name user=tim threads=15 maximum-requests=3600 inactivity-timeout=3600 home=/home/tim/web

# name:                        # name of the daemon process group must be unique for the whole server
                                         (not possible to use the same daemon process group name in different virtual hosts)

# user:                          # If this option is not supplied the daemon processes will be run as the same user that Apache would run child processes

# group=name

# processes:                 # If not defined then only one process will be run in this process group.

# threads:                     # default will be to create 15 threads in each daemon process within the process group

# "maximum-requests"  # default: 0 . number of requests a daemon process should process before it is shutdown and restarted

# "inactivity-timeout"      # number of seconds allowed to pass before the daemon process is shutdown and restarted
                                      # when the daemon process has entered an idle state.

# "home"                       # Defines an absolute path of a directory which should be used as the initial current working directory of the daemon processes

# python-path=directory  # List of colon separated directories to add to the Python module search path

# stack-size=nnn             # Linux: 8MB (quite excessive)
                                           The amount of virtual memory in bytes to be allocated for the stack corresponding to each thread
                                           created by mod_wsgi in a daemon process.
 

WSGIProcessGroup

WSGIProcessGroup process_group_name

# specify which process group a WSGI application will be executed in

# If WSGIProcessGroup is not used, the application will be run within the standard Apache child processes.

Testing

ps aux | grep httpd

tim      12757  7.6  4.0 228040 20332 ?        Sl   16:11   0:04 /usr/sbin/httpd

 


Auto Reload

 

WSGIScriptReloading

Enable/Disable detection of WSGI script file changes.

Default: On

 


Other Options

 

WSGIPythonHome

Description:     Absolute path to Python prefix/exec_prefix directories.

WSGIPythonPath

Description:     Additional directories to search for Python modules.

WSGIPythonOptimize

Description:     Enables basic Python optimisation features.

# 0 - The default => no optimisations are applied

# 1 - basic optimisations (pyc(bytecode) to .pyo)

# 2 - doc strings will not be generated and thus not retained

pyo

pyo = optimized code

Python interpreter is invoked with the -O flag to generate

When a script is run by giving its name on the command line,

the bytecode for the script is never written to a ‘.pyc’ or ‘.pyo’ file.

 


Doc

 

http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives