This article mainly introduces how to use mod_wsgi to connect Python to the Apache server on MacOS, this article also describes the common solutions to problems that may occur in mod_wsgi connection mode when using the Python Django framework. For more information, see
1. Install mod_wsgi 3.4:
./configure --with-apxs=/Users/levin/dev/apache2.2.27/bin/apxs --with-python=/usr/bin/pythonmakemake install
Edit httpd. conf to make the Apache import module mod_wsgi.so and introduce the vhost configuration file:
LoadModule wsgi_module modules/mod_wsgi.soInclude conf/extra/httpd-vhosts.conf
Edit extra/httpd-vhosts.conf new project and add gzip compressed python output text:
Listen 8001.
WSGIScriptAlias // Users/levin/dev/py/webapp/app. py/Alias/assets/Users/levin/dev/py/webapp/static/AddType text/html. py
Order deny, allow Allow from all SetOutputFilter DEFLATE # enable gzip SetEnvIfNoCase Request_URI .(? : Gif | jpe? G | png) $ no-gzip dont-vary # The image does not enable gzip SetEnvIfNoCase Request_URI .(? : Exe | t? Gz | zip | bz2 | rar) $ no-gzip dont-vary # gzip SetEnvIfNoCase Request_URI is not enabled for the compressed package .(? : Pdf | doc) $ no-gzip dont-vary AddOutputFilterByType DEFLATE text/* AddOutputFilterByType DEFLATE application/javascript application/x-javascript application/xml AddOutputFilterByType DEFLATE application/x-httpd
First write a test script app. py
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return ['Hello, world.']
Or use the web. py framework:
import weburls = ( '/.*', 'hello',)class hello: def GET(self): return "Hello, world."application = web.application(urls, globals()).wsgifunc()
Access: http: // localhost: 8001/in the browser. If you see Hello, world, the installation is successful.
2. Problems That Django may encounter during use:
1. Modify the setting. py file:
DEBUG = True TEMPLATE_DEBUG = False ALLOWED_HOSTS = ['localhost']
2. modify wsgi. py, which is created when the project is created, with setting. py is in the same directory. I have created it many times, and then I found that the file location is incorrect. It is a tragedy.
#/Library/WebServer/Documents is the DocumentRoot location in apache # votebing is my project import sys. path. append ('/Library/WebServer/Documents/votebing ')
3. Modify httpd. conf in the apache installation directory.
# Load mod_wsgi LoadModule wsgi_module/usr/libexec/apache2/mod_wsgi.so logs/votebing/Library/WebServer/Documents/votebing/wsgi. py logs/Library/WebServer/Documents
Order deny, allow Allow from all
Alias/media // Library/WebServer/Documents/votebing/media/Alias/static // Library/WebServer/Documents/votebing/static/
Allow from all
Allow from all