Django 部署(Nginx)
https://github.com/django/django
https://code.ziqiangxuetang.com/django/django-nginx-deploy.html
https://www.djangoproject.com/download/
==================
【環(huán)境】
Ubuntu18
==================
基礎(chǔ)知識儲備
當(dāng)我們發(fā)現(xiàn)用瀏覽器不能訪問的時(shí)候,我們需要一步步排查問題衔肢。
整個(gè)部署的鏈路是 Nginx -> uWSGI -> Python Web程序豁翎,通常還會提到supervisord工具。
uWSGI是一個(gè)軟件邦尊,部署服務(wù)的工具刘陶,了解整個(gè)過程匙隔,我們先了解一下WSGI規(guī)范熏版,uwsgi協(xié)議等內(nèi)容。
WSGI(Web Server Gateway Interface)規(guī)范再膳,WSGI規(guī)定了Python Web應(yīng)用和Python Web服務(wù)器之間的通訊方式曲横。
目前主流的Python Web框架禾嫉,比如Django,F(xiàn)lask艳吠,Tornado等都是基于這個(gè)規(guī)范實(shí)現(xiàn)的孽椰。
uwsgi協(xié)議是uWSGI工具獨(dú)有的協(xié)議凛篙,簡潔高效的uwsgi協(xié)議是選擇uWSGI作為部署工具的重要理由之一栏渺,詳細(xì)的 uwsgi協(xié)議 可以參考uWSGI的文檔。
uWSGI是 實(shí)現(xiàn)了uwsgi協(xié)議削彬,WSGI規(guī)范和HTTP協(xié)議的 一個(gè)C語言實(shí)現(xiàn)的軟件秀仲。
Nginx是一個(gè)Web服務(wù)器,是一個(gè)反向代理工具神僵,我們通常用它來部署靜態(tài)文件。主流的Python Web開發(fā)框架都遵循WSGI規(guī)范沛励。
uWSGI通過WSGI規(guī)范和我們編寫的服務(wù)進(jìn)程通訊炮障,然后通過自帶的高效的 uwsgi 協(xié)議和 Nginx進(jìn)行通訊胁赢,最終Nginx通過HTTP協(xié)議將服務(wù)對外透出。
當(dāng)一個(gè)訪問進(jìn)來的時(shí)候谅摄,首先到 Nginx系馆,Nginx會把請求(HTTP協(xié)議)轉(zhuǎn)換uwsgi協(xié)議傳遞給uWSGI由蘑,uWSGI通過WSGI和web server進(jìn)行通訊取到響應(yīng)結(jié)果,再通過uwsgi協(xié)議發(fā)給Nginx爷狈,最終Nginx以HTTP協(xié)議發(fā)現(xiàn)響應(yīng)給用戶谓媒。
有些同學(xué)可能會說,uWSGI不是支持HTTP協(xié)議么土辩,也支持靜態(tài)文件部署,我不用Nginx行不行各墨?
當(dāng)然可以启涯,這么做沒問題结洼,但目前主流的做法是用Nginx,畢竟它久經(jīng)考驗(yàn)蒸殿,更穩(wěn)定鸣峭,當(dāng)然也更值得我們信賴摊溶。
supervisor?是一個(gè)進(jìn)程管理工具。任何人都不能保證程序不異常退出霞玄,不別被人誤殺浓镜,所以一個(gè)典型的工程做法就是使用supervisor看守著你的進(jìn)程膛薛,一旦異常退出它會立馬進(jìn)程重新啟動(dòng)起來补鼻。
如果服務(wù)部署后出現(xiàn)異常,不能訪問咨跌。我們需要分析每一步有沒有問題硼婿,這時(shí)候就不得不用到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的默認(rèn)啟動(dòng)項(xiàng)
$ sudo update-alternatives --config python
根據(jù)數(shù)字選擇你想要的Python版本碗硬。這時(shí)瓢颅,Python及其對應(yīng)的pip都跟著變成默認(rèn)的了挽懦。
======================================================
$ 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巾兆,一個(gè)專門用來管理進(jìn)程的工具,來管理 uwsgi 進(jìn)程蔫磨。
$?sudo pip?install supervisor
======================================================
使用 uWSGI 部署
【安裝】uwsgi
$?sudo pip?install uwsgi?--upgrade
使用 uwsgi 運(yùn)行項(xiàng)目
$?uwsgi?--http?:8001?--chdir?/path/to/project --home=/path/to/env --module?project.wsgi
--home 指定virtualenv 路徑堤如,如果沒有可以去掉窒朋。project.wsgi 指的是 project/wsgi.py 文件。
======================================================
使用【supervisor】來管理進(jìn)程
【安裝】supervisor
$?sudo pip?install?supervisor
生成 supervisor 默認(rèn)配置文件榔至,比如我們放在 /etc/supervisord.conf 路徑中:
$?sudo echo_supervisord_conf?>?/etc/supervisord.conf
打開 supervisor.conf 在最底部添加(每一行前面不要有空格欺劳,防止報(bào)錯(cuò)):
[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 中寫上對應(yīng)的命令划提,這樣,就可以用 supervisor 來管理了淡诗。
【啟動(dòng)】supervisor
$ sudo supervisord?-c?/etc/supervisord.conf
【重啟】django程序(項(xiàng)目)
$ sudo supervisorctl?-c?/etc/supervisord.conf?restart django
======================================================
【配置】Nginx
新建一個(gè)網(wǎng)站 django
$ sudo vi /etc/nginx/sites-available/django.conf
寫入以下內(nèi)容:
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虛擬機(jī)
$ sudo ln-s?/etc/nginx/sites-available/django.conf?/etc/nginx/sites-enabled/django.conf
測試配置語法問題
sudo nginx -t
重啟 nginx 服務(wù)器
sudo service?nginx?reload