文章是uwsgi關於django部署的學習筆記,過程當中涉及:html
最終各個組件之間的關係是 瀏覽器 <-> nginx服務器 <-> linux socket <-> uwsgi服務 <-> Django應用
python
首先,建立一個test.py文件用來處理來自瀏覽器的請求,內容以下:jquery
# 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,並指定test.py進行請求處理linux
uwsgi --http :8000 --wsgi-file test.py
最後,打開瀏覽器,請求http://localhost:8000頁面,若是能夠看到Hello World的話,說明瀏覽器 <----> uwsgi服務 <----> python應用
這個通道是順暢的。nginx
首先,建立django項目,好比叫mysite。 其次,啓動uwsgi,並指定由mysite.wsgi文件處理請求web
uwsgi --http :8000 --module mysite.wsgi
最後,打開瀏覽器訪問http://localhost:8000頁面,若是一切正常的話,訪問到的應該是django經典的It worked頁面了。若是看到的話,說明瀏覽器 <----> uwsgi服務 <----> django應用
這個通道是一樣是順暢的.django
首先,配置nginx使用8000端口,處理/media和/static的請求,其餘請求一概交給uwsgi啓動的8001端口處理.瀏覽器
# 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 http://localhost:8001; include uwsgi_params; } }
其次,在mysite中的settings.py文件中進行以下配置服務器
STATIC_ROOT=os.path.join(BASE_DIR, 'static') STATIC_URL='/static/' MEDIA_ROOT=os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
執行python manage.py collectstatic
將全部的靜態文件收集到指定的位置. 最後,打開瀏覽器,訪問http://localhost:8000/static/js/jquery.min.js。若是一切正常的話,會看到jquery文件,說明nginx已經能夠處理用戶對靜態文件的請求了.app
首先,使用上面的nginx配置。 其次,使用uwsgi啓動django應用
uwsgi --http :8001 --module mysite.wsgi
最後,打開瀏覽器訪問http://localhost:8000,將出現django的it worked,訪問http://localhost:8000/static/js/jquery.min.js會出現jquery文件.
使用socket代替http,並使用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設置爲開機自啓動
# /etc/rc.local /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log exit 0