Python-Django Win7上使用Apache24和mod_wsgi部署Django1.11应用程序
Win7上使用Apache24和mod_wsgi部署Django1.11应用程序
by:授客 QQ:1033553122
测试环境
win7 64
Django-1.11.4.tar.gz
下载地址:https://pan.baidu.com/s/1hsclV5y
httpd-2.4.23-win64.zip
下载地址1:https://www.apachelounge.com/download/VC10/
下载地址2:https://pan.baidu.com/s/1hsclV5y
Python 3.4.0
Apache24-win64-VC10|mod_wsgi-py34-VC10.so
下载地址:https://pan.baidu.com/s/1hsclV5y(链接包含不同版本的集合包)
软件包依赖VS2010

先决条件
注意,部署之前,必须先满足以下几个条件
1、位数要对。操作系统,安装的对应软件,要么统一32位,要么统一用64位,不要混用。
2、安装Python时选择为所有用户安装,而不是仅为安装python的用户安装。
3、使用相同Microsoft C/C++ 编译器编译的Apache和Python版本。
4、使用相同Microsoft C/C++ 编译器编译的Apache和mod_wsgi版本。
以下为Python版本和C/C++编译器的对应关系
Python 2.6 - VC9
Python 2.7 - VC9
Python 3.3 - VC10
Python 3.4 - VC10
以下为Apache版本和mod_wsgi版本的对应关系
Apache22-win32-VC9/modules/mod_wsgi-py26-VC9.so
Apache22-win32-VC9/modules/mod_wsgi-py27-VC9.so
Apache24-win32-VC9/modules/mod_wsgi-py26-VC9.so
Apache24-win32-VC9/modules/mod_wsgi-py27-VC9.so
Apache24-win32-VC10/modules/mod_wsgi-py33-VC10.so
Apache24-win32-VC10/modules/mod_wsgi-py34-VC10.so
Apache24-win64-VC10/modules/mod_wsgi-py33-VC10.so
Apache24-win64-VC10/modules/mod_wsgi-py34-VC10.so
参考链接:
https://github.com/GrahamDumpleton/mod_wsgi/blob/develop/win32/README.rst
部署操作
项目文件结构


解压httpd-2.4.23-win64.zip,取出其中的目录(例中Apache24),放到目标路径(不能有空格等),例中D:/Apache24
检查Apache版本是否正确
cd /d D:/Apache24/bin
httpd.exe -V
Server version: Apache/2.4.23 (Win64)
……
修改Apache配置
打开conf/httpd.conf文件,编辑,修改服务器根目录
ServerRoot "c:/Apache24" 改成 ServerRoot "d:/Apache24"
然后查找所有的 "c:/Apache24",全部改成 "d:/Apache24"
修改监听端口(可选,根据实际需要)
Listen 80 改成 Listen 8000
修改服务器名称(建议)
#ServerName www.example.com:80 改成 ServerName 192.168.1.101:80
注:这里我没有注册域名,直接改成了本机ip
去掉#注释,打开访问日志
CustomLog "logs/access.log" common
修改#LoadModule rewrite_module modules/mod_rewrite.so 为如下:
LoadModule rewrite_module modules/mod_rewrite.so
说明:加载重写模块,防止出现 The requested URL / was not found on this server的情况。
找到如下配置
AllowOverride none
Require all denied
修改为
AllowOverride ALL
Require all granted
说明:配置更改,以防止出现如下情形:
Forbidden
You don't have permission to access / on this server.
启动Apache
1)httpd.exe -k install -n Apache2.4
Installing the 'Apache2.4' service
The 'Apache2.4' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
注:install:把Apache注册为Windows服务,反之 uninstall, -n 接服务名称
2) httpd.exe -k start
注:start 启动, stop 停止
浏览器访问


添加mod_wsgi.so模块
把mod_wsgi-py34-VC10.so重命名为mod_wsgi.so,放入D:\Apache24\modules目录下。
参考链接:
http://modwsgi.readthedocs.io/en/develop/
https://pypi.python.org/pypi/mod_wsgi
再次修改Apache 配置
打开conf/httpd.conf文件,编辑,在末尾添加一下内容:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / D:/AutotestPlatform/AutotestPlatform/wsgi.py
WSGIPythonHome "D:/Program Files (x86)/python34"
WSGIPythonPath D:/AutotestPlatform/AutotestPlatform/website
Alias /static/ D:/AutotestPlatform/website/static/
<Directory D:/AutotestPlatform/website/static>
Require all granted
</Directory>
<Directory D:/AutotestPlatform/AutotestPlatform/website/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
说明:
LoadModule 加载模块
WSGIScriptAlias 设置base URL, / 代表应用程序root url
WSGIPythonHome 设置python安装路径,
WSGIPythonPath 用于确保项目包可导入,即import AutotestPlatform 有效。
<Directory> 保证Apache可访问wsgi.py,及其它必要文件。
以下设置Apache通过mod_wsgi为静态资源服务
Alias /static D:/AutotestPlatform/AutotestPlatform/website/static/
<Directory D:/AutotestPlatform/AutotestPlatform/website/static>
Require all granted
</Directory>
注意:如果使用的apache低于2.4版本,修改Require all granted 为如下内容:
Order deny,allow
Allow from all
参考链接:
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/
https://github.com/GrahamDumpleton/mod_wsgi/blob/develop/win32/README.rst
收集静态配置文件
1)修改应用的settings.py(例中为D:\AutotestPlatform\AutotestPlatform\settings.py)
STATIC_URL = '/static/' 改行下方新增如下内容:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
2)运行命令,收集静态文件到指定的static目录
cd /d D:\AutotestPlatform
python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
D:\AutotestPlatform\static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes #输入yes,回车
完成后,把收集的静态文件都放到 D:\AutotestPlatform\static目录下
参考链接:
https://pypi.python.org/pypi/mod_wsgi
Django访问IP配置
修改应用的settings.py(例中为D:\AutotestPlatform\AutotestPlatform\settings.py),编辑,找到ALLOWED_HOSTS修改为如下值,其中192.168.1.101是Django所在主机ip,也就是客户端浏览器访问用的IP
ALLOWED_HOSTS = ['localhost','127.0.0.1','192.168.1.101']
修改wsgi.py
如下,新增带背景色内容,以解决找不到AutotestPlatform模块的问题
from django.core.wsgi import get_wsgi_application
import sys
sys.path.append('D:/AutotestPlatform/AutotestPlatform/')
sys.path.append('D:/AutotestPlatform/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "AutotestPlatform.settings")
application = get_wsgi_application()
重启Apache并启动Django应用
D:\Apache24\bin>httpd.exe -k stop
The 'Apache2.4' service is stopping.
The 'Apache2.4' service has stopped.
D:\Apache24\bin>httpd.exe -k start
说明:到这一步,已经可以浏览器访问了,以下为了看对比效果,同时开启了Django
启动Django
python manage.py runserver 0.0.0.0:8001 --insecure
浏览器验证




Python-Django Win7上使用Apache24和mod_wsgi部署Django1.11应用程序的更多相关文章
- 使用Nginx+uwsgi在亚马逊云服务器上部署python+django项目完整版(二)——部署配置及相关知识
---恢复内容开始--- 一.前提: 1.django项目文件已放置在云服务器上,配置好运行环境,可正常运行 2.云服务器可正常连接 二.相关知识 1.python manage.py runserv ...
- Python - Django - 页面上展示固定的页码数
如果页数太多的话,全部显示在页面上就会显得很冗杂 可以在页面中显示规定的页码数 例如: book_list.html: <!DOCTYPE html> <html lang=&quo ...
- Python Django文件上传
文件保存本地 view视图 def update(request): if request.method =='GET': return render(request,'update.html') e ...
- python django 批量上传文件并绑定对应文件的描述
- Win7上IIS发布网站系统\部署项目
1.系统已经安装IIS,如果没有安装,请到[控制面板]→[程序]→[程序和功能]→[打开或关闭Windows功能],选中Internet 信息服务Web管理工具下面的所有选项,确定:如下图 2.发布文 ...
- CentOS 5系统安装Django、Apache 、mod_wsgi部署Python环境教程
Django,是一款针对Python环境的WEB开发框架,能够帮助我们构架快捷.简单的WEB框架设置,Django框架非常适合开发内容应用环境,所以在本文中,麦子将整理基于Centos系统部署安装Dj ...
- 使用Visual Studio 2017开发python,并在iis上部署Python Django
作为宇宙第一IDE,怎么可以不支持python开发呢? 1.Visual Studio Installer 扩展Python开发 开始菜单中打开Visual Studio Installer,点修改. ...
- python Django之文件上传
python Django之文件上传 使用Django框架进行文件上传共分为俩种方式 一.方式一 通过form表单进行文件上传 #=================================== ...
- [原创]win7环境下搭建eclipse+python+django开发环境
一)工具下载 a)eclipse(最新版4.3.1)官网下载地址 http://www.eclipse.org/downloads/ b)python (2.X版本)官网下载地址 http://pyt ...
随机推荐
- 如何写gdb命令脚本
作为UNIX/Linux下使用广泛的调试器,gdb不仅提供了丰富的命令,还引入了对脚本的支持:一种是对已存在的脚本语言支持,比如python,用户可以直接书写python脚本,由gdb调用python ...
- Spring Boot 主类及目录结构介绍
Spring Boot 与传统项目最大的区别是,传统项目都是打成 WAR 包部署到服务器上面,需要额外的 Servlet 容器, 而 Spring Boot 则可以直接打成 jar 包,并内置集成了 ...
- python基础-字典(8)
一.字典介绍 字典和列表一样,也是存储多个数据的容器,只不过存储和读取的方式不同 字典的语法格式: 字典名 = {key1 : value1,key2:value2,key3:value3,……} 说 ...
- [干货,阅后进BAT不是梦]面试心得与总结---阿里、小米、腾讯
之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人一点指引和总结, 也希望能使大家少走点弯路 , 如 ...
- 两种方法:VS2008下C++窗体程序显示控制台的方法——在QT程序中使用cout和cin
老蔡写了一个基于QT的窗体程序,而过去写的类的调试信息都是用cout显示的,苦于窗体程序无法显示cout信息很多信息都看不到,于是就想到让控制台和窗体同时显示.显示控制台方法如下 1.项目(或者叫“工 ...
- sql server 性能调优之 资源等待 CXPACKET
一.概述 CXPACKET是指:线程正在等待彼此完成并行处理.什么意思呢? 当sql server发现一条指令复杂时,会决定用多个线程并行来执行,由于某些并行线程已完成工作,在等待其它并行线程来同步 ...
- Chapter 4 Invitations——2
To my dismay, I found myself the center of attention for the rest of that week. 令我沮丧的是, 我发现我自己剩余注意力的 ...
- 1197多行事务要求更大的max_binlog_cache_size处理与优化
1197多语句事务要求更大的max_binlog_cache_size报错 binlog_cache_size:为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存,提高记录bi ...
- JavaScript和Ajax部分(2)
11. DOM如何操作文档的标准节点? 1) 查看节点:使用getElementById(),getElementByName(),getElementByTagName可以查看HTML文档中的任何元 ...
- java 学习基础知识点拾遗 导航页
每种编程语言的知识点都是很多很杂的,java也是如此 相信很多人学习的过程中都是深一脚浅一脚,最基础的东西可能有些也不是非常确定 整理了最基本的一些知识点,可以说是java入门的-1层级别的,作为自己 ...