Python-Django Win7上使用Apache24和mod_wsgi部署Django1.11应用程序

简介: Python-Django Win7上使用Apache24和mod_wsgi部署Django1.11应用程序

Win7上使用Apache24mod_wsgi部署Django1.11应用程序


 

测试环境

win7 64

Django-1.11.4.tar.gz

下载地址:https://pan.baidu.com/s/1hsclV5y

 

httpd-2.4.23-win64.zip

下载地址1https://www.apachelounge.com/download/VC10/

下载地址2https://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++ 编译器编译的ApachePython版本。

4、使用相同Microsoft C/C++ 编译器编译的Apachemod_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 -kstart

注: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.101Django所在主机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

 

浏览器验证

 

 

 


 

目录
相关文章
|
11月前
|
监控 数据可视化 数据挖掘
Python Rich库使用指南:打造更美观的命令行应用
Rich库是Python的终端美化利器,支持彩色文本、智能表格、动态进度条和语法高亮,大幅提升命令行应用的可视化效果与用户体验。
953 0
|
人工智能 Linux 开发工具
Python从零到一:手把手带你写出第一个实用程序
Python语法简洁易懂,适合编程新手入门。它广泛应用于人工智能、自动化办公、Web开发等领域。学习Python可快速搭建项目,拥有丰富库支持和强大社区资源。通过本教程,你将掌握基础语法、环境搭建、程序逻辑控制及实战项目开发,开启编程之旅。
1435 0
|
12月前
|
数据采集 监控 Java
Python 函数式编程的执行效率:实际应用中的权衡
Python 函数式编程的执行效率:实际应用中的权衡
447 102
|
11月前
|
机器学习/深度学习 算法 安全
【强化学习应用(八)】基于Q-learning的无人机物流路径规划研究(Python代码实现)
【强化学习应用(八)】基于Q-learning的无人机物流路径规划研究(Python代码实现)
736 6
|
11月前
|
设计模式 决策智能 Python
Python条件控制:让程序学会"思考"的魔法
本文深入浅出地讲解Python条件控制,从基础if语句到多分支、嵌套结构,再到简洁的三元表达式与Python 3.10新增的match-case模式匹配,结合电商折扣、会员等级、ATM系统等实战案例,全面掌握程序“智能决策”的核心逻辑。
640 0
|
11月前
|
设计模式 缓存 运维
Python装饰器实战场景解析:从原理到应用的10个经典案例
Python装饰器是函数式编程的精华,通过10个实战场景,从日志记录、权限验证到插件系统,全面解析其应用。掌握装饰器,让代码更优雅、灵活,提升开发效率。
654 0
|
12月前
|
数据采集 存储 数据可视化
Python网络爬虫在环境保护中的应用:污染源监测数据抓取与分析
在环保领域,数据是决策基础,但分散在多个平台,获取困难。Python网络爬虫技术灵活高效,可自动化抓取空气质量、水质、污染源等数据,实现多平台整合、实时更新、结构化存储与异常预警。本文详解爬虫实战应用,涵盖技术选型、代码实现、反爬策略与数据分析,助力环保数据高效利用。
601 0
|
12月前
|
存储 程序员 数据处理
Python列表基础操作全解析:从创建到灵活应用
本文深入浅出地讲解了Python列表的各类操作,从创建、增删改查到遍历与性能优化,内容详实且贴近实战,适合初学者快速掌握这一核心数据结构。
780 0
|
12月前
|
中间件 机器人 API
Python多态实战:从基础到高阶的“魔法”应用指南
Python多态机制通过“鸭子类型”实现灵活接口,使不同对象统一调用同一方法,自动执行各自行为。它简化代码逻辑、提升扩展性,适用于数据处理、策略切换、接口适配等场景。掌握多态思维,能有效减少冗余判断,使程序更优雅、易维护。
527 0