mod_wsgi

mod_wsgi is an Apache HTTP Server module for running Python software that’s compatible with the Web Server Gateway Interface (PEP 333). A mod_wsgi application as created with the control panel provides an Apache HTTP Server with the mod_wsgi module.

Reducing Application Start-Up Time

Some mod_wsgi-based sites’ first page load may take a long time as mod_wsgi imports and starts the Python application. To minimize loading delays, configure mod_wsgi to load the application on process start, instead of on first request. To configure mod_wsgi to load the application on process start:

  1. Open an SSH session to your account.

  2. Open $HOME/webapps/app/apache2/conf/httpd.conf, where app is the application name, in a text editor.

  3. On the line starting with WSGIScriptAlias, append process-group=<app> application-group=%{GLOBAL}, where <app> is the name of your application.

    For example, with a Django application named django_demo running a project named myproject, this line:

    WSGIScriptAlias / /home/demo/webapps/django_demo/myproject/myproject/wsgi.py
    

    becomes:

    WSGIScriptAlias / /home/demo/webapps/django_demo/myproject/myproject/wsgi.py process-group=django_demo application-group=%{GLOBAL}
    
  4. Save and close the file.

  5. Restart Apache. Enter $HOME/webapps/app/apache2/bin/restart and press Enter.

After restarting, subsequent first page load times are shorter.

Reducing mod_wsgi Memory Consumption

mod_wsgi applications can be tuned to consume more or less memory. These strategies may help reduce your application’s memory consumption, but note that some configuration changes may degrade overall performance. Experiment with different combinations of configuration values to suit your memory and performance needs.

  • Use the WSGIDaemonProcess directive’s processes setting conservatively: Start fewer processes by setting a small value for the WSGIDaemonProcess directive’s process setting.

    In ~/webapps/application/apache2/conf/httpd.conf, where application is the name of the mod_wsgi-based application, edit this line:

    WSGIDaemonProcess <app> processes=<num> python-path=/home/<user>/webapps/<app>:/home/<user>/webapps/<app>/lib/python2.7 threads=1
    

    where:

    • <app> is the name of the mod_wsgi-based application,
    • <num> is the number of allowed processes, and
    • <user> is your account name.

    For example, two to five processes will be enough for a Django site that uses a separate application to serve static media.

  • Use the WSGIDaemonProcess directive’s maximum-requests setting: If an application is not releasing memory in a way that’s beyond your control (for example, a library used by your application is leaking memory), then configure mod_wsgi to serve fewer requests before stopping and replacing a child process.

    In ~/webapps/application/apache2/conf/httpd.conf, where application is the name of the mod_wsgi-based application, edit this line:

    WSGIDaemonProcess <app> processes=5 python-path=/home/<user>/webapps/<app>:/home/<user>/webapps/<app>/lib/python2.7 threads=1 maximum-requests=<num>
    

    where:

    • <app> is the name of the mod_wsgi-based application,
    • <num> is the maximum number of requests, and
    • <user> is your account name.

    Reasonable values are typically between 100 and 1000 requests, though you may need to experiment to find an appropriate setting for your application.

Previous topic

Mercurial

Next topic

MongoDB

Search the documentation

Example: "configure email" or "create database"

Feedback

Send us your feedback on this documentation.