转载 : codingsoho.com

前言

本文主要介绍利用apache去部署Django项目,所有步骤均在本机Window7和阿里云验证通过。

配置

本例的基本配置如下:

工作目录: C:/virtualenv/zakkabag

项目名称: zakkabag

最终的安装版本如下,后面我会一步一步描述如何安装

操作系统

python

mysql

apache

Windows Server 2016 64bit

python 2.7.3 (32 位)

python-2.7.3.msi

32位安装版

mysql-5.5.12-win32.msi

MySQL-python-1.2.4b4.win32-py2.7.exe

32位安装版

httpd-2.2.25-win32-x86-no_ssl.msi

mod_wsgi-win32-ap22py27-3.3

安装

Python2.7.3

  • Windows x86 MSI Installer (2.7.3) (sig) http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi

安装路径: D:\Python27 (example)

安装完成之后,记得把D:\Python27加入环境变量Path,这样Python命令可以在任意路径执行

执行python命令,如果出现下面信息,说明安装已成功

C:\Users\User1>python

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

Django

Django的安装有多种方法,可以从源码安装,也可以利用pip命令安装

源码安装

文件路径; D:\Django-1.5.5 (example),这是一个很老的版本,之前用这个方法安装过,这儿仅是提供一种方法,一般我现在采用pip方法安装

安装命令:

cd D:\Django-1.5.5

python setup.py install

Django会在python Lib目录下面创建新的目录: d:\Python27\Lib\site-packages\django

….

byte-compiling d:\Python27\Lib\site-packages\django\views\i18n.py to i18n.pyc

byte-compiling d:\Python27\Lib\site-packages\django\views\static.py to static.py

……

byte-compiling d:\Python27\Lib\site-packages\django\views\__init__.py to __init__.pyc

byte-compiling d:\Python27\Lib\site-packages\django\__init__.py to __init__.pyc

running install_scripts

creating d:\Python27\Scripts

copying build\scripts-2.7\django-admin.py -> d:\Python27\Scripts

running install_egg_info

Writing d:\Python27\Lib\site-packages\Django-1.5.5-py2.7.egg-info

把D:\Python27\Lib\site-packages\加入环境变量path

把D:\Python27\Script加入环境变量Path,这样django-admin.py可以在任意目录运行

pip安装

pip install django==1.5.5

mysql & MySQLLdb

参考mysql安装指南

Apache2.2

安装版

从官网下载apache安装包http://httpd.apache.org/download.cgi,我的安装路径为d:\Program Files\

我之前用的版本是httpd-2.2.25-win32-x86-no_ssl.msi,官网已经找不到了,我的理解只要装同一系列应该问题就可以了。

安装完成之后,主要的路径及文件如下:

d:\Program Files\Apache Software Foundation\Apache2.2\modules\ 组件位置,需要加载的组件放在该路径

d:\Program Files\Apache Software Foundation\Apache2.2\conf\ httpd.conf 最重要的配置文件

D:\Program Files\Apache Software Foundation\Apache2.4\logs 相关错误打印可以在下面文件找到

免安装版

下载

直接从官网http://httpd.apache.org/download.cgi下载,并解压到对应目录

配置

需要将一些对应的路径替换掉,以httpd-2.2-x64版本为例,将安装文件httpd-2.2.19-win64.rar解压到目录D:/PythonWebSW

文件conf/httpd.conf

ServerRoot "/httpd-2.2-x64"

DocumentRoot "/httpd-2.2-x64/htdocs"

<Directory "/httpd-2.2-x64/cgi-bin">

ScriptAlias /cgi-bin/ "/httpd-2.2-x64/cgi-bin/"

替换为

ServerRoot "D:/PythonWebSW/httpd-2.2.19-win64"

DocumentRoot "D:/PythonWebSW/httpd-2.2.19-win64/htdocs"

<Directory "D:/PythonWebSW/httpd-2.2.19-win64/cgi-bin">

ScriptAlias /cgi-bin/ "D:/PythonWebSW/httpd-2.2.19-win64/cgi-bin/"

文件conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"

ServerName dummy-host.example.com

ServerAlias www.dummy-host.example.com

ErrorLog "logs/dummy-host.example.com-error.log"

CustomLog "logs/dummy-host.example.com-access.log" common

</VirtualHost>

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host2.example.com

DocumentRoot "/httpd-2.2-x64/docs/dummy-host2.example.com"

ServerName dummy-host2.example.com

ErrorLog "logs/dummy-host2.example.com-error.log"

CustomLog "logs/dummy-host2.example.com-access.log" common

</VirtualHost>

替换为

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot "D:/PythonWebSW/httpd-2.2.19-win64/docs/dummy-host.example.com"

ServerName dummy-host.example.com

ServerAlias www.dummy-host.example.com

ErrorLog "logs/dummy-host.example.com-error.log"

CustomLog "logs/dummy-host.example.com-access.log" common

</VirtualHost>

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host2.example.com

DocumentRoot "D:/PythonWebSW/httpd-2.2.19-win64/docs/dummy-host2.example.com"

ServerName dummy-host2.example.com

ErrorLog "logs/dummy-host2.example.com-error.log"

CustomLog "logs/dummy-host2.example.com-access.log" common

</VirtualHost>

安装

D:\PythonWebSW\httpd-2.2.19-win64\bin>httpd -k install

有的时候会报错

D:\httpd-2.2.19-win64\bin>httpd -k install

Installing the Apache2.2 service

The Apache2.2 service is successfully installed.

Testing httpd.conf....

Errors reported here must be corrected before the service can be started.

httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.1.101 for ServerName

(OS 10013)An attempt was made to access a socket in a way forbidden by its access permissions.  : make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shutting down

Unable to open logs

原因及解决:有可能是系统80端口默认被占用了,可以修改conf/httpd.conf监听端口,Listen 80 修改为 Listen 8081(或其他端口号)

启动

D:\PythonWebSW\httpd-2.2.19-win64\bin>start httpd

卸载

D:\PythonWebSW\httpd-2.2.19-win64\bin>httpd -k uninstall

mod_wsgi

下载

注意:下载mod_wsgi.so版本需和python和apache匹配

http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

我之前下过一个对应apache22和python2.7的版本,名字mod_wsgi-win32-ap22py27-3.3,这个可以直接用,所以一直用着,网上可以直接搜到。

安装和配置

将mod_wsgi.so放到d:\Program Files\Apache Software Foundation\Apache2.2\modules\ , 并在httpd.conf里添加

LoadModule wsgi_module modules/mod_wsgi.so

httpd.conf的位置在d:\Program Files\Apache Software Foundation\Apache2.2\conf\

注意:apache装载wsgi模块,名字可以自定义

配置 httpd.conf

搜索http.conf文件,把"Deny from all" 及 "Require all denied" 等语句全部注释掉:

<Directory />

Options FollowSymLinks

AllowOverride None

Require all denied

Order deny,allow

Deny from all

</Directory>

修改为

<Directory />

Options Indexes FollowSymLinks

AllowOverride None

</Directory>

测试wsgi

添加test.wsgi文件,我放在项目下面的apache文件夹下,这个文件夹是专门用来放跟apache相关的配置的

def application(environ, start_response):

status = '200 OK'

output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),

('Content-Length', str(len(output)))]

start_response(status, response_headers)

return [output]

在http.conf里面引入这段代码

Include " C:/virtualenv/zakkabag/apache /test.wsgi "

测试 : http://localhost:8081

注:这儿监听端口是8081

项目配置

项目主目录

在项目目录下面创建一个文件夹apache,添加文件"django.wsgi" & "apache_django_wsgi.conf"

和test.wsgi一样,我们也可以用这个方法引入这段配置

Include " C:/virtualenv/zakkabag/apache/apache_django_wsgi.conf"

注:这些配置文件可直接写在httpd.conf文件里,但我倾向于用专门的文件,这样便于项目维护

apache_django_wsgi.conf

测试项目

WSGIScriptAlias / " C:/virtualenv/zakkabag/apache/test.wsgi"

实际项目

WSGIScriptAlias / " C:/virtualenv/zakkabag/apache/django.wsgi"

<Directory " C:/virtualenv/zakkabag ">

Options FollowSymLinks

AllowOverride None

Order deny,allow

Allow from all

</Directory>

django.wsgi

os.environ['DJANGO_SETTINGS_MODULE'] ="zakkabag.settings"

import django

django.setup()

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

静态资源

apache_django_wsgi.conf

Alias /static "C:/virtualenv/ zakkabag /static_in_env/static_root"

<Directory " C:/virtualenv/zakkabag/static_in_env ">

Order allow,deny

Allow from all

</Directory>

Django的setting里

Settings.py

STATIC_URL = '/static/'

重启

配置完成之后记得重启apache

常见问题

问题1 (OS 5)Access is denied. : Apache2.2: OpenService failed

如果出现这个错误,用管理员权限打开cmd

问题2 (OS 1072)The specified service has been marked for deletion. : Apache2.2: Failed to delete the service.

将service里面的停掉

问题3ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

原因是MysqlLdb没有安装好,具体参考mysql安装指南文章

Window 安装文件

MySQL-python-1.2.3.win-amd64-py2.7.exe

Linux:

MySQL-python==1.2.3

生成的文件如下

MySQL_python-1.2.3-py2.7.egg-info (文件夹)

MySQLdb (文件夹)

_mysql.pyd

_mysql_exceptions.py

_mysql_exceptions.pyc

_mysql_exceptions.pyo

问题4机器上可能会配置多个apache,有的时候安装新的apache时需要先把之前install的uninstall

问题5 Django: AppRegistryNotReady()

在 django.wsgi, 添加setup()

os.environ['DJANGO_SETTINGS_MODULE'] ="zakkabag.settings"

import django

django.setup()

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

参考 http://stackoverflow.com/questions/24793351/django-appregistrynotready

setup()

This function is called automatically:

  • When running an HTTP server via Django's WSGI support.
  • When invoking a management command.

It must be called explicitly in other cases, for instance in plain Python scripts.

关注下方公众号获取更多文章

转载于:https://www.cnblogs.com/2dogslife/p/8437170.html

Django在Window下的部署相关推荐

  1. window部署python项目_Django在Window下的部署

    转载 : codingsoho.com 前言 本文主要介绍利用apache去部署Django项目,所有步骤均在本机Window7和阿里云验证通过. 配置 本例的基本配置如下: 工作目录: C:/vir ...

  2. uwsgi+django在ubuntu下命令部署亲测ok

    项目目录: 我的setting文件: """ Django settings for jango_one project.Generated by 'django-adm ...

  3. Django简介Django 各个环境下的安装

    目录: Django介绍 Django 全貌 Django 安装 Window 下安装 Django Linux 上安装 Django Mac 下安装 Django介绍 Django 是一个由 Pyt ...

  4. window下zookeeper的下载启动和报错等问题

    在使用dubbo等需要用到zookeeper,之前window下本地部署,启动一直有问题,后面折腾了下才部署成功,此次记录下来. 将zookeeper下载之后,解压到指定目录即可,无需安装.例如:解压 ...

  5. [转]Docker部署Django由浅入深系列(下): 八步部署Django+Uwsgi+Nginx+MySQL+Redis

    在上篇教程中,我们手动构建了两个容器,一个容器放Django + Uwsgi,另一个容器放Nginx,成功部署了一个简单的Django项目.然而在实际的生产环境中,我们往往需要定义数量庞大的 dock ...

  6. 通过nginx在window下部署项目

    1-安装nginx 首先,进入nginx 的官网,下载nginx,安装nginx 通过,https://nginx.org/ 链接进入 Mainline version:开发版,mainline 目前 ...

  7. Django+Linux+Uwsgi+Nginx项目部署文档

    Django+Linux+Uwsgi+Nginx项目部署文档 WSGI 在生产环境中使用WSGI作为python web的服务器 WSGI:全拼为Python Web服务器网关接口,Python We ...

  8. pythonmysql部署_详解centos7+django+python3+mysql+阿里云部署项目全流程

    (PS:本文假设你已经在本地联调好django和客户端,只是需要将django部署到外网) 购买阿里云服务器 到[阿里云官网],选择轻量应用服务器, 步骤如图所示: 地域随便选择哪一个,镜像的话,对比 ...

  9. windows系统搭建portal服务器,Windows下安装部署OpenPortal1.1

    <Windows下安装部署OpenPortal1.1>由会员分享,可在线阅读,更多相关<Windows下安装部署OpenPortal1.1(24页珍藏版)>请在人人文库网上搜索 ...

  10. window下配置nginx 及虚拟主机

    Nginx ("engine x") 是一款高性能的,轻量级的HTTP Web 服务器 和 反向代理服务器及电子邮件 IMAP/POP3/SMTP 代理服务器. Nginx 是由俄 ...

最新文章

  1. 和12岁小同志搞创客开发:手撕代码,做一款声控灯
  2. WPF 动态模拟CPU 使用率曲线图
  3. 宽字符集(unicode)说明以及转换函数
  4. 怎样让elementui表格里面的文字不换行,溢出隐藏,tooltip显示内容
  5. Linux字符设备驱动框架
  6. mysql组合索最左_MySQL组合索引和最左匹配原则
  7. Linux Perl 升级
  8. demo 网络运维_网络运维简介
  9. python ftplib模块_python:ftplib模块
  10. 【python 11】super()
  11. 祝贺王远当选为中国区第二位 Teams MVP
  12. linux malloc core,Linux上使用的malloc版本
  13. ArrayList和CopyOnWriteArrayList
  14. 整天说Code Review重要,你知道应该关注哪些关键点吗?
  15. 进击的雨燕------------错误处理
  16. python植物大战僵尸脚本_用 Python 实现植物大战僵尸代码!
  17. matlab arma 仿真,基于Matlab的ARMA模型时间序列分析法仿真
  18. eXosip注册函数与使用说明
  19. 职称计算机ppt2007易错题,职称计算机考试xcel2007模块难题通关技巧.doc
  20. grok java_Java Grok.match方法代码示例

热门文章

  1. (3)Matplotlib_subplot, subplots
  2. ECMAScript 学习笔记01
  3. 第三章 网络体系结构作业
  4. iview的select联动_render函数渲染的iview中的Select组件如何联动?
  5. 配置的代理服务器未响应 电脑连不上网_手机、电脑为什么连不上网(断网)?...
  6. word图表自动编号
  7. Python进行特征提取
  8. 详解基于图卷积的半监督学习(附代码)
  9. taro 微信公众号_Taro 如何开始微信小程序的开发
  10. 2021-08-09 idea -- Mysql 增删改查