Django 部署(Nginx)

Django 部署(Nginx)

https://github.com/django/django

https://code.ziqiangxuetang.com/django/django-nginx-deploy.html

https://www.djangoproject.com/download/

==================

【环境】

Ubuntu18

==================

基础知识储备

当我们发现用浏览器不能访问的时候,我们需要一步步排查问题。

整个部署的链路是 Nginx -> uWSGI -> Python Web程序,通常还会提到supervisord工具。

uWSGI是一个软件,部署服务的工具,了解整个过程,我们先了解一下WSGI规范,uwsgi协议等内容。

WSGI(Web Server Gateway Interface)规范,WSGI规定了Python Web应用和Python Web服务器之间的通讯方式。

目前主流的Python Web框架,比如Django,Flask,Tornado等都是基于这个规范实现的。

uwsgi协议是uWSGI工具独有的协议,简洁高效的uwsgi协议是选择uWSGI作为部署工具的重要理由之一,详细的 uwsgi协议 可以参考uWSGI的文档。

uWSGI是 实现了uwsgi协议,WSGI规范和HTTP协议的 一个C语言实现的软件。

Nginx是一个Web服务器,是一个反向代理工具,我们通常用它来部署静态文件。主流的Python Web开发框架都遵循WSGI规范。

uWSGI通过WSGI规范和我们编写的服务进程通讯,然后通过自带的高效的 uwsgi 协议和 Nginx进行通讯,最终Nginx通过HTTP协议将服务对外透出。

当一个访问进来的时候,首先到 Nginx,Nginx会把请求(HTTP协议)转换uwsgi协议传递给uWSGI,uWSGI通过WSGI和web server进行通讯取到响应结果,再通过uwsgi协议发给Nginx,最终Nginx以HTTP协议发现响应给用户。

有些同学可能会说,uWSGI不是支持HTTP协议么,也支持静态文件部署,我不用Nginx行不行?

当然可以,这么做没问题,但目前主流的做法是用Nginx,毕竟它久经考验,更稳定,当然也更值得我们信赖。

supervisor 是一个进程管理工具。任何人都不能保证程序不异常退出,不别被人误杀,所以一个典型的工程做法就是使用supervisor看守着你的进程,一旦异常退出它会立马进程重新启动起来。

如果服务部署后出现异常,不能访问。我们需要分析每一步有没有问题,这时候就不得不用到Linux中一些命令。

======================================================

Django is a high-level Python Web framework that encourages rapid development

and clean, pragmatic design. Thanks for checking it out.

All documentation is in the "``docs``" directory and online at

https://docs.djangoproject.com/en/stable/. If you're just getting started,

here's how we recommend you read the docs:

* First, read ``docs/intro/install.txt`` for instructions on installing Django.

* Next, work through the tutorials in order (``docs/intro/tutorial01.txt``,

  ``docs/intro/tutorial02.txt``, etc.).

* If you want to set up an actual deployment server, read

  ``docs/howto/deployment/index.txt`` for instructions.

* You'll probably want to read through the topical guides (in ``docs/topics``)

  next; from there you can jump to the HOWTOs (in``docs/howto``) for specific

  problems, and check out the reference (``docs/ref``) for gory details.

* See ``docs/README`` for instructions on building an HTML version of the docs.

Docs are updated rigorously. If you find any problems in the docs, or think

they should be clarified in any way, please take 30 seconds to fill out a

ticket here: https://code.djangoproject.com/newticket

To get more help:

* Join the ``#django`` channel on irc.freenode.net. Lots of helpful people hang

  out there. Seehttps://en.wikipedia.org/wiki/Wikipedia:IRC/Tutorial if you're

  new to IRC.

* Join the django-users mailing list, or read the archives, at

  https://groups.google.com/group/django-users.

To contribute to Django:

* Check out https://docs.djangoproject.com/en/dev/internals/contributing/ for

  information about getting involved.

To run Django's test suite:

* Follow the instructions in the "Unit tests" section of

  ``docs/internals/contributing/writing-code/unit-tests.txt``, published online at

  https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/#running-the-unit-tests

======================================================

/var/www/django/docs/intro$ vi install.txt 

===================

Quick install guide

===================

Before you can use Django, you'll need to get it installed. We have a

:doc:`complete installation guide </topics/install>` that covers all the

possibilities; this guide will guide you to a simple, minimal installation

that'll work while you walk through the introduction.

Install Python

==============

Being a Python Web framework, Django requires Python. See

:ref:`faq-python-version-support` for details. Python includes a lightweight

database called SQLite_ so you won't need to set up a database just yet.

.. _sqlite: https://sqlite.org/

Get the latest version of Python at https://www.python.org/downloads/ or with

your operating system's package manager.

You can verify that Python is installed by typing ``python`` from your shell;

you should see something like::

    Python 3.x.y

    [GCC 4.x] on linux

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

    >>>

Set up a database

=================

This step is only necessary if you'd like to work with a "large" database engine

like PostgreSQL, MariaDB, MySQL, or Oracle. To install such a database, consult

the :ref:`database installation information <database-installation>`.

Install Django

==============

You've got three easy options to install Django:

* :ref:`Install an official release <installing-official-release>`. This

  is the best approach for most users.

* Install a version of Django :ref:`provided by your operating system

  distribution `.

* :ref:`Install the latest development version

  `. This option is for enthusiasts who want

  the latest-and-greatest features and aren't afraid of running brand new code.

  You might encounter new bugs in the development version, but reporting them

  helps the development of Django. Also, releases of third-party packages are

  less likely to be compatible with the development version than with the

  latest stable release.

.. admonition:: Always refer to the documentation that corresponds to the

    version of Django you're using!

    If you do either of the first two steps, keep an eye out for parts of the

    documentation marked **new in development version**. That phrase flags

    features that are only available in development versions of Django, and

    they likely won't work with an official release.

Verifying

=========

To verify that Django can be seen by Python, type ``python`` from your shell.

Then at the Python prompt, try to import Django:

.. parsed-literal::

    >>> import django

    >>> print(django.get_version())

    |version|

You may have another version of Django installed.

That's it!

==========

That's it -- you can now :doc:`move onto the tutorial </intro/tutorial01>`.

======================================================

【安装】Python3

$ sudo apt-get install python3

因此需要将Python2与Python3完美切换。

切换方法如下:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 200

查看Python的默认启动项

$ sudo update-alternatives --config python

根据数字选择你想要的Python版本。这时,Python及其对应的pip都跟着变成默认的了。

======================================================

$ cd /var/www/django

$ python manage.py runserver 127.0.0.1:8000

======================================================

【安装】Django

/var/www/django$ vi INSTALL 

Thanks for downloading Django.


To install it, make sure you have Python 3.6 or greater installed. Then run

this command from the command prompt:

    python setup.py install

If you're upgrading from a previous version, you need to remove it first.

For more detailed instructions, see docs/intro/install.txt.

【安装】pip

https://pip.pypa.io/en/latest/installing/#installing-with-get-pip-py

$ sudo apt-get install python-pip

或者

$ sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

【升级】pip

$ sudo pip install --upgrade pip

【安装】 Django

$ git clone https://github.com/django/django.git

【检查】Django是否安装成功

终端上输入 

$ python

>>> import django

>>> django.VERSION

3.0

======================================================

【安装】nginx

$ sudo apt-get install python-dev

$ sudo apt-get install nginx

【安装】supervisor

 supervisor,一个专门用来管理进程的工具,来管理 uwsgi 进程。

$ sudo pip install supervisor

======================================================

使用 uWSGI 部署

【安装】uwsgi

$ sudo pip install uwsgi --upgrade

使用 uwsgi 运行项目

$ uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi

--home 指定virtualenv 路径,如果没有可以去掉。project.wsgi 指的是 project/wsgi.py 文件。

======================================================

使用【supervisor】来管理进程

【安装】supervisor

$ sudo pip install supervisor

生成 supervisor 默认配置文件,比如我们放在 /etc/supervisord.conf 路径中:

$ sudo echo_supervisord_conf > /etc/supervisord.conf

打开 supervisor.conf 在最底部添加(每一行前面不要有空格,防止报错):

[program:X]

command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi

directory=/path/to/zqxt

startsecs=0

stopwaitsecs=0

autostart=true

autorestart=true

command 中写上对应的命令,这样,就可以用 supervisor 来管理了。

【启动】supervisor

$ sudo supervisord -c /etc/supervisord.conf

【重启】django程序(项目)

$ sudo supervisorctl -c /etc/supervisord.conf restart django

======================================================

【配置】Nginx

新建一个网站 django

$ sudo vi /etc/nginx/sites-available/django.conf

写入以下内容:

server {

    listen      80;

    server_name django.com;

    charset     utf-8;


    client_max_body_size 75M;


    location /media  {

        alias /var/www/django;

    }


    location /static {

        alias /var/www/django;

    }


    location / {

        uwsgi_pass  unix:///home/tu/zqxt/zqxt.sock;

        include     /etc/nginx/uwsgi_params;

    }

}

【激活】Nginx虚拟机

$ sudo ln-s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled/django.conf

测试配置语法问题

sudo nginx -t

重启 nginx 服务器

sudo service nginx reload

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342