在Mac OS上使用mod_wsgi连接Python与Apache服务器
作者:goldensun 时间:2021-12-18 11:20:02
一、安装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>
标签:Mac,Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
nx.adjacency_matrix计算邻接矩阵与真实结果不一致的解决
2023-12-06 14:23:56
windows下Python安装、使用教程和Notepad++的使用教程
2023-04-21 09:31:39
![](https://img.aspxhome.com/file/2023/6/86366_0s.png)
Python得到弹幕并保存到Excel中怎么设置
2021-04-04 16:42:22
![](https://img.aspxhome.com/file/2023/9/99289_0s.jpg)
W3C优质网页小贴士(一)
2008-04-06 16:35:00
![](https://img.aspxhome.com/file/UploadPic/up/2008040712541063.gif)
asp OpenTextFile文本读取与写入实例代码
2011-04-15 11:00:00
分享个asp文件缓存代码,使程序从缓存读数据
2011-03-09 19:47:00
SQL SQL Server 2008中的新日期数据类型
2009-03-10 15:01:00
VS Code有哪些奇技淫巧(必知)
2022-01-07 21:10:47
![](https://img.aspxhome.com/file/2023/7/100847_0s.png)
解析python高级异常和运算符重载
2021-06-17 00:56:41
pyecharts绘制时间轮播图柱形图+饼图+玫瑰图+折线图
2022-01-18 13:19:47
![](https://img.aspxhome.com/file/2023/0/79800_0s.png)
python,pycharm的环境变量设置方式
2023-08-04 10:44:41
![](https://img.aspxhome.com/file/2023/0/101060_0s.png)
python3 字符串str和bytes相互转换
2023-03-31 09:12:47
不归路系列:Python入门之旅-一定要注意缩进!!!(推荐)
2021-07-08 07:35:00
![](https://img.aspxhome.com/file/2023/5/78735_0s.png)
Python闭包实现计数器的方法
2021-01-23 09:11:00
Python二叉树的镜像转换实现方法示例
2023-09-28 18:36:35
![](https://img.aspxhome.com/file/2023/3/62553_0s.png)
Python判断变量名是否合法的方法示例
2022-07-31 19:05:12
![](https://img.aspxhome.com/file/2023/1/68411_0s.png)
python分布式编程实现过程解析
2023-11-10 21:13:48
在线HTML编辑器原理(eweb原理)
2009-01-08 12:25:00
python thrift 实现 单端口多服务的过程
2022-04-28 21:46:00
全面解读Python Web开发框架Django
2022-06-24 19:41:45