在Mac OS上使用mod_wsgi连接Python与Apache服务器


Posted in Python onDecember 24, 2015

一、安装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

<VirtualHost *:8001>
  WSGIScriptAlias / /Users/levin/dev/py/webapp/app.py/
  Alias /assets /Users/levin/dev/py/webapp/static/
  AddType text/html .py 
  <Directory /Users/levin/dev/py/webapp/>
    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
  </Directory>
</VirtualHost>

先写个测试脚本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 
 
<Directory /Library/WebServer/Documents/votebing/> 
<Files wsgi.py> 
Order deny,allow 
Allow from all 
</Files> 
</Directory> 
Alias /media/ /Library/WebServer/Documents/votebing/media/ 
Alias /static/ /Library/WebServer/Documents/votebing/static/ 
 
<Directory /Library/WebServer/Documents/votebing/static> 
Allow from all 
</Directory> 
 
<Directory /Library/WebServer/Documents/votebing/media> 
Allow from all 
</Directory>
Python 相关文章推荐
python实现复制整个目录的方法
May 12 Python
python利用Guetzli批量压缩图片
Mar 23 Python
使用Python的package机制如何简化utils包设计详解
Dec 11 Python
python leetcode 字符串相乘实例详解
Sep 03 Python
Python推导式简单示例【列表推导式、字典推导式与集合推导式】
Dec 04 Python
通过python爬虫赚钱的方法
Jan 29 Python
python django框架中使用FastDFS分布式文件系统的安装方法
Jun 10 Python
Django模板Templates使用方法详解
Jul 19 Python
Python进程间通信 multiProcessing Queue队列实现详解
Sep 23 Python
基于Python+Appium实现京东双十一自动领金币功能
Oct 31 Python
如何基于python操作json文件获取内容
Dec 24 Python
python 如何用urllib与服务端交互(发送和接收数据)
Mar 04 Python
在Mac OS上搭建Python的开发环境
Dec 24 #Python
详解Python字符串对象的实现
Dec 24 #Python
浅谈Python单向链表的实现
Dec 24 #Python
Python使用面向对象方式创建线程实现12306售票系统
Dec 24 #Python
安装ElasticSearch搜索工具并配置Python驱动的方法
Dec 22 #Python
Python生成随机验证码的两种方法
Dec 22 #Python
基于python实现微信模板消息
Dec 21 #Python
You might like
php 特殊字符处理函数
2008/09/05 PHP
3个PHP多维数组转为一维数组的方法实例
2014/03/13 PHP
PHP多进程编程实例
2014/10/15 PHP
PHP微信公众号开发之微信红包实现方法分析
2017/07/14 PHP
ThinkPHP3.2框架操作Redis的方法分析
2019/05/05 PHP
JavaScript的类型简单说明
2010/09/03 Javascript
js输入框邮箱自动提示功能代码实现
2013/12/10 Javascript
jquery获取tr并更改tr内容示例代码
2014/02/13 Javascript
jQuery老黄历完整实现方法
2015/01/16 Javascript
jQuery EasyUI datagrid实现本地分页的方法
2015/02/13 Javascript
JS通过ajax动态读取xml文件内容的方法
2015/03/24 Javascript
JavaScript中的toLocaleDateString()方法使用简介
2015/06/12 Javascript
jquery对dom节点的操作【推荐】
2016/04/15 Javascript
React.js中常用的ES6写法总结(推荐)
2017/05/09 Javascript
JS点击图片弹出文件选择框并覆盖原图功能的实现代码
2017/08/25 Javascript
JS鼠标3次点击事件实现代码及扩展思路
2017/09/12 Javascript
微信小程序block的使用教程
2018/04/01 Javascript
Javascript 关于基本类型和引用类型的个人理解
2019/11/01 Javascript
Node.js API详解之 readline模块用法详解
2020/05/22 Javascript
[00:13]天涯墨客二技能展示
2018/08/25 DOTA
Python设计模式之观察者模式实例
2014/04/26 Python
Python 实现自动获取种子磁力链接方式
2020/01/16 Python
Pyecharts 动态地图 geo()和map()的安装与用法详解
2020/03/25 Python
django rest framework使用django-filter用法
2020/07/15 Python
PyTorch如何搭建一个简单的网络
2020/08/24 Python
HTML5声音录制/播放功能的实现代码
2018/05/03 HTML / CSS
微软新西兰官方网站:Microsoft New Zealand
2018/08/17 全球购物
介绍一下SOA和SOA的基本特征
2016/02/24 面试题
培训专员岗位职责
2014/02/26 职场文书
暑期培训班招生方案
2014/08/26 职场文书
手机被没收的检讨书
2014/10/04 职场文书
2014最新股权信托合同协议书
2014/11/18 职场文书
仓库管理员岗位职责
2015/02/03 职场文书
世界文化遗产导游词
2015/02/13 职场文书
Python图像处理库PIL详细使用说明
2022/04/06 Python
六个好看实用的 HTML + CSS 后台登录入口页面
2022/04/28 HTML / CSS