使用nginx和uwsgi將django項(xiàng)目部署到云服務(wù)器

一. 簡(jiǎn)介

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

A Web Server Gateway Interface - WSGI - does this job. WSGI is a Python standard.

二. 組件概覽

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

nginx充當(dāng)the web server形纺,響應(yīng)處理靜態(tài)資源的請(qǐng)求贷币,并將動(dòng)態(tài)資源的請(qǐng)求通過(guò)socket以u(píng)wsgi協(xié)議轉(zhuǎn)發(fā)給uWSG處理搜骡,uWSG則是運(yùn)行django項(xiàng)目的web服務(wù)鱼辙。

三. 具體步驟

逐步搭建,方便排查問(wèn)題谋币。以下步驟若未特殊聲明寨昙,則均在云服務(wù)器上執(zhí)行掠归。

1. virtualenv

方法一:
安裝virtualenv

sudo apt-get install virtualenv

創(chuàng)建和激活virtualenv

virtualenv env --no-site-packages --python=python3
source env/bin/activate

--no-site-packages 表示不使用系統(tǒng)已經(jīng)安裝的第三方庫(kù)
--python指定項(xiàng)目用到的python版本

方法二:
使用python3內(nèi)置venv

$ python3 -m venv envpy3
2. 測(cè)試uwsgi

將uwsgi安裝到virtualenv中。激活virtualenv再執(zhí)行

pip install uwsgi

新建文件test.py并寫(xiě)入以下內(nèi)容

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

運(yùn)行uwsgi

uwsgi --http :8000 --wsgi-file test.py

訪問(wèn)127.0.0.1:8000查看是否成功卢鹦,若成功說(shuō)明以下組件連接成功

the web client <-> uWSGI <-> Python
3. 測(cè)試django項(xiàng)目

在本機(jī)django的項(xiàng)目根目錄執(zhí)行以下命令臀脏,生成項(xiàng)目依賴清單requirements.txt

pip freeze > requirements.txt

用scp命令將django項(xiàng)目傳到云服務(wù)器

 scp [option] <localfile> <username>@<host>:<remote-file-location>
示例:
scp -r /home/user/hello_django user@x.x.x.x:/home/user/

激活virtualenv劝堪,切換到項(xiàng)目根目錄下安裝依賴

pip install -r requirements.txt

運(yùn)行django

python manage.py runserver 0.0.0.0:8000

如果運(yùn)行成功,再測(cè)試使用uwsgi運(yùn)行django

uwsgi --http :8000 --module hello_django.wsgi

--module 指定django模塊
如果成功揉稚,說(shuō)明以下組件連接成功

the web client <-> uWSGI <-> Django
4. 測(cè)試nginx

安裝

sudo apt-get install nginx

啟動(dòng)

sudo systemctl start nginx

nginx默認(rèn)監(jiān)聽(tīng)80端口秒啦,所以可以訪問(wèn)127.0.0.1:80測(cè)試是否成功。
nginx安裝之后搀玖,會(huì)默認(rèn)創(chuàng)建www-data用戶和組余境,并以www-data身份運(yùn)行nginx(可以在/etc/nginx/nginx.conf首行查看或修改)。為了避免文件訪問(wèn)權(quán)限問(wèn)題灌诅,修改django項(xiàng)目的用戶組

sudo chown -R :www-data hello_django
5. 部署靜態(tài)資源

在django項(xiàng)目的settings.py文件添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后執(zhí)行

python manage.py collectstatic

即可將項(xiàng)目的靜態(tài)資源統(tǒng)一收集到項(xiàng)目根目錄下的static文件夾中芳来。

在/etc/nginx/sites-available/目錄下,新建hello_django.conf并寫(xiě)入以下內(nèi)容猜拾,將static路徑替換成上面的項(xiàng)目的static目錄路徑

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste
   
    # 開(kāi)啟gzip
    gzip on;

    # 啟用gzip壓縮的最小文件即舌,小于設(shè)置值的文件將不會(huì)壓縮
    gzip_min_length 1k;

    # gzip 壓縮級(jí)別,1-10挎袜,數(shù)字越大壓縮的越好顽聂,也越占用CPU時(shí)間,后面會(huì)有詳細(xì)說(shuō)明
    gzip_comp_level 2;

    # 進(jìn)行壓縮的文件類型盯仪。javascript有多種形式紊搪。其中的值可以在 mime.types 文件中找到。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

    # 是否在http header中添加Vary: Accept-Encoding全景,建議開(kāi)啟
    gzip_vary on;

    # 禁用IE 6 gzip
    gzip_disable "MSIE [1-6]\.";

    # Django media
#    location /media  {
#        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
#    expires 30d;
#    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
        # enable cache 
        expires 30d; 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

并創(chuàng)建軟鏈接到/etc/nignx/sites-enabled目錄

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

重加載nginx

nginx -s reload

然后訪問(wèn)127.0.0.1:8000/static/xxx/xxx.png耀石,若成功說(shuō)明靜態(tài)資源部署成功。
若訪問(wèn)失敗可查看nginx錯(cuò)誤日志(/var/log/nginx/error.log)爸黄,Permission denied問(wèn)題請(qǐng)將static目錄加入到www-data用戶組娶牌。

6. 連接nginx和uwsgi

以--socket方式運(yùn)行django項(xiàng)目,并將端口改成nginx中配置的8001端口

uwsgi --socket :8001 --module hello_django.wsgi 

然后訪問(wèn)127.0.0.1:8000查看測(cè)試nginx的動(dòng)態(tài)轉(zhuǎn)發(fā)是否成功馆纳。
若成功,則所有組件的連接都成功了

the web client <-> the web server <-> the socket <-> uWSGI <-> Python
7. 使用Unix sockets替換uwsgi的端口

nignx和uwsgi間通過(guò)socket端口連接雖然簡(jiǎn)單汹桦,但是不是最佳方式鲁驶,應(yīng)改用Unix sockets。
修改hello_django.conf

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)

指定使用Unix sockets時(shí)創(chuàng)建的sock文件舞骆,如

server unix:///home/www-data/hello_django/hello_django.sock; # for a file socket

然后改以u(píng)nit socket方式運(yùn)行django

uwsgi --socket /home/www-data/hello_django/hello_django.sock --module hello_django.wsgi --chmod-socket=666

--chmod-socket是為了避免hello_django.sock文件的權(quán)限問(wèn)題
然后訪問(wèn)查看是否成功钥弯。

8. 以.ini文件方式運(yùn)行uwsgi

創(chuàng)建文件 hello_django_uwsgi.ini

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

然后指定文件啟動(dòng)uwsgi

uwsgi --ini hello_django_uwsgi.ini # the --ini option is used to specify a file
9. 將uwsgi配置成service并設(shè)置開(kāi)機(jī)自啟

退出virtualenv

deactivate

將uwsgi安裝到系統(tǒng)環(huán)境

sudo pip3 install uwsgi

然后用系統(tǒng)環(huán)境的uwsgi測(cè)試

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

在/etc/systemd/system目錄,新建文件hello_django.service

[Unit]
Description=hello django uwsgi web service
After=syslog.target

[Service]
Type=simple
ExecStart= /usr/local/bin/uwsgi --ini /path/to/your/hello_django.ini
Restart=always
StandardError=syslog

[Install]
WantedBy=multi-user.target

配置完成后督禽,reload一下systemd脆霎,即成功添加了hello_django的service

$ sudo systemctl daemon-reload

啟動(dòng)

systemclt start hello_django

設(shè)置開(kāi)機(jī)自啟

systemctl enable hello_django

systemd還可以監(jiān)控服務(wù)進(jìn)程,代替supervisor第三方庫(kù)狈惫。

10. 其他

以Emperor模式啟動(dòng)uwsgi睛蛛,該模式下,會(huì)監(jiān)視uwsgi的配置文件,當(dāng)配置文件被修改后忆肾,會(huì)自動(dòng)重啟uwsgi下對(duì)應(yīng)的web服務(wù)荸频。

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

啟動(dòng)

sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

在hello_django.service的配置文件中修改啟動(dòng)的shell命令

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

參考文章
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市客冈,隨后出現(xiàn)的幾起案子旭从,更是在濱河造成了極大的恐慌,老刑警劉巖场仲,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件和悦,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡渠缕,警方通過(guò)查閱死者的電腦和手機(jī)鸽素,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)褐健,“玉大人付鹿,你說(shuō)我怎么就攤上這事⊙裂福” “怎么了舵匾?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)谁不。 經(jīng)常有香客問(wèn)我坐梯,道長(zhǎng),這世上最難降的妖魔是什么刹帕? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任吵血,我火速辦了婚禮,結(jié)果婚禮上偷溺,老公的妹妹穿的比我還像新娘蹋辅。我一直安慰自己,他們只是感情好挫掏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布侦另。 她就那樣靜靜地躺著,像睡著了一般尉共。 火紅的嫁衣襯著肌膚如雪褒傅。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,370評(píng)論 1 302
  • 那天袄友,我揣著相機(jī)與錄音殿托,去河邊找鬼。 笑死剧蚣,一個(gè)胖子當(dāng)著我的面吹牛支竹,可吹牛的內(nèi)容都是我干的旋廷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼唾戚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼柳洋!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起叹坦,我...
    開(kāi)封第一講書(shū)人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤熊镣,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后募书,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體绪囱,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年莹捡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鬼吵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡篮赢,死狀恐怖齿椅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情启泣,我是刑警寧澤涣脚,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站寥茫,受9級(jí)特大地震影響遣蚀,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纱耻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一芭梯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧弄喘,春花似錦玖喘、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至卖漫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赠群,已是汗流浹背羊始。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留查描,地道東北人突委。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓柏卤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親匀油。 傳聞我的和親對(duì)象是個(gè)殘疾皇子缘缚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容