zoukankan      html  css  js  c++  java
  • Django在Window下的部署

     转载 : 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

    安装路径: D:Python27 (example)

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

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

    C:UsersUser1>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:Python27Libsite-packagesdjango

    ….

    byte-compiling d:Python27Libsite-packagesdjangoviewsi18n.py to i18n.pyc

    byte-compiling d:Python27Libsite-packagesdjangoviewsstatic.py to static.py

    ……

    byte-compiling d:Python27Libsite-packagesdjangoviews\__init__.py to __init__.pyc

    byte-compiling d:Python27Libsite-packagesdjango\__init__.py to __init__.pyc

    running install_scripts

    creating d:Python27Scripts

    copying buildscripts-2.7django-admin.py -> d:Python27Scripts

    running install_egg_info

    Writing d:Python27Libsite-packagesDjango-1.5.5-py2.7.egg-info

    把D:Python27Libsite-packages加入环境变量path

    把D:Python27Script加入环境变量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 FilesApache Software FoundationApache2.2modules 组件位置,需要加载的组件放在该路径

    d:Program FilesApache Software FoundationApache2.2conf httpd.conf 最重要的配置文件

    D:Program FilesApache Software FoundationApache2.4logs 相关错误打印可以在下面文件找到

    免安装版

    下载

    直接从官网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:PythonWebSWhttpd-2.2.19-win64in>httpd -k install

    有的时候会报错

    D:httpd-2.2.19-win64in>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:PythonWebSWhttpd-2.2.19-win64in>start httpd

    卸载

    D:PythonWebSWhttpd-2.2.19-win64in>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 FilesApache Software FoundationApache2.2modules , 并在httpd.conf里添加

    LoadModule wsgi_module modules/mod_wsgi.so

    httpd.conf的位置在d:Program FilesApache Software FoundationApache2.2conf

    注意: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.

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

  • 相关阅读:
    hdu 3085
    hdu 3295 模拟过程。数据很水
    hdu 2181 水搜索
    pku ppt some problem
    2-sat
    The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
    nenu contest3
    The 10th Zhejiang Provincial Collegiate Programming Contest
    最小费用最大流
    多源最短路
  • 原文地址:https://www.cnblogs.com/2dogslife/p/8437170.html
Copyright © 2011-2022 走看看