### uWSGIとは?
-WSGIは仕様で、uWSGIは既存のWebサーバに機能を追加
-WebサーバにNginx, WSGIコンテナにuWSGI(Nginxとは別プロセス)
-Apache2, Nginx, cherokee, lighttpdに対応
-gunicornというWSGIも有名
-Apacheは同時接続数が極端に多くなると対応できないが、Nginxはレスポンスが早い
### Django構築
$ django-admin startproject testSite
$ python3 manage.py startapp testapp
settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testapp', ] ALLOWED_HOSTS = ['*']
views.py
from django.http import HttpResponse # Create your views here. def hello(request): return HttpResponse("hello, Nginx!")
urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.hello, name='hello'), ]
urlpatterns = [ path('admin/', admin.site.urls), path('testapp/', include('testapp.urls')), ]
### UbuntuにNginx
$ sudo apt update
$ apt show nginx
Package: nginx
Version: 1.18.0-0ubuntu1
$ sudo apt install nginx
$ nginx -v
### djangoでuWSGI
$ python3 manage.py runserver 192.168.33.10:8000
$ uwsgi –http :8000 –module testSite.wsgi
testApp/uwsgi_params
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;
testApp/testSite_nginx.conf
upstream django { server 192.168.33.10:8001; } server { listen 8000; server_name 192.168.33.10; charset utf-8; location /static { alias /home/vagrant/other/testSite/static; } location / { uwsgi_pass django; include /home/vagrant/other/testSite/testSite/uwsgi_params; } }
$ sudo ln -s /home/vagrant/other/testSite/testSite/testSite_nginx.conf /etc/nginx/sites-enabled/
setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
$ python3 manage.py collectstatic
$ sudo nginx
failed (Result: exit-code)
$ sudo lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 75758 root 6u IPv4 87712 0t0 TCP *:http (LISTEN)
nginx 75758 root 7u IPv6 87713 0t0 TCP *:http (LISTEN)
nginx 75759 www-data 6u IPv4 87712 0t0 TCP *:http (LISTEN)
nginx 75759 www-data 7u IPv6 87713 0t0 TCP *:http (LISTEN)
nginx 75760 www-data 6u IPv4 87712 0t0 TCP *:http (LISTEN)
nginx 75760 www-data 7u IPv6 87713 0t0 TCP *:http (LISTEN)
$ uwsgi –socket :8001 –module testSite.wsgi buffer-size=32768
うーん、ちょっとよくわからんな。。