shared hosting, mod_wsgi, multiple sites

I just finished setting up multiple sites on a shared host (webfaction), all using mod_wsgi and the same apache instance. The apache config file:
ServerRoot "/home/mehome/webapps/djangoprojects/apache2"
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule wsgi_module modules/mod_wsgi.so

KeepAlive Off
Listen 28512
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
Timeout 300
ServerLimit 1
StartServers 1
MaxRequestsPerChild 2000
NameVirtualhost *:28512

<virtualhost *:28512>
  ServerName site1.com
  ServerAlias www.site1.com
  WSGIDaemonProcess site1 threads=2 maximum-requests=2000 inactivity-timeout=120 display-name=wsgi:site1
  WSGIProcessGroup site1
  WSGIScriptAlias / /home/mehome/webapps/djangoprojects/site1.wsgi
  ErrorLog /home/mehome/webapps/djangoprojects/site1.log
</virtualhost>

<virtualhost *:28512>
  ServerName site2.com
  ServerAlias www.site2.com
  WSGIDaemonProcess site2 threads=2 maximum-requests=2000 inactivity-timeout=120 display-name=wsgi:site2
  WSGIProcessGroup site2
  WSGIScriptAlias / /home/mehome/webapps/djangoprojects/site2.wsgi
  ErrorLog /home/mehome/webapps/djangoprojects/site2.log
</virtualhost>
explanation:
  1. ServerLimit: limit to 1, since all the activity is on the wsgi deamon processes.
  2. MaxRequestsPerChild: if you serve your static content with something other than apache, this setting doesn't mean much.
  3. NameVirtualHost: the address:port to use for the virtual hosts.
  4. ServerName, ServerAlias: domain names to serve with a virtual host.
  5. WSGIProcessGroup: a group of processes that will serve only this domain, where the name is the same as that of the daemon.
  6. WSGIDaemonProcess: run the virtual host in daemon mode, use only 2 threads, reset the daemon after 2000 requests or inactivity-time of 120 seconds, and display the process with the assigned name.
  7. WSGIScriptAlias: enter the top level of the site '/' using the script mentioned.
  8. ErrorLog: this is obvious...
The trick here is to use the wsgi daemon mode with a group, so that all requests relating to the same virtual host will be served by the same processes. Also, notice the restarting of the daemon after certain amount of requests or inactivity time. This ensures that memory consumption stays low.

Comments

Popular posts from this blog

Plotting residuals in openFOAM