zoukankan      html  css  js  c++  java
  • ubuntu apache2 wsgi 部署django

    入题 分为如下几步

    1.安装python

    2.安装django

    3.安装wsgi,如有问题请参照上一篇 ubuntu 编译安装 mod_wsgi

    4.与apache集成这里主要讲这部分

      环境apache 2.4.7 高于此版本适用于此 如果版本不同可以参考此配置,但是出现问题可以尝试其他

      1.在apache的模块里添加wsgi

        我这里是在 /etc/apache2/mods-available添加文件

        wsgi.load 内容如下:

        LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

      2.sudo a2enmod wsgi

        此步提示 mod_wsgi 不存在

        网上搜索之得到答案如下:

        执行:sudo apt-get install libapache2-mod-wsgi

        再次执行sudo a2enmod wsgi

        这时候由于我手动新建文件了 会提示是否覆盖选择是就可以了 这个时候在mods-available 时候会增加两个文件wsgi.load 与wsgi.conf

      3.配置wsgi

        

    """
    WSGI config for testdjproject project.
    
    It exposes the WSGI callable as a module-level variable named ``application``.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
    """
    
    import os
    import sys
    path = '/usr/bin/testdjproject'
    if path not in sys.path:
        sys.path.append('/usr/bin/testdjproject')
        sys.path.append('/usr/bin/testdjproject/testdjproject')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testdjproject.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

    这里需要将网站路径添加至系统路径里。可能出现的错误以及疑点如下

          1.No module named testdjproject.settings 这个错误是由于在下文配置的wsgi.py里面没有将路径添加进去 导致找不到这个模块

          2.要在setting.py里添加对127.0.0.1以及localhost的访问如下:

            ALLOWED_HOSTS = ['127.0.0.1','localhost']

          3.为什么这里是 testdj.setting这个与网站名称一样的

          4.路径后面一定要准确 不需要的参数就不要加了

      4.配置apache2

        由于我这个其他网站是在000-default的 为了拿出来 方便排查 新建了django.conf 

        内容如下:

        

    <VirtualHost *:90>
        Alias /media/ /usr/bin/testdjproject/media/
        <Directory /usr/bin/testdjproject/media/>
            #Order deny,allow
            #Allow from all
            Require all granted
        </Directory>
        WSGIScriptAlias / /usr/bin/testdjproject/testdjproject/wsgi.py
        #WSGIPythonPath /usr/bin/testdjproject/testdjproject 
        <Directory /usr/bin/testdjproject/testdjproject>
            Options FollowSymLinks
    #AllowOverride all
    ### Order deny,allow
    ### Deny from all
    #Order allow,deny
    #Allow from all
    #Satisfy all
        #    options -MultiViews
    Require all granted
            <Files wsgi.py>
                Order deny,allow
                Allow from all
            </Files>
        </Directory>
    </VirtualHost>

     这里有几个问题 1.如果配置完成之后提示 client denied by server configuration:这时候可能与Require all granted 有关可以自己排查

             2.Invalid command 'WSGIScriptAlias' 提示这个 也就是wsgi模块没有安装正确 请参照上述

             3.WSGIScriptAlias / /usr/bin/testdjproject/testdjproject/wsgi.py 这个文件是新建的在网站根目录下设置即可

             4.注意wsgi.py的权限,如果权限不对也会出错,这时候查看apache2的错误日志就可以 我这里是/var/log/apache2/error.log

      5.重启apache 打开浏览器 就会出现你之前开发好的页面啦至此一切正常。不一定能匹配所有错误,如果有问题请仔细看日志再做。开始也是安装好之后什么都没有,原来是忘记加监听端口了这一步还是不能少  Listen 89

        

  • 相关阅读:
    [CSP-S模拟测试]:影子(并查集+LCA)
    [CSP-S模拟测试]:夜鹰与玫瑰(数学)
    [CSP-S模拟测试]:抛硬币(DP)
    [CSP-S模拟测试]:影魔(树状数组+线段树合并)
    [CSP-S模拟测试]:队长快跑(DP+离散化+线段树)
    [CSP-S模拟测试]:玄学题/c(数学)
    [CSP-S模拟测试]:卡常题/b(基环树+DP)
    [CSP-S模拟测试]:工业题/a(数学)
    BZOJ5297 [Cqoi2018]社交网络 【矩阵树定理】
    BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
  • 原文地址:https://www.cnblogs.com/EncryptingLife/p/4833228.html
Copyright © 2011-2022 走看看