您好,登錄后才能下訂單哦!
環境搭建
1.安裝uwsgi、nginx和django
apt install nginx pip install uwsgi pip install django
2.測試uwsgi和nginx的連接
PS:下面的例子采用的是 unix socket 的鏈接發送
創文件foobar.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
創文件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 # 這個配置本來是true,即退出就刪掉socket,但這會導致nginx的啟動失敗,所以改為false vacuum = false
創文件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; } }
將這個文件鏈接到/etc/nginx/sites-enabled,這樣nginx就可以看到它了
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
啟動nginx
sudo service nginx start
啟動uwsgi
uwsgi --ini foobar_uwsgi.ini
訪問127.0.0.1:8099,如果出現“Hello world”就說明下面連接棧是成功的。
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
3.建立整個連接棧
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
建立django項目
django-admin startproject mysite
在項目的根目錄建立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
在項目根目錄建立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/
運行
# 運行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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。