nginx+uwsgi+django環(huán)境搭建的方法步驟
環(huán)境搭建
1.安裝uwsgi、nginx和django
apt install nginx pip install uwsgi pip install django
2.測試uwsgi和nginx的連接
PS:下面的例子采用的是 unix socket 的鏈接發(fā)送
創(chuàng)文件foobar.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
創(chuàng)文件foobar_uwsgi.ini
[uwsgi] # Django-related settings # the base directory (full path) chdir = /home/dmd/project/ENV/mysite # Django's wsgi file module = foobar # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /home/dmd/project/ENV/mysite/foobar.sock # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit # 這個(gè)配置本來是true,即退出就刪掉socket,但這會(huì)導(dǎo)致nginx的啟動(dòng)失敗,所以改為false vacuum = false
創(chuàng)文件foobar_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 unix:///home/dmd/project/ENV/mysite/foobar.sock; # 用unix socket # uwsgi_pass 127.0.0.1:8000 # 用TCP socket uwsgi_read_timeout 2; } }
將這個(gè)文件鏈接到/etc/nginx/sites-enabled,這樣nginx就可以看到它了
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
啟動(dòng)nginx
sudo service nginx start
啟動(dòng)uwsgi
uwsgi --ini foobar_uwsgi.ini
訪問127.0.0.1:8099,如果出現(xiàn)“Hello world”就說明下面連接棧是成功的。
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
3.建立整個(gè)連接棧
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
建立django項(xiàng)目
django-admin startproject mysite
在項(xiàng)目的根目錄建立mysite_uwsgi.ini
# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = mysite.sock # the base directory (full path) chdir = /home/dmd/project/ENV/mysite # Django s wsgi file module = mysite.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = false
在項(xiàng)目根目錄建立mysite_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 unix:///home/dmd/project/ENV/mysite/mysite.sock; # 用unix socket # uwsgi_pass 127.0.0.1:8000 # 用TCP socket uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/dmd/project/ENV/mysite/static/; } }
鏈接sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
運(yùn)行
# 運(yùn)行uwsgi uwsgi --ini mysite_uwsgi.ini # 開啟niginx sudo service nginx start
測試。訪問 127.0.0.1:8099 ,如果看到django的頁面,說明成功。
完整的目錄樹
mysite/ ├── db.sqlite3 ├── manage.py ├── mysite │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── settings.cpython-36.pyc │ │ ├── urls.cpython-36.pyc │ │ └── wsgi.cpython-36.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── mysite.sock ├── mysite_nginx.conf ├── mysite_uwsgi.ini └── uwsgi_params
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 聊聊Django+uwsgi+nginx服務(wù)器部署問題
- django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
- Django+Uwsgi+Nginx如何實(shí)現(xiàn)生產(chǎn)環(huán)境部署
- Nginx+Uwsgi+Django 項(xiàng)目部署到服務(wù)器的思路詳解
- 詳解Ubuntu環(huán)境下部署Django+uwsgi+nginx總結(jié)
- Django uwsgi Nginx 的生產(chǎn)環(huán)境部署詳解
- Centos部署django服務(wù)nginx+uwsgi的方法
- 詳解Django+Uwsgi+Nginx 實(shí)現(xiàn)生產(chǎn)環(huán)境部署
- Django+Nginx+uwsgi服務(wù)器部署
相關(guān)文章
Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔以及刻度范圍
這篇文章主要介紹了Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔以及刻度范圍,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06詳解Django 時(shí)間與時(shí)區(qū)設(shè)置問題
這篇文章主要介紹了Django 時(shí)間與時(shí)區(qū)設(shè)置問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07PyTorch高級(jí)教程之自定義模型、數(shù)據(jù)加載及設(shè)備間數(shù)據(jù)移動(dòng)
在深入理解了PyTorch的核心組件之后,我們將進(jìn)一步學(xué)習(xí)一些高級(jí)主題,包括如何自定義模型、加載自定義數(shù)據(jù)集,以及如何在設(shè)備(例如CPU和GPU)之間移動(dòng)數(shù)據(jù),需要的朋友可以參考下2023-07-07Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖
這篇文章主要介紹了Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python使用get_text()方法從大段html中提取文本的實(shí)例
今天小編就為大家分享一篇Python使用get_text()方法從大段html中提取文本的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python解析json文件相關(guān)知識(shí)學(xué)習(xí)
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。接下來通過本文給大家介紹python解析json文件相關(guān)知識(shí),對python解析json文件相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03Python實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作,結(jié)合具體實(shí)例形式分析了2種帶索引的遍歷操作實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05