目次
文書の過去の版を表示しています。
uWSGI - WSGI アプリケーションコンテナ
uWSGI は、「ホスティングサービスを構築するためのフルスタックの開発を的とする」ソフトウェアアプリケーションです。プロジェクトでサポートされている最初のプラグインである Web Server Gateway Interface(WSGI; ウィズギー) にちなんで命名されている。
uWSGIは、Cherokee や Nginx などの Web サーバーと組み合わせて Python Web アプリケーションを提供するためによく使用され、uWSGI のネイティブ uwsgi プロトコルを直接サポートする。
uWSGI - Wikipedia より
本家: The uWSGI project — uWSGI 2.0 documentation
ソースコード: GitHub - unbit/uwsgi: uWSGI application server container
dnf によるインストール
$ sudo dnf install uwsgi uwsgi-plugin-python3
メタデータの期限切れの最終確認: 0:00:44 時間前の 2020年04月12日 16時55分24秒 に実施しまし た。 依存関係が解決しました。 ========================================================================================== Package Architecture Version Repository Size ========================================================================================== インストール: uwsgi x86_64 2.0.18-2.fc31 fedora 383 k uwsgi-plugin-python3 x86_64 2.0.18-2.fc31 fedora 78 k 依存関係のインストール: uwsgi-plugin-common x86_64 2.0.18-2.fc31 fedora 46 k トランザクションの概要 ========================================================================================== インストール 3 パッケージ 合計サイズ: 507 k インストール済みのサイズ: 1.2 M これでよろしいですか? [y/N]: y パッケージのダウンロード: [SKIPPED] uwsgi-2.0.18-2.fc31.x86_64.rpm: Already downloaded [SKIPPED] uwsgi-plugin-common-2.0.18-2.fc31.x86_64.rpm: Already downloaded [SKIPPED] uwsgi-plugin-python3-2.0.18-2.fc31.x86_64.rpm: Already downloaded ------------------------------------------------------------------------------------------ 合計 50 MB/s | 507 kB 00:00 トランザクションの確認を実行中 トランザクションの確認に成功しました。 トランザクションのテストを実行中 トランザクションのテストに成功しました。 トランザクションを実行中 準備 : 1/1 scriptletの実行中: uwsgi-2.0.18-2.fc31.x86_64 1/3 インストール中 : uwsgi-2.0.18-2.fc31.x86_64 1/3 scriptletの実行中: uwsgi-2.0.18-2.fc31.x86_64 1/3 Executing systemd post-install tasks インストール中 : uwsgi-plugin-common-2.0.18-2.fc31.x86_64 2/3 インストール中 : uwsgi-plugin-python3-2.0.18-2.fc31.x86_64 3/3 scriptletの実行中: uwsgi-plugin-python3-2.0.18-2.fc31.x86_64 3/3 検証 : uwsgi-2.0.18-2.fc31.x86_64 1/3 検証 : uwsgi-plugin-common-2.0.18-2.fc31.x86_64 2/3 検証 : uwsgi-plugin-python3-2.0.18-2.fc31.x86_64 3/3 インストール済み: uwsgi-2.0.18-2.fc31.x86_64 uwsgi-plugin-common-2.0.18-2.fc31.x86_64 uwsgi-plugin-python3-2.0.18-2.fc31.x86_64 完了しました!
uWSGI Emperor Service の有効化
$ sudo systemctl enable uwsgi
Created symlink /etc/systemd/system/multi-user.target.wants/uwsgi.service → /usr/lib/systemd/system/uwsgi.service.
uwsgi 動作確認
Python バージョン確認
$ python -VV
Python 3.7.6 (default, Jan 30 2020, 09:44:41) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
Python 仮想環境の作成
$ python -m venv py3dj3_venv
Python 仮想環境の有効化
$ . py3dj3_venv/bin/activate (py3dj3_venv) $
Python 仮想環境の pip を更新
(py3dj3_venv) $ python -m pip install --upgrade pip
Collecting pip Using cached https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl Installing collected packages: pip Found existing installation: pip 19.1.1 Uninstalling pip-19.1.1: Successfully uninstalled pip-19.1.1 Successfully installed pip-20.0.2
Django のインストール
(py3dj3_venv) $ pip install django
Collecting django Using cached Django-3.0.5-py3-none-any.whl (7.5 MB) Collecting sqlparse>=0.2.2 Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB) Collecting pytz Using cached pytz-2019.3-py2.py3-none-any.whl (509 kB) Collecting asgiref~=3.2 Using cached asgiref-3.2.7-py2.py3-none-any.whl (19 kB) Installing collected packages: sqlparse, pytz, asgiref, django Successfully installed asgiref-3.2.7 django-3.0.5 pytz-2019.3 sqlparse-0.3.1
Django プロジェクトの作成
(py3dj3_venv) $ django-admin startproject django_project
Python 仮想環境の終了
(py3dj3_venv) $ deactivate $
一番シンプルな uwsgi コマンドで Django アプリの動作確認を行う
$ uwsgi --http-socket :8000 --plugin python3 --virtualenv /home/tomoyan/py3dj3_venv --chdir /home/tomoyan/django_project --wsgi-file django_project/wsgi.py
*** Starting uWSGI 2.0.18 (64bit) on [Tue Apr 14 03:23:45 2020] *** compiled with version: 9.1.1 20190605 (Red Hat 9.1.1-2) on 27 July 2019 00:00:00 os: Linux-4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 nodename: Cmon machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 4 current working directory: /home/tomoyan detected binary path: /usr/sbin/uwsgi chdir() to /home/tomoyan/django_project *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 50741 your memory page size is 4096 bytes detected max file descriptor number: 1048576 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address :8000 fd 3 Python version: 3.7.6 (default, Jan 30 2020, 09:44:41) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] PEP 405 virtualenv detected: /home/tomoyan/py3dj3_venv Set PythonHome to /home/tomoyan/py3dj3_venv *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x98b450 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72920 bytes (71 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x98b450 pid: 5711 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 5711, cores: 1)
http://localhost:8000 にブラウザでアクセスする。
uwsgi コマンドを Ctrl + C で終了
[pid: 5711|app: 0|req: 1/1] 127.0.0.1 () {42 vars in 890 bytes} [Mon Apr 13 18:24:59 2020] GET / => generated 16351 bytes in 21 msecs (HTTP/1.1 200) 4 headers in 123 bytes (1 switches on core 0) Not Found: /static/admin/css/fonts.css [pid: 5711|app: 0|req: 2/2] 127.0.0.1 () {40 vars in 805 bytes} [Mon Apr 13 18:24:59 2020] GET /static/admin/css/fonts.css => generated 2025 bytes in 21 msecs (HTTP/1.1 404) 4 headers in 129 bytes (1 switches on core 0) [pid: 5711|app: 0|req: 3/3] 127.0.0.1 () {42 vars in 884 bytes} [Mon Apr 13 18:27:42 2020] GET / => generated 16351 bytes in 14 msecs (HTTP/1.1 200) 4 headers in 123 bytes (1 switches on core 0) Not Found: /static/admin/css/fonts.css [pid: 5711|app: 0|req: 4/4] 127.0.0.1 () {40 vars in 805 bytes} [Mon Apr 13 18:27:42 2020] GET /static/admin/css/fonts.css => generated 2025 bytes in 8 msecs (HTTP/1.1 404) 4 headers in 129 bytes (1 switches on core 0) Ctrl + C を入力 $
uWSGI Emperor Service の ini ファイルを設定
Django アプリのミニマム設定
- /etc/uwsgi.d/django_project.ini
http-socket = :8000 plugin = python3 virtualenv = /usr/local/lsws/Example/py3dj3_venv chdir = /usr/local/lsws/Example/django_project wsgi-file = django_project/wsgi.py
pip によるインストール
$ pip install uwsgi
Collecting uwsgi Downloading uwsgi-2.0.18.tar.gz (801 kB) |████████████████████████████████| 801 kB 738 kB/s Installing collected packages: uwsgi Running setup.py install for uwsgi ... done Successfully installed uwsgi-2.0.18
Apache 2.4 への mod_uwsgi.so のインストール
$ sudo dnf install httpd-devel $ sudo -s
# cd ~ # curl -O https://raw.githubusercontent.com/unbit/uwsgi/master/apache2/mod_uwsgi.c
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22715 100 22715 0 0 60251 0 --:--:-- --:--:-- --:--:-- 60251
# apxs -i -a -c mod_uwsgi.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd -I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_uwsgi.lo mod_uwsgi.c && touch mod_uwsgi.slo mod_uwsgi.c: 関数 ‘uwsgi_handler’ 内: mod_uwsgi.c:572:7: 警告: 真偽値として使われる代入のまわりでは、丸括弧の使用をお勧めします [-Wparentheses] 572 | if (hret = ap_scan_script_header_err_brigade(r, bb, NULL)) { | ^~~~ mod_uwsgi.c: 関数 ‘cmd_uwsgi_force_wsgi_scheme’ 内: mod_uwsgi.c:601:21: 警告: ‘&’ の被演算子内にある比較の周りに小括弧を付けることを推奨します [-Wparentheses] 601 | if (strlen(scheme) < 9 & strlen(scheme) > 0) { | ~~~~~~~~~~~~~~~^~~ mod_uwsgi.c: 関数 ‘cmd_uwsgi_max_vars’ 内: mod_uwsgi.c:614:13: 警告: 使用されない変数 ‘val’ です [-Wunused-variable] 614 | int val ; | ^~~ mod_uwsgi.c: 関数 ‘cmd_uwsgi_socket2’ 内: mod_uwsgi.c:705:6: 警告: 真偽値として使われる代入のまわりでは、丸括弧の使用をお勧めします [-Wparentheses] 705 | if (tcp_port = strchr(path, ':')) { | ^~~~~~~~ mod_uwsgi.c: 関数 ‘cmd_uwsgi_socket’ 内: mod_uwsgi.c:738:6: 警告: 真偽値として使われる代入のまわりでは、丸括弧の使用をお勧めします [-Wparentheses] 738 | if (tcp_port = strchr(path, ':')) { | ^~~~~~~~ /usr/lib64/apr-1/build/libtool --silent --mode=link gcc -Wl,-z,relro,-z,now -o mod_uwsgi.la -rpath /usr/lib64/httpd/modules -module -avoid-version mod_uwsgi.lo /usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' mod_uwsgi.la /usr/lib64/httpd/modules /usr/lib64/apr-1/build/libtool --mode=install install mod_uwsgi.la /usr/lib64/httpd/modules/ libtool: install: install .libs/mod_uwsgi.so /usr/lib64/httpd/modules/mod_uwsgi.so libtool: install: install .libs/mod_uwsgi.lai /usr/lib64/httpd/modules/mod_uwsgi.la libtool: install: install .libs/mod_uwsgi.a /usr/lib64/httpd/modules/mod_uwsgi.a libtool: install: chmod 644 /usr/lib64/httpd/modules/mod_uwsgi.a libtool: install: ranlib /usr/lib64/httpd/modules/mod_uwsgi.a libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/lib64/httpd/modules ---------------------------------------------------------------------- Libraries have been installed in: /usr/lib64/httpd/modules If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the '-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the 'LD_RUN_PATH' environment variable during linking - use the '-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to '/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- chmod 755 /usr/lib64/httpd/modules/mod_uwsgi.so
# chmod 755 /usr/lib64/httpd/modules/mod_uwsgi.so # vi /etc/httpd/conf/httpd.conf
- /etc/httpd/conf/httpd.conf
# # Dynamic Shared Object (DSO) Support # # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # LoadModule uwsgi_module modules/mod_uwsgi.so Include conf.modules.d/*.conf
参考文献
The uWSGI project — uWSGI 2.0 documentation
Apache support — uWSGI 2.0 documentation
Deploying Python Web Applications with nginx and uWSGI Emperor | Chris Warrick
How to use Django with uWSGI | Django documentation | Django
uWSGIのiniファイルの文法まとめ - Qiita
- python/uwsgi.1587083093.txt.gz
- 最終更新: 2020/04/17 09:24
- by ともやん