Django 部署(Nginx)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末韩容,一起剝皮案震驚了整個(gè)濱河市宙攻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌座掘,老刑警劉巖溢陪,帶你破解...
    沈念sama閱讀 212,718評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異杉编,居然都是意外死亡咆霜,警方通過查閱死者的電腦和手機(jī)蛾坯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評論 3 385
  • 文/潘曉璐 我一進(jìn)店門脉课,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人倘零,你說我怎么就攤上這事呈驶。” “怎么了跌穗?”我有些...
    開封第一講書人閱讀 158,207評論 0 348
  • 文/不壞的土叔 我叫張陵虏辫,是天一觀的道長砌庄。 經(jīng)常有香客問我,道長佩微,這世上最難降的妖魔是什么萌焰? 我笑而不...
    開封第一講書人閱讀 56,755評論 1 284
  • 正文 為了忘掉前任扒俯,我火速辦了婚禮,結(jié)果婚禮上撼玄,老公的妹妹穿的比我還像新娘掌猛。我一直安慰自己,他們只是感情好废膘,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評論 6 386
  • 文/花漫 我一把揭開白布慕蔚。 她就那樣靜靜地躺著坊萝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪菩鲜。 梳的紋絲不亂的頭發(fā)上惦积,一...
    開封第一講書人閱讀 50,050評論 1 291
  • 那天狮崩,我揣著相機(jī)與錄音蛛勉,去河邊找鬼睦柴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛坦敌,可吹牛的內(nèi)容都是我干的痢法。 我是一名探鬼主播财搁,決...
    沈念sama閱讀 39,136評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼躬络,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了提茁?” 一聲冷哼從身側(cè)響起膘滨,我...
    開封第一講書人閱讀 37,882評論 0 268
  • 序言:老撾萬榮一對情侶失蹤火邓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后铲咨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,330評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坯苹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評論 2 327
  • 正文 我和宋清朗相戀三年粹湃,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了为鳄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腕让。...
    茶點(diǎn)故事閱讀 38,789評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纯丸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出觉鼻,到底是詐尸還是另有隱情,我是刑警寧澤滑凉,帶...
    沈念sama閱讀 34,477評論 4 333
  • 正文 年R本政府宣布畅姊,位于F島的核電站咒钟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏若未。R本人自食惡果不足惜朱嘴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望粗合。 院中可真熱鬧萍嬉,春花似錦、人聲如沸隙疚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽供屉。三九已至行冰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間悼做,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評論 1 267
  • 我被黑心中介騙來泰國打工哗魂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肛走,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,598評論 2 362
  • 正文 我出身青樓录别,卻偏偏與公主長得像朽色,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子组题,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評論 2 351