Linux 版本:SentOS7.4html
Python 版本:3.7.1python
pip3 install uwsgi
uwsgi 主要的任務是座位分發路由的服務器。linux
先寫一個測試文件,測試 uwsgi 是否能夠正常使用。nginx
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
而後用 uwsgi 啓動測試文件django
uwsgi --http :8000 --wsgi-file test.py
而後訪問 127.0.0.1:8000 端口,便可收到 hello word 。證實 uwsgi 啓動成功。服務器
重點來了:用 uwsgi 使用配置文件啓動 Django 項目。app
先不要急,先用 Django 項目自帶的 wsgi.py 文件啓動一下 django 試試。dom
uwsgi --http :8000 --wsgi-file djangp/wsgi.py
訪問 127.0.0.1:8000socket
OK,啓動成功。測試
接下來的任務就是寫配置文件了,能夠設置更加詳細的參數。
文件 wsgi.ini:
[uwsgi] # django 相關配置 # 必須所有爲絕對路徑 # 項目執行的端口號 # http = :8000 # 用 uwsgi 啓動項目用 http socket = :8001 # 用 nginx 啓動用 socket # 項目路徑 chdir = 項目路徑 # Django 的 wsgi 文件的相對路徑 # wsgi-file = 項目路徑/wsgi.py module = mysite.wsgi # python 虛擬環境的 路徑 home = /root/pythonEnvs/virtualenvName # 進程相關設置 # 主進程 master = True # 最大數量工做進程 processes = 4 # socket文件地址,絕對路徑(不用手動建立,額nginx連接後自動建立) #socket = # 設置socket權限 #chmod-socket=666 # 守護進程的方式運行,log日誌存在log文件裏 deamonize = 項目路徑/uwsgi.log # 退出時清理環境 vacuum=True
如今用這個文件經過
uwsgi --ini wsgi.ini
就能夠啓動 django 項目了。
關於其中的 http 和 socket 字段,當只用 uwsgi 啓動 django 項目的時候,用 http 字段,由於是經過 http 請求服務器的。
若是還要使用 nginx 就使用 socket 字段。
好啦,如今 uwsgi 也配置好了。
接下來配置 nginx
首先下載 nginx
yum install nginx
下載成功後執行 nginx
命令直接啓動。
而後訪問本地地址能夠看到 nginx 的歡迎頁面。
OK,接下來接入 nginx 就能夠了,先來寫 nginx 的配置文件。
events { worker_connections 1024; } http { upstream django { server 127.0.0.1:8001; } server { # the port your site will be served on listen 8000 ; # 端口號 # the domain name it will serve for server_name 0.0.0.0; # 服務器 ip 或是域名 charset utf-8; # Django media location /media { alias 項目路徑/media; # 媒體文件所在文件夾 } location /static { alias 項目路徑/static; # 靜態文件所在文件夾 } # max upload size client_max_body_size 75M; # adjust to taste # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /uwsgi_params; #uwsgi_params 路徑 } } }
其中用到一個 uwsgi_params 文件,這個文件是用來連接 nginx 和 uwsgi 的。
本身建一個文件就能夠,內容以下:
uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
先來部署一下靜態文件,若是你的 app 下有多個靜態文件的話(我沒用到)。
python manage.py collectstatic
STATIC_ROOT 必定要設置好。
啓動 nginx:
nginx -c 配置文件地址
配置文件地址必定要寫絕對路徑。
啓動 uWSGI:
uwsgi --ini wsgi.ini
OK,到這裏 nginx+uWSGI+Django 項目部署就完成啦。