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.

Using a virtual environment with mod_wsgi

You can use a mod_wsgi application with a Python virtual environment (also known as a virtualenv) by modifying a configuration file.

To reconfigure a mod_wsgi application (versions 3.4 or later) to use a virtual environment:

  1. Open an SSH session to your account.

  2. Open ~/webapps/application/apache2/conf/httpd.conf in a text editor.

  3. Set the path to your virtual environment. In the WSGIDaemonProcess directive, replace python-path=/home/username/webapps/app/lib/python3.5 with python-home=venv, where venv is the path to your virtual environment.

    For example, if your mod_wsgi application is in /home/demo/webapps/mywsgiapp/ and your virtual environment is in /home/demo/webapps/mywsgiapp/venv/, replace python-path=/home/demo/webapps/mywsgiapp/lib/python3.5 with python-home=/home/demo/webapps/mywsgiapp/venv/.

  4. Optionally, set additional Python path directories, such as the path to your Python project. In the WSGIDaemonProcess directive, add python-path=path. You can add more than one python-path, if needed.

  5. Save and close the file.

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

The mod_wsgi now uses your virtual environment.