在macos上使用mod

一、安装mod_wsgi 3.4:

./configure –with-apxs=/users/levin/dev/apache2.2.27/bin/apxs –with-python=/usr/bin/python
make
make install

编辑httpd.conf使apache导入模块mod_wsgi.so以及引入vhost配置文件:

loadmodule wsgi_module modules/mod_wsgi.so
include conf/extra/httpd-vhosts.conf

编辑extra/httpd-vhosts.conf新建项目并增加gzip压缩python输出的文本:

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 #开启gzip
setenvifnocase request_uri .(?:gif|jpe?g|png)$ no-gzip dont-vary #图片不开启gzip
setenvifnocase request_uri .(?:exe|t?gz|zip|bz2|rar)$ no-gzip dont-vary #压缩包不开启gzip
setenvifnocase request_uri .(?:pdf|doc)$ no-gzip dont-vary
addoutputfilterbytype deflate text/*
addoutputfilterbytype deflate application/javascript application/x-javascript application/xml
addoutputfilterbytype deflate application/x-httpd-php

先写个测试脚本app.py

def application(environ, start_response):
start_response(‘200 ok’, [(‘content-type’, ‘text/html’)])
return [‘hello, world.’]

或者使用web.py框架:

import web
urls = (
‘/.*’, ‘hello’,
)
class hello:
def get(self):
return “hello, world.”
application = web.application(urls, globals()).wsgifunc()

在浏览器中访问: http://localhost:8001/,看到hello, world.就算安装成功了。

二、django使用中可能遇到的麻烦解决:
1.修改setting.py文件:

debug = true
template_debug = false
allowed_hosts = [‘localhost’]

2.修改项目中的wsgi.py,这个是建项目的时候就自带创建的,跟setting.py在同一目录,我傻傻的自己创建好多次,后来才发现文件位置不对,悲剧了。

#/library/webserver/documents是apache中documentroot位置
#votebing是我建的项目
import sys
sys.path.append(‘/library/webserver/documents/votebing’)

3.修改apache安装目录中的httpd.conf,我的是在/etc/apache2/httpd.conf

#载入mod_wsgi
loadmodule wsgi_module /usr/libexec/apache2/mod_wsgi.so
wsgiscriptalias /votebing /library/webserver/documents/votebing/votebing/wsgi.py
wsgipythonpath /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

Posted in 未分类

发表评论