Upgrade to Pro — share decks privately, control downloads, hide ads and more …

apache に django の web アプリを デプロイしてみよう - OSC 2018...

apache に django の web アプリを デプロイしてみよう - OSC 2018 Tokyo/Spring

Debian GNU/Linux環境を使って apache に django の web アプリをデプロイしてみよう

Norimitsu Sugimoto

March 24, 2018
Tweet

More Decks by Norimitsu Sugimoto

Other Decks in Technology

Transcript

  1. Agenda Debian ͱ Debian JP ʹ͍ͭͯ Debian ͱ web αʔό

    Debian Ͱ Django ͳ web ΞϓϦΛಈ͔͢ ࣮ߦ؀ڥ web αʔόɺWSGI αʔόͷΠϯετʔϧ web ΞϓϦέʔγϣϯͷ഑ஔ mod wsgi ͷઃఆ apache2 ͷઃఆ ಈ࡞֬ೝ ·ͱΊ ࠓޙͷΠϕϯτͱ৘ใೖखઌ
  2. Debian ͱ͸ʁ https://www.debian.org/ Debian ͸ϑϦʔ/ΦʔϓϯͳΦϖϨʔςΟϯάγεςϜ (OS) Λ࡞੒͠Α͏ͱ͢ΔϘϥϯςΟΞϕʔεͷϓϩ δΣΫτɻ ࣗ෼ͨͪͷߟ͑ΔϑϦʔͱ͍͏ݴ༿ʹؔ͢Δఆٛɺ։ ൃ໨తɺύοέʔδϯάϙϦγʔΛݫ֨ʹܾΊ͍ͯΔɻ

    ੈքதʹ 1000 ਓҎ্ͷ։ൃऀ͕͓ΓɺଞͷσΟετϦ Ϗϡʔγϣϯͷϕʔεͱͯ͠࠾༻ɻ ໿ 2 ೥ຖʹϦϦʔε͕ߦΘΕɺଟ͘ͷύοέʔδͱ ΞʔΩςΫνϟΛαϙʔτɻ ೥ʹҰ౓ɺDebconf ͱ͍͏։ൃऀ͕ू͏Πϕϯτ͕͋ ΔɻDebconf18 ͸ 7/21-7/27 ʹ୆࿷Ͱ։࠵༧ఆɻ αʔόɺPCɺ૊ΈࠐΈͳͲ༷ʑͳͱ͜ΖͰར༻͞Εͯ ͍Δ Linux σΟετϦϏϡʔγϣϯɻ
  3. Debian JP Project ͱ Debianษڧձ Debian JP Project https://www.debian.or.jp/ ೔ຊͰ

    Debian Λීٴͤ͞Δ͜ͱΛ໨తͱͨ͠೚ҙஂମɻ Debian ͷ೔ຊޠʹΑΔ৘ใൃ৴ɺϢʔβͱͷ৘ใަ׵ɺ Debian ։ൃऀɺύοέʔδϝϯςφͷҭ੒ͳͲɻ Debian ษڧձ 2005 ೥ 1 ݄։࢝ɻ ౦ژͱؔ੢Ͱຖ݄։࠵͍ͯ͠Δ Debian ։ൃऀɺϢʔβ ʹΑΔษڧձɻ https://tokyodebian.alioth.debian.org/ https://wiki.debian.org/KansaiDebianMeeting ౦ژΤϦΞ͸ୈ 3 ౔༵೔ɺؔ੢ΤϦΞ͸ୈ 4 ೔༵೔ʹ։ ࠵͢Δ͜ͱ͕ଟ͍ɻ
  4. DebianͷwebαʔόͰݴΘΕΔ͜ͱ web αʔό༻్Ͱ Debian ͸͋·Γ࢖ΘΕ͍ͯͳ͍ͷͰ ͸ͳ͍͔ web αʔόͷύοέʔδ͸ଟ਺͋ΓɺͲΕΛΠϯετʔ ϧ͢ΔͱΑ͍ͷ͔Θ͔Βͳ͍ web

    αʔόͷઃఆϑΝΠϧͷσΟϨΫτϦߏ଄͕ RHEL ΍ tarball ͰΠϯετʔϧͨ͠ͱ͖ͱҟͳΔͨΊΑ͘Θ ͔Βͳ͍ ͦΜͳٙ໰ʹ͓౴͑͢ΔͨΊɺࠓճ͸ debian 9 ͷ apache2 ٴͼ python ͷ Web ΞϓϦέʔγϣϯαʔό”WSGI”ͷઃఆ ʹ͍͓ͭͯ࿩͠͠·͢ɻ
  5. webαʔόʹͳͥDebianΛબ୒͢Δͷ͔ Linux ͷ web αʔόͷՔಇ OS γΣΞ 1 1 Ґ

    Ubuntu 39.1 % 2 Ґ Debian 30.8 % 3 Ґ CentOS 20.6 % 4 Ґ Red Hat 2.9 % ݄ 100 ສΞΫηεҎ্ͷ web αΠτͷ web αʔόγΣ Ξ 2 1 Ґ Apache 37.03 % 2 Ґ nginx 30.46 % 3 Ґ Microsoft IIS Web Server 9.57 % 1https: //w3techs.com/technologies/details/os-linux/all/all 2https://news.netcraft.com/archives/2018/01/19/ january-2018-web-server-survey.html
  6. DebainͷWSGIαʔόͷύοέʔδ WSGI = Web Server Gateway Interface PEP 333 ʹͯఆٛ

    4 WSGI Λ࣮૷ͨ͠ web ΞϓϦέʔγϣϯαʔό libapache2-mod-wsgiɺlibapache2-mod-wsgi-py3 ύο έʔδ apache2 ͷ֦ுϞδϡʔϧ mod wsgi apache2 ͷΈར༻Մೳ uwsgi ύοέʔδ nginxɺapache2 Ͱར༻Մೳ python-gunicornɺpython3-gunicorn ύοέʔδ nginxɺapache2 Ͱར༻Մೳ ੑೳϕϯνϚʔΫͷྫʢ2016 ೥ 5 ݄ʣ5 4https://www.python.org/dev/peps/pep-0333/ 5https://blog.appdynamics.com/engineering/ a-performance-analysis-of-python-wsgi-servers-part-2/
  7. ࣮ߦ؀ڥ ࡞੒͢ΔΞϓϦέʔγϣϯͷ࣮ߦ؀ڥ Debian GNU/Linux 9 Stretch amd64 apache-2.4 mod wsgi-4.5

    python-3.5 sqlite3 django-2.0 (deb ύοέʔδΛར༻ͤͣ pip ͰΠϯε τʔϧ) ࡞੒͢Δ࣮ߦ؀ڥͷલఏ virtualenv ͸࢖ΘͣɺγεςϜϫΠυʹ python ϥΠϒϥ ϦΛΠϯετʔϧ͢Δ ৘ใ https://docs.djangoproject.com/ja/2.0/howto/ deployment/wsgi/modwsgi/
  8. webαʔόɺWSGIαʔόͷΠϯετʔϧ # apt update # apt install python3.5 python3-pip #

    apt install apache2 libapache2-mod-wsgi-py3 # apt install sqlite3 # pip3 install django==2.0.2 ஫ҙ఺ libapache2-mod-wsgi-py3 ͱ libapache2-mod-wsgi ͸ύο έʔδͷ੍໿Ͱยํ͔͠ΠϯετʔϧͰ͖ͳ͍ ͭ·Γɺmod wsgi Ͱ͸ python2.7ɺpython3.5 ͷΞϓϦ ΛಉډͰ͖ͳ͍
  9. webΞϓϦέʔγϣϯͷ഑ஔ # apt install git $ git clone https://github.com/dictoss/django-tutorial.git $

    cd django-tutorial/2.0 $ su # mkdir -p /var/www/django-tutorial # cp -r mysite /var/www/django-tutorial/ # chown -fR www-data:www-data /var/www/django-tutorial
  10. mod wsgiͷઃఆʢ2ʣ mod wsgi ʹ͓͚ΔΞϓϦέʔγϣϯͷಈ࡞Ϟʔυ͸ 2 छྨ 7 Daemon ModeʢσϑΥϧτʣ

    Embedded ModeʢWSGIRestrictEmbedded On Λࢦఆʣ Table: mod wsgi ͷಈ࡞Ϟʔυͷൺֱ ʕ Embedded Mode Daemon Mode ϝϞϦ࢖༻ྔ େ த CPU ࢖༻ྔ େ த ੑೳ ߴ த ΞϓϦ࠶ಡΈࠐΈ restart apache2 reload apache2 or touch wsgi.py apache MPM prefork prefork worker event 7http://modwsgi.readthedocs.io/en/latest/user-guides/ reloading-source-code.html
  11. mod wsgiͷઃఆʢ3ʣ ΞϓϦέʔγϣϯݻ༗ͷઃఆʢDaemon Mode ར༻࣌ͷઃఆʣ # vi /etc/apache2/conf-available/wsgi_mysite.conf Alias /static/polls

    ‘‘/var/www/django-tutorial/mysite/polls/static/polls’’ <Directory ‘‘/var/www/django-tutorial/mysite/polls/static/polls’’> Require all granted </Directory> Alias /static/admin \ ‘‘/usr/local/lib/python3.5/dist-packages/django/contrib/admin/static/admin’’ <Directory \ ‘‘/usr/local/lib/python3.5/dist-packages/django/contrib/admin/static/admin’’> Require all granted </Directory> WSGIScriptAlias /mysite /var/www/django-tutorial/mysite/mysite/wsgi.py WSGIDaemonProcess mysite user=www-data group=www-data \ processes=2 threads=64 maximum-requests=256 \ python-path=/var/www/django-tutorial/mysite \ display-name=%{GROUP} WSGIProcessGroup mysite
  12. mod wsgiͷઃఆʢ4ʣ ΋͠ virtualenv Λ࢖͏৔߹͸ɺWSGIDaemonProcess ʹ”python-home”Λࢦఆ͢Δ WSGIDaemonProcess mysite user=www-data group=www-data

    \ processes=2 threads=64 maximum-requests=256 \ python-path=/var/www/django-tutorial/mysite \ python-home=/path/to/dir \ display-name=%{GROUP} WSGIDaemonProcess ͷৄࡉύϥϝʔλ͸υΩϡϝϯτ ࢀর 8 8http://modwsgi.readthedocs.io/en/latest/ configuration-directives/WSGIDaemonProcess.html
  13. apache2ͷઃఆʢ1ʣ Debian ͷ”/etc/apache2”σΟϨΫτϦͷߏ੒ $ tree /etc/apache2 /etc/apache2 ᵓᴷᴷ apache2.conf ᵓᴷᴷ

    conf-available ᴹ ᵓᴷᴷ ʢઃఆϑΝΠϧΛอଘʣ ᵓᴷᴷ conf-enabled ᴹ ᵓᴷᴷ ʢઃఆϑΝΠϧͷ༗ޮ/ແޮઃఆΛอଘʣ ᵓᴷᴷ envvars ᵓᴷᴷ magic ᵓᴷᴷ mods-available ᴹ ᵓᴷᴷ ʢ֦ுϞδϡʔϧͷઃఆϑΝΠϧΛอଘʣ ᵓᴷᴷ mods-enabled ᴹ ᵓᴷᴷ ʢ֦ுϞδϡʔϧͷ༗ޮ/ແޮઃఆΛอଘʣ ᵓᴷᴷ ports.conf ᵓᴷᴷ sites-available ᴹ ᵓᴷᴷ ʢVirtualHost ͷઃఆϑΝΠϧΛอଘʣ ᵋᴷᴷ sites-enabled ᵓᴷᴷ ʢVirtualHost ͷ༗ޮ/ແޮઃఆΛอଘʣ
  14. apache2ͷઃఆʢ2ʣ Debian ͷ”/etc/apache2”σΟϨΫτϦͷղઆ apache2.conf, envvars, magic, ports.conf ઃఆ΍؀ڥม਺Λఆٛ͢ΔϑΝΠϧ *-available σΟϨΫτϦ

    confɺmodsɺsite ͷ 3 छྨ ઃఆϑΝΠϧͷอଘ৔ॴ *-enabled σΟϨΫτϦ confɺmodsɺsite ͷ 3 छྨ ༗ޮ/ແޮͷઃఆͷอଘ৔ॴ a2enconf ͱ a2disconf Ͱ conf ͷ༗ޮ/ແޮΛ੾Γସ͑ a2enmod ͱ a2dismod Ͱ mods ͷ༗ޮ/ແޮΛ੾Γସ͑ a2ensite ͱ a2dissite Ͱ sites ͷ༗ޮ/ແޮΛ੾Γସ͑
  15. apache2ͷઃఆʢ3ʣ ࡞੒ͨ͠ WSGI ΞϓϦέʔγϣϯͷઃఆΛ༗ޮԽ # cd /etc/apache2 # a2enconf wsgi_mysite

    Enabling conf wsgi_mysite. To activate the new configuration, you need to run: systemctl reload apache2 conf-enabled σΟϨΫτϦͷঢ়ଶΛ֬ೝ # cd /etc/apache2/conf-enabled # ls -l wsgi_mysite.conf lrwxrwxrwx 1 root root 34 2 ݄ 18 12:28 wsgi_mysite.conf -> ../conf-available/wsgi_mysite.conf
  16. ಈ࡞֬ೝ ઃఆΛ൓ө͢ΔͨΊɺapache2 Λ࠶ىಈ # systemctl restart apache2 ϓϩηε֬ೝ # ps

    -ef | grep www | grep -v grep www-data 6507 6506 0 3 ݄ 18 ? 00:00:00 (wsgi:mysite) -k start www-data 6508 6506 0 3 ݄ 18 ? 00:00:00 (wsgi:mysite) -k start www-data 6509 6506 0 3 ݄ 18 ? 00:00:00 /usr/sbin/apache2 -k start www-data 6510 6506 0 3 ݄ 18 ? 00:00:00 /usr/sbin/apache2 -k start ΞΫηε֬ೝ http://localhost/mysite/polls/
  17. ·ͱΊ Debian ܥ OS ͸ web αʔόͱͯ͠ଟ͘ར༻͞Ε͍ͯΔ web αʔόͱ WSGI

    αʔόͷ deb ύοέʔδͷ঺հ apache2 ʴ mod wsgi ͷΠϯετʔϧํ๏ mod wsgi Ͱಈ࡞͢ΔΞϓϦέʔγϣϯͷઃఆϑΝΠϧ ͷ࡞੒ํ๏ debian ͷ apache2 ͷσΟϨΫτϦߏ੒ͷղઆ
  18. ࠓޙͷΠϕϯτͱ৘ใೖखઌ ୈ 132 ؔ੢ Debian ษڧձ 2018/02/25ʢ೔ʣ 13:30 - 17:00

    େࡕɹ෱ౡ۠ຽηϯλʔ https://wiki.debian.org/KansaiDebianMeeting/ 20180225 ୈ 161 ճ౦ژΤϦΞ Debian ษڧձ 2018/03/24ʢ౔ʣ 14:00 - 18:00 http: //tokyodebian.alioth.debian.org/2018-03.html Debian Project Web αΠτ https://www.debian.org/ Debian Wiki https://wiki.debian.org/ja Debian JP Project Web αΠτ https://www.debian.or.jp/ Twitter @debianjp, @debian jp ࡶࢽ Software Design ʮDebian Hot Topicsʯ