You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

手动停止mod_wsgi启动的进程并监控数量,及Python Web应用部署疑问

Answers to Your mod_wsgi & Apache Questions

1. Manually Stopping mod_wsgi Processes & Monitoring Process Count

Monitoring Running Processes

How you track mod_wsgi processes depends on whether you’re using embedded mode (mod_wsgi runs inside Apache’s httpd processes) or daemon mode (mod_wsgi spawns dedicated standalone processes):

  • Embedded Mode: All mod_wsgi workers are part of Apache’s child processes. Count them with:
    ps aux | grep '[h]ttpd' | wc -l
    
    The [h] trick avoids counting the grep command itself. Drop the | wc -l to see individual process details like PIDs.
  • Daemon Mode: mod_wsgi runs distinct processes. Filter directly for them:
    ps aux | grep '[m]od_wsgi' | wc -l
    

Stopping Processes

Always prefer using Apache’s service management over manual process killing to avoid orphaned processes or broken state:

  • Debian/Ubuntu:
    # Stop Apache entirely (kills all mod_wsgi processes)
    sudo systemctl stop apache2
    
    # Restart to refresh processes (safer for app code updates)
    sudo systemctl restart apache2
    
  • RHEL/CentOS/Rocky:
    sudo systemctl stop httpd
    sudo systemctl restart httpd
    

If you must kill a stuck daemon process (last resort), find its PID with ps aux | grep mod_wsgi then run:

sudo kill <PID>
# Force terminate if needed
sudo kill -9 <PID>

2. Reverse Proxy to Dev Server vs. mod_wsgi for Production + Setup Guide

Your Understanding is 100% Correct!

You’re right to avoid using Apache’s RewriteRule as a reverse proxy to Flask/Bottle’s built-in dev server (python myapp.py --port=5000) in production. The dev server is for local testing only—it’s single-threaded, lacks security hardening, and can’t handle real-world traffic reliably. Using mod_wsgi (a native WSGI adapter for Apache) is the stable, high-performance production choice.

Step-by-Step Setup for Python App ↔ mod_wsgi ↔ Apache

Prerequisites

  • Apache installed on your server
  • Python 3.x (match the version your app uses)
  • Your Flask/Bottle app in a dedicated directory (e.g., /home/yourusername/myapp)
  • A virtual environment for your app (critical to avoid dependency conflicts)

1. Install mod_wsgi

Match the package to your OS:

  • Debian/Ubuntu:
    sudo apt-get update && sudo apt-get install libapache2-mod-wsgi-py3
    
  • RHEL/CentOS/Rocky:
    sudo yum install mod_wsgi-python3
    

Verify installation with apache2ctl -M | grep wsgi (Debian) or httpd -M | grep wsgi (RHEL)—you should see wsgi_module (shared).

2. Configure Apache Virtual Host

Create a config file for your app:

  • Debian/Ubuntu: /etc/apache2/sites-available/myapp.conf
  • RHEL/CentOS: /etc/httpd/conf.d/myapp.conf

Add this configuration (adjust paths and settings to your setup):

<VirtualHost *:80>
    ServerName yourdomain.com  # Replace with your domain/server IP
    ServerAdmin admin@yourdomain.com

    # Define a dedicated daemon process group for your app
    WSGIDaemonProcess myapp \
        user=www-data group=www-data \
        threads=5 \
        python-path=/home/yourusername/myapp:/home/yourusername/myapp/venv/lib/python3.10/site-packages

    # Map root URL to your WSGI entry script
    WSGIScriptAlias / /home/yourusername/myapp/myapp.wsgi

    # Grant Apache access to your app directory
    <Directory /home/yourusername/myapp>
        Require all granted
    </Directory>

    # Logging (helpful for debugging)
    ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
    CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
</VirtualHost>
  • python-path: Includes your app directory and virtual environment site-packages so Apache can find your dependencies.
  • threads: Start with 5-10 for small apps, adjust based on traffic needs.

3. Create the WSGI Entry Script

In your app directory, make myapp.wsgi with this content:

# Activate the virtual environment
activate_this = '/home/yourusername/myapp/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# Add app directory to Python's path
import sys
sys.path.insert(0, '/home/yourusername/myapp')

# Import your app instance (mod_wsgi expects it named "application")
# For Flask:
from myapp import app as application

# For Bottle:
# import bottle
# from myapp import app
# application = bottle.default_app()

Ensure your app instance is explicitly named application—mod_wsgi looks for this variable by default.

4. Test & Enable the Configuration

  • Check for Apache config errors:
    # Debian/Ubuntu
    sudo apache2ctl configtest
    
    # RHEL/CentOS
    sudo httpd -t
    
  • Enable the site (Debian/Ubuntu only):
    sudo a2ensite myapp.conf
    
  • Restart Apache to apply changes:
    # Debian/Ubuntu
    sudo systemctl restart apache2
    
    # RHEL/CentOS
    sudo systemctl restart httpd
    

5. Verify the Setup

Visit your server’s IP/domain in a browser—your app should load correctly. If not, check the error logs (/var/log/apache2/myapp_error.log or /var/log/httpd/myapp_error.log) for debugging clues.

Key Notes

  • File Permissions: Ensure www-data (Debian) or apache (RHEL) has read access to your app directory. Avoid overly permissive settings like 777.
  • Python Version: Confirm mod_wsgi uses the same Python version as your virtual environment with mod_wsgi-express module-config.
  • Reloading Code: Restart Apache whenever you update your app code to refresh mod_wsgi processes.

内容的提问来源于stack exchange,提问作者Basj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:51:12