国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

詳解通過Nginx部署Django(基于ubuntu)

2024-08-30 12:28:28
字體:
來源:轉載
供稿:網友

Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。

在這種方式中,我們的通常做法是,將nginx作為服務器最前端,它將接收WEB的所有請求,統一管理請求。nginx把所有靜態請求自己來處理(這是NGINX的強項)。然后,NGINX將所有非靜態請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。

可見,uwsgi的作用就類似一個橋接器。起到橋梁的作用。

Linux的強項是用來做服務器,所以,下面的整個部署過程我們選擇在Ubuntu下完成。

一、安裝Nginx                                                                       

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like 協議下發行。其特點是占有內存少,并發能力強,事實上nginx的并發能力確實在同類型的網頁服務器中表現較好。

Nginx同樣為當前非常流行的web服務器。利用其部署Django,我們在此也做簡單的介紹。

Nginx官網:http://nginx.org/

打開ubuntu控制臺(ctrl+alt+t)利用Ubuntu的倉庫安裝。

fnngj@ubuntu:~$ sudo apt-get install nginx #安裝

啟動Nginx:

fnngj@ubuntu:~$ /etc/init.d/nginx start #啟動fnngj@ubuntu:~$ /etc/init.d/nginx stop #關閉fnngj@ubuntu:~$ /etc/init.d/nginx restart #重啟

修改Nginx默認端口號,打開/etc/nginx/nginx.conf 文件,修改端口號。

 server {  listen    8088;  # 修改端口號  server_name localhost;  #charset koi8-r;   #access_log logs/host.access.log main;  location / {    root  html;    index index.html index.htm;  }

大概在文件36行的位置,將默認的80端口號改成其它端口號,如 8088。因為默認的80端口號很容易被其它應用程序占用。

然后,通過上面命令重啟nginx。訪問:http://127.0.0.1:8088/

django部署到nginx,nginx部署django項目,django,nginx,部署  

如果出現如上圖,說明Nginx啟動成功。 

二、安裝uwsgi                                                           

 通過pip安裝uwsgi。

root@ubuntu:/etc# python3 -m pip install uwsgi

測試uwsgi,創建test.py文件:

def application(env, start_response):  start_response('200 OK', [('Content-Type','text/html')])  return [b"Hello World"]

通過uwsgi運行該文件。

fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py

django部署到nginx,nginx部署django項目,django,nginx,部署

接下來配置Django與uwsgi連接。此處,假定的我的django項目位置為:/home/fnngj/pydj/myweb

 

復制代碼 代碼如下:

fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

 

常用選項:

http : 協議類型和端口號

processes : 開啟的進程數量

workers : 開啟的進程數量,等同于processes(官網的說法是spawn the specified number ofworkers / processes)

chdir : 指定運行目錄(chdir to specified directory before apps loading)

wsgi-file : 載入wsgi-file(load .wsgi file)

stats : 在指定的地址上,開啟狀態服務(enable the stats server on the specified address)

threads : 運行線程。由于GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)

master : 允許主進程存在(enable master process)

daemonize : 使進程在后臺運行,并將日志打到指定的日志文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。

pidfile : 指定pid文件的位置,記錄主進程的pid號。

vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)

三、Nginx+uwsgi+Django                                        

接下來,我們要將三者結合起來。首先羅列一下項目的所需要的文件:

myweb/├── manage.py├── myweb/│  ├── __init__.py│  ├── settings.py│  ├── urls.py│  └── wsgi.py└── myweb_uwsgi.ini

在我們通過Django創建myweb項目時,在子目錄myweb下已經幫我們生成的 wsgi.py文件。所以,我們只需要再創建myweb_uwsgi.ini配置文件即可,當然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置。

# myweb_uwsgi.ini file[uwsgi]# Django-related settingssocket = :8000# the base directory (full path)chdir      = /home/fnngj/pydj/myweb# Django s wsgi filemodule     = myweb.wsgi# process-related settings# mastermaster     = true# maximum number of worker processesprocesses    = 4# ... with appropriate permissions - may be needed# chmod-socket  = 664# clear environment on exitvacuum     = true

這個配置,其實就相當于在上一小節中通過wsgi命令,后面跟一堆參數的方式,給文件化了。

socket  指定項目執行的端口號。

chdir   指定項目的目錄。

module  myweb.wsgi ,可以這么來理解,對于myweb_uwsgi.ini文件來說,與它的平級的有一個myweb目錄,這個目錄下有一個wsgi.py文件。

其它幾個參數,可以參考上一小節中參數的介紹。

接下來,切換到myweb項目目錄下,通過uwsgi命令讀取myweb_uwsgi.ini文件啟動項目。

fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini [uWSGI] getting INI configuration from myweb_uwsgi.ini*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2016] ***compiled with version: 4.8.4 on 26 January 2016 06:14:41os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015nodename: ubuntumachine: i686clock source: unixdetected number of CPU cores: 2current working directory: /home/fnngj/pydj/mywebdetected binary path: /usr/local/bin/uwsgi!!! no internal routing support, rebuild with pcre support !!!chdir() to /home/fnngj/pydj/mywebyour processes number limit is 15962your memory page size is 4096 bytesdetected max file descriptor number: 1024lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with --thunder-lock)uwsgi socket 0 bound to TCP address :8000 fd 3Python version: 3.4.3 (default, Oct 14 2015, 20:37:06) [GCC 4.8.4]*** Python threads support is disabled. You can enable it with --enable-threads ***Python main interpreter initialized at 0x8b52dc0your server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 319920 bytes (312 KB) for 4 cores*** Operational MODE: preforking ***WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)*** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 7158)spawned uWSGI worker 1 (pid: 7160, cores: 1)spawned uWSGI worker 2 (pid: 7161, cores: 1)spawned uWSGI worker 3 (pid: 7162, cores: 1)spawned uWSGI worker 4 (pid: 7163, cores: 1)

注意查看uwsgi的啟動信息,如果有錯,就要檢查配置文件的參數是否設置有誤。

再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,添加如下內容。

……server {  listen     8099;   server_name  127.0.0.1   charset UTF-8;  access_log   /var/log/nginx/myweb_access.log;  error_log    /var/log/nginx/myweb_error.log;  client_max_body_size 75M;  location / {     include uwsgi_params;    uwsgi_pass 127.0.0.1:8000;    uwsgi_read_timeout 2;  }    location /static {    expires 30d;    autoindex on;     add_header Cache-Control private;    alias /home/fnngj/pydj/myweb/static/;   } }……

 listen 指定的是nginx代理uwsgi對外的端口號。

server_name  網上大多資料都是設置的一個網址(例,www.example.com),我這里如果設置成網址無法訪問,所以,指定的到了本機默認ip。

在進行配置的時候,我有個問題一直想不通。nginx到底是如何uwsgi產生關聯。現在看來大概最主要的就是這兩行配置。

include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;

include 必須指定為uwsgi_params;而uwsgi_pass指的本機IP的端口與myweb_uwsgi.ini配置文件中的必須一直。

現在重新啟動nginx,翻看上面重啟動nginx的命令。然后,訪問:http://127.0.0.1:8099/

通過這個IP和端口號的指向,請求應該是先到nginx的。如果你在頁面上執行一些請求,就會看到,這些請求最終會轉到uwsgi來處理。

django部署到nginx,nginx部署django項目,django,nginx,部署

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阆中市| 灵山县| 太仓市| 循化| 广灵县| 黑河市| 西城区| 正阳县| 吴堡县| 钟祥市| 莒南县| 衡南县| 綦江县| 武汉市| 临海市| 曲阜市| 清涧县| 丰镇市| 化隆| 金秀| 开阳县| 望江县| 土默特左旗| 兖州市| 手机| 灵丘县| 广汉市| 栾城县| 高淳县| 新疆| 怀宁县| 西城区| 龙陵县| 铜梁县| 巴彦县| 安顺市| 丹凤县| 历史| 陕西省| 石河子市| 山东省|