【Django外传】部署 apache2 + ajango 服务

apache2 安装

自行百度

Django 安装

自行百度

Ok我们现在开始部署,首先展示下我的文件目录(其它博文乱七八糟)

apache path:这里写图片描述

Django path: 这里写图片描述

需要注意下,Django 初始目录是有上级目录的

首先,完模来到Apache2 的 目录里面

cd sites-available 进入目录 新建一个 conf 配置文件
内容如下:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName andire.com

        ServerAdmin webmaster@localhost
#       DocumentRoot /var/www/document/web_debug/webs/

        Alias /static/ /var/www/document/web_debug/webs/static/ #这里是静态文件路径,目前暂时没有用到,不过先配置下,后面再看看怎么用

        #这里配置下你Django的工程目录和你 wsgi.py 同级
        <Directory /var/www/document/web_debug/webs/HelloWorld/HelloWorld>
                <Files wsgi.py>
                        Order deny,allow
                        Allow from all
                Files>
        Directory>
        #这里配置你wsgi.py 文件路径  第一个 / 两边都有空格 表示从root目录开始(具体规则没详细了解)
        WSGIScriptAlias / /var/www/document/web_debug/webs/HelloWorld/HelloWorld/wsgi.py
        #后面默认就好
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
VirtualHost>

然后 : sudo a2ensite + xxx.conf 配置文件
如果出现提示什么语法错误接着下一行提示 :Invalid command ‘WSGIScriptAlias’, perhaps misspelled or defined by a module not included in the ser

运行下 sudo a2enmod wsgi 如果提示 : ERROR: Module mod-wsgi does not exist! 那就证明没有安装wsgi
安装 libapache2-mod-wsgi
sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
再执行 sudo a2ensite + xxx.conf 配置文件 (记得先把 000-defult.conf 关掉,后面多站点,看需求)
ps(关闭 000-defult.conf ) 执行 sudo a2enssite 000-defult.conf

然后来到你Django目录下的 wsgi.py 文件
修改如下

import os
import sys

from os.path import dirname,abspath
from django.core.wsgi import get_wsgi_application


PROJECT_DIR = dirname(dirname(abspath(__file__)))

sys.path.insert(0, PROJECT_DIR)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HelloWorld.settings")
application = get_wsgi_application()

ok 去重启下你的服务器把。

对了重启之前记得回到你Django工程目录给下权限

这里写图片描述

好了,去重启吧,怎么重启apache 请自行百度。

你可能感兴趣的:(自述)