基于nginx和uWSGI在Ubuntu系统上部署Django项目
1、 nginx
1.1 安裝
sudo apt-get install nginx
1.2啟動、停止和重啟
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart
或者
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
2、 uWSGI安裝
用python的pip安裝最簡單:
apt-get install python-dev #不安裝這個,下面的安裝可能會失敗
pip install uwsgi
3、基于uWSGI和nginx部署Django
3.1 基本測試
3.1.1 測試uWSGI是否正常
在django項目的根目錄下創建test.py文件,添加源碼如下:
# test.py
def application(env, start_response):
??? start_response('200 OK', [('Content-Type','text/html')])
??? return ["Hello World"] # python2
??? #return [b"Hello World"] # python3
然后,Run uWSGI:
uwsgi --http :8000 --wsgi-file test.py
參數含義:
?? ?http :8000: 使用http協議,8000端口
?? ?wsgi-file test.py: 加載指定文件 test.py
打開下面url,瀏覽器上應該顯示hello world
http://example.com:8000
如果顯示正確,說明下面3個環節是通暢的:
the web client <-> uWSGI <-> Python
3.1.2 測試Django項目是否正常
首先確保project本身是正常的:
python manage.py runserver 0.0.0.0:8000
如果沒問題,使用uWSGI把project拉起來:
uwsgi --http :8000 --module mysite.wsgi
?? ?module mysite.wsgi: 加載wsgi module
如果project能夠正常被拉起,說明以下環節是通的:
the web client <-> uWSGI <-> Django
3.2 nginx的配置和測試
安裝nginx完成后,如果能正常打開http://hostname,說明下面環節是通暢的:
the web client <-> the web server
3.2.1 增加nginx配置
?? ?將uwsgi_params文件拷貝到項目文件夾下。uwsgi_params文件在/etc/nginx/目錄下,也可以從這個頁面下載
?? ?在項目文件夾下創建文件mysite_nginx.conf,填入并修改下面內容:
# 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
??? # Django media
??? location /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
??? }
}
這個configuration文件告訴nginx從文件系統中拉起media和static文件作為服務,同時相應django的request
在/etc/nginx/sites-enabled目錄下創建本文件的連接,使nginx能夠使用它:
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
3.2.2 部署static文件
在django的setting文件中,添加下面一行內容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
然后運行:
python manage.py collectstatic
3.2.3 測試nginx
首先重啟nginx服務:
sudo /etc/init.d/nginx restart
然后檢查media文件是否已經正常拉起:
在目錄/path/to/your/project/project/media directory下添加文件meida.png,然后訪問http://example.com:8000/media/media.png ,成功后進行下一步測試。
3.2.4 nginx and uWSGI and test.py
執行下面代碼測試能否讓nginx在頁面上顯示hello, world
uwsgi --socket :8001 --wsgi-file test.py
訪問http://example.com:8000 ,如果顯示hello world,則下面環節是否通暢:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
4 、 用UNIX socket取代TCP port
對mysite_nginx.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)
重啟nginx,并在此運行uWSGI
uwsgi --socket mysite.sock --wsgi-file test.py
打開 http://example.com:8000/ ,看看是否成功
如果沒有成功:
檢查 nginx error log(/var/log/nginx/error.log)。如果錯誤如下:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
添加socket權限再次運行:
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)
5、Running the Django application with uswgi and nginx(運行帶有uswgi 和nginx 的 Django應用)
如果上面一切都顯示正常,則下面命令可以拉起django application
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
5.1、 Configuring uWSGI to run with a .ini file(使用 .ini文件配置 uWSGI 來啟動項目)
每次都運行上面命令運行django application實在麻煩,使用.ini文件能簡化工作,方法如下:
在application目錄下創建文件mysite_uwsgi.ini,填入并修改下面內容:
# 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
現在,只要執行以下命令,就能夠拉起django application:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
5.2、Make uWSGI startup when the system boots(在系統啟動時讓uWSGI自啟動)
編輯文件/etc/rc.local, 添加下面內容到這行代碼之前exit 0:
/usr/local/bin/uwsgi --socket /path/to/mysite.sock --module /path/to/mysite.wsgi --chmod-socket=666
6、數聚傳媒選擇使用nginx+uwsgi
數聚傳媒技術采用nginx+uwsgi部署django項目就是看中了它的如下特性:
1.?? ?支持高并發量;
2.?? ?方便管理多進程,發揮多核的優勢;
3.?? ?提升性能,因為uwsgi協議比WSGI協議有優勢;
4.?? ?安全,客戶端對Web服務器的訪問需要先經過反向代理服務器。這樣可以防止外部程序對Web服務器的直接***;
5.?? ?負載均衡,反向代理服務器可以根據Web服務器的負載情況,動態地把HTTP請求交給不同的Web服務器來處理,前提是要有多個Web服務器;
6.?? ?提升Web服務器的IO性能。一個HTTP請求的數據,從客戶端傳輸給服務器,是需要時間的,例如N秒,如果直接傳給Web服務器,Web服務器就需要讓一個進程阻塞N秒,來接收IO,這樣會降低Web服務器的性能。如果使用反向代理服務器,先讓反向代理服務器接收完整個HTTP請求,再把請求發給Web服務器,就能提升Web服務器的性能。還有一些靜態文件的請求,可以直接交給反向代理來處理,不需要經過Web服務器。
?
轉載于:https://blog.51cto.com/11926581/1837239
總結
以上是生活随笔為你收集整理的基于nginx和uWSGI在Ubuntu系统上部署Django项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Handler 系列二:如何通信
- 下一篇: HDU 5816 Hearthstone