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