使用uWSGI部署django項目

先說說什么是uWSGI吧,他是實現了WSGI協議、uwsgi、http等協議的一個web服務器,那什么是WSGI呢?

WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx)與應用服務器(如uWSGI服務器)通信的一種規范(協議)。

還有一種wsgi,uwsgi是一種線路協議而不是通信協議,在此常用于在uWSGI服務器與其他網絡服務器的數據通信。uwsgi協議是一個uWSGI服務器自有的協議,它用于定義傳輸信息的類型(type of information)。

?

部署步驟:

1. 安裝uWSGI

(lofter) ?  dj_test git:(master) pip install uwsgi
Collecting uwsgi
/home/wang/.virtualenvs/lofter/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.SNIMissingWarning
/home/wang/.virtualenvs/lofter/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.InsecurePlatformWarningDownloading uwsgi-2.0.13.1.tar.gz (784kB)100% |████████████████████████████████| 788kB 1.4MB/s 
Building wheels for collected packages: uwsgiRunning setup.py bdist_wheel for uwsgi ... doneStored in directory: /home/wang/.cache/pip/wheels/01/e4/de/0b2bbeba234858bd780924d03031a4e817119aafb0cfc4c79e
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.13.1

以上命令是安裝目前穩定版本的,也可以安裝LTS版本

# Or install LTS (long term support).
$ pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

  

2. 安裝之后進行簡單的測試:

2.1 創建一個test.py文件:

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

2.2 啟動uWSGI服務器:

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

意思是使用8000端口啟動這個文件,效果如下:

 

(lofter) ?  dj_test git:(master) uwsgi --http :8000 --wsgi-file test.py
*** Starting uWSGI 2.0.12 (64bit) on [Mon May 23 21:15:56 2016] ***
compiled with version: 4.8.4 on 08 April 2016 10:44:49
os: Linux-3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016
nodename: wang-N46VZ
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /home/wang/Workspace/Git/show-me-the-code/dj_test
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62795
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5539)
uwsgi socket 0 bound to TCP address 127.0.0.1:47320 (port auto-assigned) fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x21b8ff0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x21b8ff0 pid: 5538 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5538, cores: 1)

2.3 訪問 localhost:8000

顯示 hello world(linux截圖太麻煩了,見諒)

這就說明uWSGI啟動成功了~可以設置uWSGI啟動django了

?

3. 使用uWSGI啟動django項目

(lofter) ?  dj_test git:(master) uwsgi --http :8000 --file dj_test/wsgi.py                                                                                              
*** Starting uWSGI 2.0.12 (64bit) on [Mon May 23 21:19:35 2016] ***
compiled with version: 4.8.4 on 08 April 2016 10:44:49
os: Linux-3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016
nodename: wang-N46VZ
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /home/wang/Workspace/Git/show-me-the-code/dj_test
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62795
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5649)
uwsgi socket 0 bound to TCP address 127.0.0.1:60536 (port auto-assigned) fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2263ff0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x2263ff0 pid: 5648 (default app)
*** uWSGI is running in multiple interpreter mode ***

訪問網址,就可以看見你的項目了~

常用命令:

uwsgi --chdir=/path/to/your/project \--module=mysite.wsgi:application \--env DJANGO_SETTINGS_MODULE=mysite.settings \--master --pidfile=/tmp/project-master.pid \--socket=127.0.0.1:49152 \      # can also be a file--processes=5 \                 # number of worker processes--uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges--harakiri=20 \                 # respawn processes taking more than 20 seconds--max-requests=5000 \           # respawn processes after serving 5000 requests--vacuum \                      # clear environment on exit--home=/path/to/virtual/env \   # optional path to a virtualenv--daemonize=/var/log/uwsgi/yourproject.log      # background the process  

?

?

實際上到這里就算部署成功了,不過現實中一般使用nginx和uWSGI配合使用,使用nginx處理/static/以及/media/的請求,其他的交給uWSGI處理。

(下面基本是翻譯,而且還沒有翻譯完

1. 安裝nginx并配置nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start    # start nginx

配置文件(需要去看一下nginx簡單配置)

# mysite_nginx.conf# the upstream component nginx needs to connect to
upstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 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 onlisten      8000;# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /media  {alias /path/to/your/mysite/media;  # your Django project's media files - amend as required}location /static {alias /path/to/your/mysite/static; # your Django project's static files - amend as required}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass  django;include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed}
}

做個鏈接使得nginx可以訪問到配置文件

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

2. 部署靜態文件

python manage.py collectstatic
STATIC_ROOT一定要設置好了

3. 重啟nginx(第一次部署需要重啟,以后進行了第二步之后只需要重啟uWSGI就可以了)

sudo /etc/init.d/nginx restart

4. 啟動

uwsgi --socket :8001 --wsgi-file test.py

5. 添加uWSGI配置文件

# mysite_uwsgi.ini file
[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    = 664
# clear environment on exit
vacuum          = true

按照配置文件啟動

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

6. 使用兩者共同拉起項目

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664

  

If that doesn’t work

Check your nginx error log(/var/log/nginx/error.log). If you see something like:

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)

then probably you need to manage the permissions on the socket so that nginx is allowed to use it.

Try:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)

or:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)

You may also have to add your user to nginx’s group (which is probably www-data), or vice-versa, so that nginx can read and write to your socket properly.

It’s worth keeping the output of the nginx log running in a terminal window so you can easily refer to it while troubleshooting.

?

參考:

http://baike.baidu.com/link?url=ClozvG5bxABaSkxztwURPZAOrySGwwBNMJEiPVOWfv7dPvJwEw_ZYPK3mUn0miC5_YNnHFG27tKvv6B3wktL1K

https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/uwsgi/

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

轉載于:https://www.cnblogs.com/wswang/p/5521566.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395928.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395928.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395928.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

8 包含min函數的棧

0 引言 題目:定義棧的數據結構,請在該類型中實現一個能夠得到棧的最小元素的min函數。在該棧中,調用min、push及pop的時間復雜度都是O(1). 1 抽象問題具體化 2 具體問題抽象分析 需要解決的兩個主要問題如下。 &#x…

《Adobe Illustrator大師班:經典作品與完美技巧賞析》—Svetlana Makarova

本節書摘來自異步社區《Adobe Illustrator大師班:經典作品與完美技巧賞析》一書中的Svetlana Makarova,作者【英】Sharon Milne,更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 Svetlana MakarovaAdobe Illustrator大師班:經典作品與…

navicat無法連接遠程mysql數據庫_navicat無法遠程連接mysql的解決方法

近日在Ubuntu上安裝了一個 MySQL 5.0,因為使用 phpMyAdmin 還必須安裝 PHP,所以打算直接使用遠程管理工具Navicat for MySQL 來連接。在 Ubuntu 中通過 mysql 命令行創建好一個數據表并分配了權限:代碼如下:GRANT ALL ON testdb.* TO usera I…

有關軟件測試的證書,軟件測試證書有用嗎

要想知道證書有什么用,我們就要詳細了解軟件評測師考試,以及拿到證書的價值。那么下面和小編來看看這篇軟件測試證書有用嗎,一定會有收獲。一、證書考試軟件評測師考試是全國計算機技術與軟件技術資格考試的一個中級考試。考試不規定學歷和資…

計算機科學導論第五版_五月份將開始提供438項免費在線編程和計算機科學課程

計算機科學導論第五版Five years ago, universities like MIT and Stanford first opened up free online courses to the public. Today, more than 700 schools around the world have created thousands of free online courses.五年前,麻省理工學院和斯坦福大學…

python D29 socketserver以及FTB

一、socketserver 基于tcp協議下的socket只能和一個客戶端通信,如果用socketserver可以實現和多個客戶端通信。 他是在socket的基礎上進行封裝,也就是說底層還是調用的socket,在py2.7里面叫做SocketServer也就是大寫了兩個S,在py3…

計算機節電模式不能打開,電腦進入節電模式打不開怎么辦

大家好,我是時間財富網智能客服時間君,上述問題將由我為大家進行解答。電腦進入節電模式打不開的原因及解決方法如下:1、顯示器和顯卡接觸不良解決辦法:檢查顯示器和顯卡的連接是否正確,接觸是否良好;2、顯…

sphinx mysql存儲引擎_基于Sphinx+MySQL的千萬級數據全文檢索(搜索引擎)架構設計...

Sphinx,單一索引最大可包含1億條記錄,在1千萬條記錄情況下的查詢速度為0.x秒(毫秒級)。Sphinx創建索引的速度為:創建100萬條記錄的索引只需3~4分鐘,創建1000萬條記錄的索引可以在50分鐘內完成,而只包含最新…

《第一桶金怎么賺——淘寶開店創業致富一冊通》一一1.1 創業者需具備的素質...

本節書摘來自異步社區出版社《第一桶金怎么賺——淘寶開店創業致富一冊通》一書中的第1章,第1.1節,作者:葛存山,更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 1.1 創業者需具備的素質 第一桶金怎么賺——淘寶開店創業致…

4-1 線程安全性-原子性-atomic-1

轉載于:https://www.cnblogs.com/ZHONGZHENHUA/p/10026627.html

構建了我的第一個React Native應用程序之后,我現在確信這是未來。

by Taylor Milliman泰勒米利曼(Taylor Milliman) 構建了我的第一個React Native應用程序之后,我現在確信這是未來。 (After building my first React Native app, I’m now convinced it’s the future.) After a few weeks of playing around with React Native, …

delphi7 提示注冊過期問題

很同情你得經過~ 因為我以前也是經常遇見這個問題~就和你說得一樣~ 后來~ 我發現 下載使用的Delphi 7只能使用一個注冊碼,那就是:6AMD-PKG68E-DB8PP7-9SFE 3QH-9QW所以,你先把C:\Documents and Settings\Administrator\.borland文件夾下的兩個文件刪除然后用 Progra…

計算機開機引導的結果是,電腦開機顯示引導媒體是怎么回事

電腦開機顯示引導媒體是怎么回事分類:數據恢復常見問題|最后更新:2020年4月9日開機顯示重新啟動并選擇適當的引導設備或插入1.如果主機上接有可移動存儲介質(如光盤、移動硬盤、U盤等),將其拔掉,然后重啟。2.如果仍然這樣,進入主板設置中,依次檢測以下幾…

《操作系統真象還原》——0.23 操作系統是如何識別文件系統的

本節書摘來自異步社區《操作系統真象還原》一書中的第0章,第0.23節,作者:鄭鋼著,更多章節內容可以訪問云棲社區“異步社區”公眾號查看 0.23 操作系統是如何識別文件系統的 我們知道,一個硬盤上可以有很多分區&#xf…

mysql怎樣修改my ini_mysql修改my.ini報錯怎么辦

mysql修改my.ini報錯的解決辦法:首先將mysql默認編碼改成utf8mb4,并修改【my.ini】配置;然后修改變量,并檢查是否設置成功即可。更多相關免費學習推薦:mysql教程(視頻)mysql修改my.ini報錯的解決辦法:將mys…

golang---map類型

map 類似其它語言中的哈希表或字典,以key-value形式存儲數據key必須是支持或!比較運算的類型,不可以是函數、map或sliceMap查找比線性搜索快很多,但比使用索引訪問數據的類型慢100倍 Map使用make()創建,支持:這種簡寫方式 make([k…

易語言程序應用程序錯誤退出_為什么我退出Google并構建了一個向孩子們教授個人理財的應用程序

易語言程序應用程序錯誤退出Many of my friends thought I was crazy to leave a great position at Google to help parents and kids learn about money. Maybe they’re right.我的許多朋友都認為我為在谷歌上任職以幫助父母和孩子了解金錢而感到瘋狂。 也許他們是對的。 B…

藍疊 正在檢查服務器最新,Windows update一直停留在正在檢查更新

Windows update一直停留在正在檢查更新,為什么啊?一、查看相關服務是否開始:請您根據以下步驟,確認windows update & BITS服務設置1. 按下WinR鍵輸入“services.msc”(輸入時不要打引號),并按下回車。如果此時彈出…

spring-DataSource

spring支持的dataSource有好多, 如:自帶的org.springframework.jdbc.datasource.DriverManagerDataSource ibatis、c3p0、JDBC、hibernate等等; 首先看第一種,使用自帶的datasource: 1、項目結構 提示:spri…

《Nmap滲透測試指南》—第7章7.8節后臺打印機服務漏洞

本節書摘來自異步社區《Nmap滲透測試指南》一書中的第7章7.8節后臺打印機服務漏洞,作者 商廣明,更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 7.8 后臺打印機服務漏洞表7.8所示為本章節所需Nmap命令表,表中加粗命令為本小節所需命令——后臺打…