杰瑞科技汇

Mod Python与Python2.7如何兼容部署?

Of course. Let's break down mod_python and its use with Python 2.7.

Mod Python与Python2.7如何兼容部署?-图1
(图片来源网络,侵删)

This is a topic rooted in the history of web development for Python. Understanding it is key to appreciating the modern ecosystem.


What is mod_python?

mod_python was an Apache module that embedded the Python interpreter directly into the Apache web server. This was a significant performance optimization at the time compared to older methods like CGI.

How it worked:

  • Instead of starting a new Python process for every single web request (like CGI), mod_python would load the Python interpreter into the Apache worker processes when they started.
  • When a request came in for a Python script, Apache would pass it directly to the embedded interpreter within its own process.
  • This avoided the massive overhead of process creation and interpreter startup for each request.

Key Features (at the time):

Mod Python与Python2.7如何兼容部署?-图2
(图片来源网络,侵删)
  • Performance: Much faster than CGI.
  • Stateful Applications: You could keep application state (like database connections) in memory across requests, which was a major advantage.
  • Access to Apache Internals: It gave Python code direct access to many of Apache's internal functions and configuration details.

The Python 2.7 Context

mod_python was in its prime during the early to mid-2000s, which was also the peak era of Python 2.7 (released in 2010, but representing the mature Python 2.x line).

  • Dominance: At that time, Python 2.7 was the stable, production-ready version of Python. Most web frameworks (Django 1.x, Flask 0.x, etc.) were built for it.
  • Default Pairing: If you were using mod_python, you were almost certainly using it with Python 2.7. The module was compatible with Python 2.4 through 2.7.

Installation on an older system (e.g., CentOS 6): You would typically install it using the system's package manager:

sudo yum install mod_python27  # or similar

Or compile it from source against your Python 2.7 installation.


The mod_python Configuration (httpd.conf)

To use mod_python, you had to add specific directives to your Apache httpd.conf file. Here’s a typical example:

Mod Python与Python2.7如何兼容部署?-图3
(图片来源网络,侵删)
# 1. Load the module
LoadModule python_module modules/mod_python.so
# 2. Tell Apache to handle .py files with mod_python
#    The "handler" is the Python function that will process the request.
AddHandler mod_python .py
# 3. Configure a specific directory or virtual host
<Directory /var/www/myapp>
    # Set the Python handler for this directory
    PythonHandler myapp::handler
    # Optional: Set the Python interpreter to use
    # PythonInterpPerDirective On # Creates a new interpreter per directory
    # PythonPath "['/var/www/myapp'] + sys.path" # Add your app to the path
</Directory>

In this example, when a request was made for http://example.com/index.py, Apache would load the Python script index.py and call a function named handler within it.

Example index.py:

# index.py
def handler(req):
    # req is the Apache request object
    req.content_type = 'text/html'
    req.write("<html><body>")
    req.write("<h1>Hello from mod_python!</h1>")
    req.write("<p>The request URI was: %s</p>" % req.uri)
    req.write("</body></html>")
    return apache.OK # Important: Return an Apache status code

(Note: You would also need to import apache)


The Downfall and Deprecation of mod_python

mod_python's dominance was short-lived. It was rapidly superseded by a better technology: mod_wsgi.

Why mod_wsgi Won:

  1. Better Process Management: mod_wsgi could run in two modes:

    • Embedded Mode: Similar to mod_python, it embeds Python in Apache.
    • Daemon Mode: This was the game-changer. It runs a separate, dedicated group of Python daemon processes. This isolates your application from Apache, preventing a single buggy Python app from crashing the entire web server. It also allows for better resource management and easier use of technologies like pre-forking (Gunicorn/uWSGI style).
  2. Simplicity and Standards: mod_wsgi aimed to be simpler and more compliant with standard Python web frameworks (like WSGI). It was easier to configure and integrate with frameworks like Django and Flask.

  3. Maintenance: mod_python development effectively stalled in 2007. The last official release was 3.5.1 in 2025, but it received no active development for a decade. The community and its creator moved to mod_wsgi.

Official Deprecation: In 2025, the ASF (Apache Software Foundation) officially deprecated mod_python, stating it was no longer maintained and should not be used in new projects.


The Modern Alternative: mod_wsgi

Today, the standard and recommended way to run Python applications under Apache is mod_wsgi.

Configuration Example for mod_wsgi:

# 1. Load mod_wsgi
LoadModule wsgi_module modules/mod_wsgi.so
# 2. Configure a WSGI script alias
WSGIScriptAlias / /var/www/myapp/myapp.wsgi
<Directory /var/www/myapp>
    Require all granted
</Directory>

Example myapp.wsgi file:

# myapp.wsgi
import sys
import os
# Add the project root to the Python path
sys.path.append('/var/www/myapp')
# Set the DJANGO_SETTINGS_MODULE environment variable
# (for Django apps)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# Import your application
from myproject.wsgi import application
# The 'application' variable is the WSGI callable

Summary Table: mod_python vs. mod_wsgi

Feature mod_python (Legacy) mod_wsgi (Modern Standard)
Technology Embedded Python in Apache WSGI-compliant, can be embedded or daemon mode.
Python 2.7 Primary use case. Was the standard for Py2.7. Fully supported, but its primary use is now Python 3.
Statefulness Could be stateful, but this was often a source of bugs and complexity. Daemon mode provides clean, isolated state management.
Configuration More complex, tied to Apache internals. Simpler, WSGI-focused, more portable.
Status Deprecated & Unmaintained. Do not use for new projects. Actively Maintained. The recommended way to deploy with Apache.
Use Today Only for maintaining ancient, legacy systems. The standard for deploying Python web apps on Apache.

Conclusion

  • mod_python with Python 2.7 was a significant step forward for Python web performance in the mid-2000s.
  • However, it is now a legacy technology. It is deprecated, unmaintained, and has been completely replaced by the superior mod_wsgi.
  • If you are maintaining an old server that uses mod_python, your long-term plan should be to migrate to a modern stack, likely using mod_wsgi (if you must stay with Apache) or a more common setup like Nginx + Gunicorn/uWSGI for better performance and flexibility.
分享:
扫描分享到社交APP
上一篇
下一篇