Migrating from mod_python to mod_wsgi

mod_python was an Apache module which allowed Python scripts to be executed by the server. As of June 16, 2010, the Apache Software Foundation has retired mod_python. As a result, no more official bug fix or feature releases are expected for the module. mod_wsgi is now the preferred Apache module to host many Python applications, including Django. WebFaction advises users of mod_python to switch to mod_wsgi for new and existing Django applications.

This guide will walk you through all the steps to migrate a mod_python Django application to a mod_wsgi Django application, including identifying applications which need migration.

Identifying mod_python Django Applications

Django applications which use mod_python are recognizable in the WebFaction control panel by their App type. To find mod_python applications:

  1. Log in to the WebFaction control panel.
  2. Click Domains / websites ‣ Applications. The list of applications appears.
  3. Review each application in the list. Django applications using mod_python will be of the form Django (X.Y.Z)/mod_python 3.3.1/Python (A.B) - DISCONTINUED, where X.Y.Z and A.B are Django and Python version numbers respectively.

Completing the Migration

Once you’ve identified an application to migrate, follow these steps:

  1. Create a mod_wsgi Django application.

    1. Log in to the control panel.
    2. Click Domains / websites ‣ Applications. The list of applications appears.
    3. Click the Add new application button. The Create a new application form appears.
    4. In the Name field, enter a name for the Django application.
    5. In the App category menu, click to select Django.
    6. In the App type menu, click to select a mod_wsgi-based Django application.
    7. Click the Add application button. The application is installed and added to the list of applications.
  2. Reconfigure the mod_python Django application to use mod_wsgi.

    1. Open an SSH session to your account.

    2. In your mod_python application directory, create a new file, myproject.wsgi, containing:

      import os
      import sys
      
      sys.path = ['/home/{username}/webapps/{py}', '/home/{username}/webapps/{py}/lib/python{X.Y}'] + sys.path
      from django.core.handlers.wsgi import WSGIHandler
      
      os.environ['DJANGO_SETTINGS_MODULE'] = '{myproject}.settings'
      application = WSGIHandler()
      

      where:

      • {username} is your WebFaction account name,
      • {py} is the name of the mod_python application,
      • {X.Y} is the version number of Python your application is using, and
      • {myproject} is the name of your Django project (myproject, by default).
    3. Save and close the file.

    4. Copy mod_wsgi.so to the mod_python application. Enter cp $HOME/webapps/wsgi/apache2/modules/mod_wsgi.so $HOME/webapps/py/apache2/modules/, where wsgi is the name of the mod_wsgi application and py is the name of the mod_python application, and press Enter.

    5. Open $HOME/webapps/py/apache2/conf/httpd.conf in a text editor.

    6. Modify your httpd.conf file to use mod_wsgi.

      1. Replace LoadModule python_module modules/mod_python.so with LoadModule wsgi_module modules/mod_wsgi.so.

      2. Replace:

        <Location "/">
            PythonHandler django.core.handlers.modpython
            PythonPath "['/home/{username}/webapps/{py}', '/home/{username}/webapps/{py}/lib/python{X.Y}'] + sys.path"
            SetEnv DJANGO_SETTINGS_MODULE {myproject}.settings
            SetHandler python-program
        </Location>
        

        with:

        WSGIScriptAlias / /home/{username}/webapps/{py}/{myproject}.wsgi
        

        where:

        • {username} is your WebFaction account name,
        • {py} is the name of the mod_python application,
        • {X.Y} is the version number of Python your application is using, and
        • {myproject} is the name of your Django project (myproject, by default).
    7. Save and close the file.

    8. Restart the Django application.

  3. Visit your Django site. It should be running normally.

  4. Remove the mod_wsgi Django application.

    1. Log in to the WebFaction control panel.
    2. Click Domains / websites ‣ Applications. The list of applications appears.
    3. Click the mod_wsgi application to remove. The application’s details appear.
    4. Click the Delete button. A confirmation prompt, Are you sure you wish to delete the application, appears appears.
    5. If the correct application is listed, click the Yes, I’m sure button.

Your Django application is configured to use mod_wsgi instead of mod_python.

Previous topic

Troubleshooting

Next topic

Drupal

Search the documentation

Example: "configure email" or "create database"

Feedback

Send us your feedback on this documentation.