Apache+mod_wsgi部署Flask:如何让应用使用venv虚拟环境?
Hey there! Since you’re coming from a PHP background, let’s break this down clearly and step-by-step—no confusing jargon where we can avoid it.
1. How to check which Python version your app is currently using?
You have two straightforward ways to get this info:
Add a temporary test route to your Flask app:
Open your main Flask script (likeapp.py) and add this quick route (you can delete it later):import sys from flask import Flask app = Flask(__name__) # Temporary route to show Python version @app.route('/python-version') def show_python_version(): return f"Current Python Version: {sys.version}"Restart Apache, then visit
http://your-domain/python-versionin your browser—you’ll see the exact Python version your app is running on.Log the version to Apache’s error log:
In your.wsgifile (the one linked in your Apache config), add these lines at the very top:import sys # Print version to Apache's error log print(f"Python version used by mod_wsgi: {sys.version}", file=sys.stderr)Restart Apache, then check your error log (usually at
/var/log/apache2/error.logon Debian/Ubuntu, or/var/log/httpd/error_logon RHEL/CentOS). You’ll find the version string in there.
2. How to check your mod_wsgi version?
Again, a few easy methods:
Command line check:
If you installed mod_wsgi via your package manager, run:# For Debian/Ubuntu (Python 3) dpkg -l libapache2-mod-wsgi-py3 # Or use mod_wsgi's built-in tool mod_wsgi-express module-configThe output will include the version number.
Check Apache’s loaded modules:
Run this command to list all active Apache modules:apachectl -M # Or httpd -M on RHEL/CentOSLook for
wsgi_module (shared)—some distributions include the version here.Log it via your .wsgi file:
Add this line to your.wsgifile (alongside the Python version line if you want):import mod_wsgi print(f"mod_wsgi version: {mod_wsgi.version}", file=sys.stderr)Restart Apache and check the error log for the version.
3. How to get your app working with the venv virtual environment?
This is the tricky part, but let’s fix it step by step—your failed python-home config is usually due to path issues or permissions.
Step 1: Verify your venv path
First, make sure you’re using the absolute path to your venv. For example, if your app lives at /var/www/my-flask-app, your venv should be at /var/www/my-flask-app/venv. Double-check this path—typos here are a super common mistake.
Step 2: Fix permissions for Apache
Apache runs as a dedicated user (usually www-data on Debian/Ubuntu, apache on RHEL/CentOS). This user needs read and execute access to your venv directory. Run this command to set the correct ownership:
sudo chown -R www-data:www-data /var/www/my-flask-app/venv
Or, if you prefer to keep ownership under your user, adjust permissions instead:
sudo chmod -R 755 /var/www/my-flask-app/venv
Step 3: Update your Apache config
Open your Apache site config file (e.g., /etc/apache2/sites-available/my-flask-app.conf) and update it to include the WSGIDaemonProcess directive with your venv path:
<VirtualHost *:80> ServerName your-domain.com # Define the daemon process with your venv WSGIDaemonProcess my-flask-app \ python-home=/var/www/my-flask-app/venv \ python-path=/var/www/my-flask-app # Map the root URL to your WSGI script WSGIScriptAlias / /var/www/my-flask-app/app.wsgi # Set permissions for your app directory <Directory /var/www/my-flask-app> WSGIProcessGroup my-flask-app WSGIApplicationGroup %{GLOBAL} Require all granted </Directory> # Optional: Log paths ErrorLog ${APACHE_LOG_DIR}/my-flask-app-error.log CustomLog ${APACHE_LOG_DIR}/my-flask-app-access.log combined </VirtualHost>
Let’s break this down:
python-home: Points directly to your venv directory (this tells mod_wsgi to use the venv’s Python interpreter).python-path: Adds your app’s root directory to Python’s module search path (so it can find your Flask app files).WSGIProcessGroup: Ensures the app uses the daemon process we defined with the venv.
Step 4: Update your .wsgi file (optional but safe)
Even though python-home should handle activating the venv, adding this snippet to your .wsgi file can resolve edge cases (like if system Python has conflicting packages):
# Activate the venv (absolute path!) activate_this = '/var/www/my-flask-app/venv/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) # Import your Flask app from app import app as application
Make sure from app import app matches your actual Flask app filename and variable name (if your app is in myapp.py, use from myapp import app).
Step 5: Install dependencies in the venv
Don’t forget—your venv is isolated from the system Python, so you need to install Flask (and any other packages) inside it:
# Activate the venv source /var/www/my-flask-app/venv/bin/activate # Install Flask and dependencies pip install flask # Deactivate when done deactivate
Step 6: Restart Apache and test
Finally, restart Apache to apply all changes:
sudo systemctl restart apache2
If your app still fails, check the Apache error log—this will tell you exactly what’s wrong (e.g., missing modules, permission denied, wrong path).
内容的提问来源于stack exchange,提问作者Ronni

