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:
- Log in to the WebFaction control panel.
- Click Domains / websites ‣ Applications. The list of applications appears.
- 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:
Create a mod_wsgi Django application.
- Log in to the control panel.
- Click Domains / websites ‣ Applications. The list of applications appears.
- Click the Add new application button. The Create a new application form appears.
- In the Name field, enter a name for the Django application.
- In the App category menu, click to select Django.
- In the App type menu, click to select a mod_wsgi-based Django application.
- Click the Add application button. The application is installed and added to the list of applications.
Reconfigure the mod_python Django application to use mod_wsgi.
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).
Save and close the file.
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.
Open $HOME/webapps/py/apache2/conf/httpd.conf in a text editor.
Modify your httpd.conf file to use mod_wsgi.
Replace LoadModule python_module modules/mod_python.so with LoadModule wsgi_module modules/mod_wsgi.so.
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).
Save and close the file.
Visit your Django site. It should be running normally.
Remove the mod_wsgi Django application.
- Log in to the WebFaction control panel.
- Click Domains / websites ‣ Applications. The list of applications appears.
- Click the mod_wsgi application to remove. The application’s details appear.
- Click the Delete button. A confirmation prompt, Are you sure you wish to delete the application, appears appears.
- 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.
