The WSGIPythonHome
directive is a crucial configuration for Python WSGI applications running under Apache. It specifies the location of the Python installation that should be used by mod_wsgi. This becomes particularly important when:
- You have multiple Python versions installed
- Your system Python differs from your project's virtual environment
- You need to ensure consistent Python environment across deployments
The directive should be placed in the global server context of your Apache configuration, typically near other mod_wsgi directives. Here's the proper placement:
# In your httpd.conf or virtual host configuration
# Typically in the main configuration section
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /path/to/python/home
# Other WSGI directives...
Here are three common use cases with example configurations:
# Scenario 1: System-wide Python installation
WSGIPythonHome /usr
# Scenario 2: Virtual environment
WSGIPythonHome /path/to/venv
# Scenario 3: Custom Python build
WSGIPythonHome /opt/python-3.9.6
After adding the directive, you should verify it's working correctly. Create a test WSGI application:
# test.wsgi
def application(environ, start_response):
import sys
status = '200 OK'
output = f"Python Path: {sys.path}\nPython Executable: {sys.executable}".encode('utf-8')
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
- Ensure the path exists and is accessible by Apache
- Check Apache error logs if WSGI fails to initialize
- Verify Python version matches your application requirements
- Remember to restart Apache after configuration changes
While httpd.conf is the primary location, you can also place the directive in:
- Virtual host configuration files
- Include files (if properly scoped)
- .htaccess files (if AllowOverride is properly configured)
The WSGIPythonHome
directive is crucial when configuring Python web applications through mod_wsgi on Apache. It specifies the location of your Python installation, particularly important when:
- Using a virtual environment
- Having multiple Python installations
- Running system Python alongside custom installations
The directive should be placed in the global server configuration context of your httpd.conf file, typically before any virtual host definitions. Here's the correct placement:
# Global Apache configuration
ServerRoot "/etc/httpd"
Listen 80
# Python WSGI Configuration
WSGIPythonHome /path/to/python/installation
# Virtual Hosts
<VirtualHost *:80>
# Your host configuration
</VirtualHost>
1. Using a Virtual Environment
WSGIPythonHome /home/user/venvs/myapp
2. Custom Python Installation
WSGIPythonHome /opt/python-3.9.6
3. System-wide Python
WSGIPythonHome /usr
If you encounter issues after setting WSGIPythonHome
, check:
- Path permissions (Apache must have read access)
- Correct Python version matches your application requirements
- The directive appears only once in your configuration
After making changes, test your configuration with:
apachectl configtest
Then restart Apache:
systemctl restart httpd