Slide 1

Slide 1 text

େ౷ҰPHP 2018/03/09 PHPerKaigi 2018 લ໷ࡇ

Slide 2

Slide 2 text

uzullaͰ͢

Slide 3

Slide 3 text

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

https://www.youtube.com/watch?v=2tZxNtn0kDc • PerlͷΧϯϑΝϨϯεͰPHPͷ࿩ͰϕεττʔΫ໯ͬͨ΍ͭ • ʢ݁Ռɺ৆඼Ͱւ֎ΧϯϑΝϨϯεʹɺӳޠ͕Θ͔ΒΜPHPer ͕ɺӳޠͰPerlτʔΫΛ͠ʹߦ͘Ӌ໨͜ͱʹͳͬͨɺΊͬͪΌ Α͔ͬͨʣ • ΋͏಺༰͸ݹ͍ʢ·ͩPHPΛ஌Βͳ͍ਓʹ͸ྑ͍͔΋ʣͷͩ ͕ɺ͍·ͩʹײ૝Λ͍ͨͩ͘

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

https://www.youtube.com/watch?v=XOlSMGHhfrY • php.iniʹ͍ͭͯ̍࣌ؒҎ্ʁͨͬ΀Γ࿩ͨ͠ • ·͋ɺPHPerͳΒݺٵͱಉ͘͡Β͍׳Ε͍ͯΔࣄͩͱ͸ࢥ͏ • ·ͩ໾ʹͨͭͱࢥ͏ʢͨͿΜʣ

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

https://www.youtube.com/watch?v=bVRgVgEf9Ho • ʮPHPͰεϚϗΞϓϦʂʯʮPHPͰεϚʔτσδλϧΧϝϥʂʯ • ࢓ࣄʹ͸໾ཱͨͳ͍੒෼ଟΊ • PHP͕޷͖ͳͷͰ • ࣗ෼ͷझຯͰ͢

Slide 10

Slide 10 text

͸͍

Slide 11

Slide 11 text

DISCLAIMER • ͜ͷτʔΫ͸ٕज़తͳڵຯΛຬͨͨ͢Ίͷख๏Ͱ͋Γɺৗ༻͢ ΂͖ͱ͸͍͑ͳ͍ख๏ؚ͕·Ε͍ͯ·͢ɻ • ʮ͏͚͝͹͍͍ʢ΍ͬͯΈͨΒ͏͍ͨ͝ʣʯతͳख๏΋͋Γ· ͢ɻ • ۀ຿ʹ࢖͏લʹಉ྅ͱ૬ஊ͠·͠ΐ͏ʂ

Slide 12

Slide 12 text

ຊ୊

Slide 13

Slide 13 text

ʮ౷Ұʯ ɹ ʮී௨ʯ

Slide 14

Slide 14 text

ࢲ͕ײ͡Δ໰୊

Slide 15

Slide 15 text

Α͋͘Δ෩ܠ ʁɿʮPHPͷ؀ڥߏஙΑ͘Θ͔ΒΜʯ ࢲɿʮࢲ΋Α͘Θ͔ΒΜɺମ͕͔ͬͯʹಈ͘ʯ

Slide 16

Slide 16 text

1, PHPͰΞϓϦΛಈ͔͢ʮ؀ڥʯ͕৭ʑ͋Γ͗͢Δ • Apache(+mod_php) • Apache with php-cgi • nginx with php-fpm+php-cgi(FastCGI) • IIS with php-cgi(FastCGIɺPM͸IIS͕ߦ͏) • h2o with php-cgi(FastCGIɺPM͸h2o͕ߦ͏ʣ • PHP on GAE(ṖͷPHP) • HHVMʢ୯ମͰhttpdʹͳΔʣ

Slide 17

Slide 17 text

ී௨͸(?) • nginx౳ͷhttpd + httpΛ஻ΔΞϓϦ • 㙽ʹ֯ΞϓϦ͸httpΛ஻Δ • ʢPHP͸FastCGI…ΊΜͲ͍…ʣ

Slide 18

Slide 18 text

2, PHPΛͲ͔͜Β΋ͬͯ͘Δʁ • yum,apt (Linux • installer (Windows • Brew (Mac • php-build (Linux,Mac • ໺ྑϏϧυ (tar ball͔ΒϏϧυ • ࣗՈrpm

Slide 19

Slide 19 text

ී௨͸(?) • ·͋ɺLLͳΒ͜͜Β΁Μ͸େࠩͳ͍…ͷ͕ͩ • goͱ͔͸ϫϯόΠφϦ • ʮCͱ͔͸so͕͋Δ͔ΒʯʮsoͳΜͩʯ • ʮࠓ͙͢μ΢ϯϩʔυʯ • ʮαʔόϨε͸όΠφϦ΋͍Βͳ͍ͧʂʯʮͦ͏͍͏࿩Ͱ͸ͳ ͍Ͱ͢…ʯ

Slide 20

Slide 20 text

PHPόΠφϦ͸৭ʑ͋Δ • php(-cli) • php-cgi • php-fpm(+php-cgi) • mod_php(.soɺ.dllʣ • PHPerͳΒ͜ΕΒΛ֮ࣗతʹʮ࡞Εʯ·͢ΑͶ

Slide 21

Slide 21 text

ී௨͸ʢʁʣ • ʮී௨͸Ͳ͏ͳͷʁʯʮී௨͸Ұݸ͚ͩͩΑʯ • PerlͳΒperlʂʂRubyͳΒrubyʂʂ • ʢbundler,cartonͳͲ͸πʔϧͰ͢ʣ • ʮmod_perl͸ʁʯʮ΢ο಄͕ʯ • ʮmod_rubyʯʮૉਓͳͷͰΈͨ͜ͱͳ͍Ͱ͢ʯ • GoͳΒίϯύΠϧޙόΠφϦ

Slide 22

Slide 22 text

3, C֦ுʢPeclʣ • peclίϚϯυͰάϩʔόϧʹೖΕΔ • ໺ྑϏϧυͰάϩʔόϧʹೖΕΔ • yum,apt౳ͰೖΕΔ • php.iniͰࣄલϩʔυ͢Δ

Slide 23

Slide 23 text

ී௨͸ʢʁʣ • ʮcpanm -lͰϥΠϒϥϦͱಉ༷ʹϩʔΧϧʹೖΕΔʯ • ʮbundlerͰϥΠϒϥϦͱಉ༷ʹϩʔΧϧʹೖΕΔʯ • ຋༁ɿʢcomposer install͢ΔͱɺvendorͷԼʹC֦ு͕ ೖΔʣ • ※ ·͋͜Ε͸ඞͣͦ͏Ͱ͸ͳ͍͕ɺPHPͩͱ໰౴ແ༻Ͱάϩʔ όϧʹೖΔΑͶ • ʮpeclͬͯͳʹʁcomposerͰ͸͍Βͳ͍ͷʁΉ͔͍ͣ͠ʂʯ

Slide 24

Slide 24 text

PHP͸ଉΛ͢ΔΑ͏ʹ͜͜Β΁ΜΛཧղ͠ɺ࢖͏͕… • ී௨ɺ͜͜·ͰόϦΤʔγϣϯͳ͍ͷͰɺඇPHPer͸ࠔ࿭ • Ͱ΋ɺզʑ͸໰୊ʹ͸ͳͬͯͳ͍ • օ͞Μ͸͍͔͢͝Β • PHP͸޿͘࢖ΘΕ͍ͯΔ͔Β੔උ͞Ε͍ͯΔ • ͔͠͠ɺීஈgoͳͲΛ࢖͍ͬͯΔਓʹ͸Α͘Θ͔ΒΜΒ͍͠ • ͜·ͬͨ͜·ͬͨ

Slide 25

Slide 25 text

·ͱΊΔͱ • ී௨ͷਓ͔Βݟͯɺʮී௨ʯͰ͸ͳ͍PHP • (ʮී௨ʯͷఆٛ͸͜͜Ͱ͸͠·ͤΜʂʂ) • அઈΛײ͡Δ… • ʮΑ͠ɺී௨ʢʁʣʹ͋Θ͍ͤͯ͜͏ʂʯ

Slide 26

Slide 26 text

ʮ౷Ұ͍ͯͧ͘͠ʂʯ

Slide 27

Slide 27 text

͸͍

Slide 28

Slide 28 text

ͱɺ͍͏͜ͱͰ

Slide 29

Slide 29 text

ͱΓ·ɺhttpdʹͳΖ͏ʂʢʁʣ

Slide 30

Slide 30 text

࠶ܝʣଞͷݴޠͰ͸… • go → ࣗ෼͕httpdʹͳΔ • ruby → ࣗ෼͕httpdʹͳΔ • Perl → ࣗ෼͕httpdʹͳΔ • PHP → Apache౳͕͍Δ

Slide 31

Slide 31 text

ʮͱʹ͔͘httpdʹͳΔͷ͕ී௨ʂ Զ͸ී௨ʹͳ͍ͬͯͧ͘ʂʂʂʯ • ※ ཁग़య • ※ ಠࣗղऍ

Slide 32

Slide 32 text

΋ͪΖΜզʑ΋httpdʹ ͳΕ·͢

Slide 33

Slide 33 text

(Builtin Serverʁ͋Ε͸։ൃ༻͔ͩΒ…)

Slide 34

Slide 34 text

ͱ͍͏͜ͱͰ ΍ͬͯΈΑ͏

Slide 35

Slide 35 text

Slim framework Ͱ࣮૷ྫ

Slide 36

Slide 36 text

ී௨ͷslim get('/', function ($request, $response) { return $response->write("hello!!"); }); $app->run();

Slide 37

Slide 37 text

ReactPHPΛ͔ͭͬͨ Slim

Slide 38

Slide 38 text

react஌Βͳ͍ਓ΁ • ΠϕϯτۦಈͰ༷ʑͳॲཧΛ͢ΔϥΠϒϥϦ • ͳΜͰ΋ਓͱมΘͬͨ͜ͱ͕޷͖ͳਓʹ͸͓͢͢Ί • PHP୯ମͰhttpdʹͳΕΔκʂ • (ผͷεϥΠυͰઆ໌ͯ͠·͢)

Slide 39

Slide 39 text

get('/', function ($request, $response) { return $response->write("hello!!"); }); // !Ҿ਺ʹtrueΛ͍ΕΔͱɺresponse ͕ؼ͖ͬͯ·͢ͷͰɺͦΕΛreact΁ return $app->run(true); } )); $socket = new React\Socket\Server(8888, $loop); $server->listen($socket); $loop->run();

Slide 40

Slide 40 text

demo $ php main.php • ͸ͱ͘ʹ͠·ͤΜʂޙͰʹͨΑ͏ͳ͜ͱ΍Δ

Slide 41

Slide 41 text

SlimͱreactPHP͸PSR-7ରԠͳͷͰ • ͜Μͳ෩ʹͿͬ͜Ί͹ಈ͘ʢ͸ͣʣ • ϫϯλονͰҠ২͕Ͱ͖ΔΘ͚Ͱ͸ͳ͍ • PSR-7 ରԠͱ͸ͦ͏͍͏ҙຯͰ͸ͳ͍ • ͜Ε͸ผʹߴ଎Ͱ͸ͳ͍ʢ࠷దԽ΍γϯάϧτϯԽ౳ͯ͠ͳ ͍ʣ • ϒϩοΩϯάΛߟ͑ΔͱΉ͠Ζ஗͍ࣄ΋

Slide 42

Slide 42 text

օ͞Μʣʮ͋Ε…ʁҙ֎ͱ؆୯ͳͷͰ͸…ʁʯ

Slide 43

Slide 43 text

ͦΕ͸Ͳ͏͔ͳʁ

Slide 44

Slide 44 text

ʮී௨ʯͷͨΊʹɺ৭ʑ΍Δ͜ͱ͸͋Δ • httpdͷ୅ΘΓʹ੩తϑΝΠϧ഑৴ʢAPIͳΒෆཁ͕ͩ • ΤϥʔϋϯυϦϯά • ϩΨʔ • ηογϣϯʢCSRF౳ͷͨΊʣ • ޙड़͠·͢ • etc...

Slide 45

Slide 45 text

༨ஊ PSR-7ͱ͸(ৄࡉ͸άάͬͯ) • ͍ΘΏΔʮ$_POSTͰಡΈࠐΈɺechoͰग़ྗʯΛ͠ͳ͍ • request/responseΦϒδΣΫτΛಡΜͩΓੜ੒ͨ͠Γͯ͠ɺ ೖग़ྗΛOOPͰऔΓѻ͏ • ʮ̍ϦΫΤετ̍ϓϩηεʯͷϝϦοτͰ͋Δεʔύʔάϩʔ όϧΛഉআ͠ɺʮNϦΫΤετɺ̍ϓϩηεʯΛ࣮ݱͨ͠Γɺς ελϒϧʹͨ͠ΓɺϞδϡϥʔԽʢϛυϧ΢ΣΞԽʣ͕Ͱ͖Δ Α͏ʹͳΔͳͲ͕͋Δɻ

Slide 46

Slide 46 text

࣮ࡍʹαϯϓϧΞϓϦΛҠ২ͯ͠Έͨ • Tinitterͱ͍͏ɺ੿ஶʮWebΞϓϦΤϯδχΞཆ੒ಡຊʯͷα ϯϓϧܝࣔ൘ΞϓϦΛҠ২ • ʢPHP7ରԠͨ͠ΓɺSlim Framework3ʹରԠͤͨ͞Γɺ ORMΛ౤ࣺ͛ͯͨΓ͢Δͷʹ͕͔͔࣌ؒͬͨ…ʣ • https://github.com/uzulla/Tinitter • (ͷ reactize ϒϥϯν)

Slide 47

Slide 47 text

demo • վमલͷී௨ͷTinitter • Ͳ͏ͱ͍͏͜ͱ͸ͳ͍͕ɺBuiltin serverͰ͏͔͝͠ͱΔͧ ͱ͍͏࿩ɻ

Slide 48

Slide 48 text

reactͱSlimΛͭͳ͙ • ͜͜͸͢Ͱʹઆ໌ࡁΈͳͷͰলུ

Slide 49

Slide 49 text

੩తϑΝΠϧ഑৴ • ͜Ε͸؆୯ɺϛυϧ΢ΣΞΛΞϓϦͷલʹࠩ͠ࠐΉ͚ͩ • ʢϛυϧ΢ΣΞʹ͍ͭͯ͸ޙड़ʣ • ΞϓϦΑΓલʹ͜͞͠Ί͹ɺϑΝΠϧ͕ͳ͚Ε͹ΞϓϦʹ౸ୡ ͢Δ new WebrootPreloadMiddleware(__DIR__."/htdocs"),

Slide 50

Slide 50 text

༨ஊ ϛυϧ΢ΣΞͱ͸ʁ • PSR-7(ҎޙPSR-15΋ؚΉ)ରԠͷϛυϧ΢ΣΞͱ͸ɺελο Ϋͨ͠ϛυϧ΢ΣΞΛReq/ResΦϒδΣΫτΛ௨ա༷ͤͯ͞ʑ ͳલॲཧɺޙॲཧΛߦ͏ɻ • CSRFɺΞΫηε੍ݶɺηογϣϯͳͲɻ • PSR-7ରԠͷʮϑϨʔϜϫʔΫʯ͸ɺجຊϛυϧ΢ΣΞͱͯ͠ ࣮૷͞ΕΔɻ

Slide 51

Slide 51 text

1 1 quote the image from http://www.darrenmothersele.com/blog/2017/03/28/php-middleware/

Slide 52

Slide 52 text

$server = new Server(array( // ΤϥʔϋϯυϦϯά ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ function (ServerRequestInterface $request, callable $next) { return $next($request); /* ུ */ }, // ੩తϑΝΠϧ഑৴ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ new WebrootPreloadMiddleware(__DIR__."/htdocs"), // ηογϣϯ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ new SessionMiddleware(/* ུ */), // csrfରࡦ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ function (ServerRequestInterface $request, callable $next) { /* ུ */ return $next($request); }, // ΢ΣϒΞϓϦ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ function (ServerRequestInterface $request) { $app = new \Slim\App($container); /* ུ */ return $app->run(true); // ΢Σϒຊମ࣮ߦɺ͜͜Ͱ͸next͕ͳ͍ } ));

Slide 53

Slide 53 text

ݟ͑ͳ͍ͷͰൈਮ֦େ

Slide 54

Slide 54 text

$server = new Server([ // ഑ྻͰϛυϧ΢ΣΞΛొ࿥͢Δ // !ΤϥʔϋϯυϦϯάϛυϧ΢ΣΞ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ function ($request, $next) { // " req(Πϯελϯε)ͱnext(ؔ਺)ΛऔΔ return $next($request); // " ࣍΁ /* ུ */ }, // !੩తϑΝΠϧ഑৴ϛυϧ΢ΣΞ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ new WebrootPreloadMiddleware(__DIR__."/htdocs"), // " $this->__invoke() // !΢ΣϒΞϓϦʢϛυϧ΢ΣΞʣ ʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹʹ function ($request) { // ͜͜͸࣍(next)͕ͳ͍ $app = new \Slim\App($container); /* ུ */ return $app->run(true); // " ΢ΣϒΞϓϦຊମ࣮ߦɺres(Πϯελϯε)Λreturn } ]);

Slide 55

Slide 55 text

• ελοΫ͕RequestΠϯελϯεΛϦϨʔ͢ΔΑ͏ʹ্͔ΒԼ ʹ࣮ߦ͞Εɺ ɹ$next($request)ͰResponseΠϯελϯε্͕ʹ·ͨ໭ͬͯ ͍͘ɻ • ్தͰ্ʹฦͯ͠͠·͏͜ͱ΋Ͱ͖Δ • ੩తϑΝΠϧ഑৴ͰฦͤΕ͹ɺͦͷԼʹ͸͍͔ͳ͍ • ͜ͷ͋ͨΓɺ࠷ۙͰ͸PSR-15ͰҾ਺ͷ༷͕ࣜมΘΔ ɹʢ͕ɺجຊ͸ಉ͡ʣ

Slide 56

Slide 56 text

༨ஊʢύον͕ඞཁͰͨ͠ʣ • ͦͷ··Ͱಈ͔ͳ͔ͬͨ • mimeΛर͑ͳ͍ͱɺnull͕Ͱ͖ͯͯམͪΔ • PatchedWebrootPreloadMiddlewareʹίϐͬͯಀ͛ͨ

Slide 57

Slide 57 text

ΤϥʔϋϯυϦϯά • ΤϥʔϋϯυϦϯά͕ඞཁ • ௨ৗΤϥʔ͕ը໘ʹ͸ग़ͳ͍ɺϩάʹ΋ग़ͳ͍

Slide 58

Slide 58 text

function (ServerRequestInterface $request, callable $next) { $promise = new React\Promise\Promise( function ($resolve) use ($next, $request) { $resolve($next($request)); } ); // ྫ֎͕͕͋ͬͨΒɺ!͕࣮ߦ͞ΕΔ return $promise->then(null, function (Throwable $e) { // Throwable͕ྑ͍ͱࢥ͏ error_log("ERROR:" . $e->getMessage()); error_log($e->getTraceAsString()); return new Response( 500, array(), 'Internal Server error' ); }); },

Slide 59

Slide 59 text

• ϛυϧ΢ΣΞͱͯ͠ɺྫ֎ΛͳΜͰ΋Ωϟον͢Δ܅Λ࡞Δ • ྫ֎͕͕͖͋ͬͯͨΒɺΩϟονͯ͠Τϥʔը໘Λදࣔ͢Δ • ྫ֎ͷ଴ͪड͚͸reactͷPromiseΛ͔ͭͬͯ଴ͪड͚ • ମࡋ͸झຯͰ͢Ͷɺࣗ͝༝ʹͲ͏ͧ

Slide 60

Slide 60 text

ηογϣϯ • ϩάΠϯ΍CSRF౳ͷͨΊʹඞཁ • PSR-7ʹ͸ʮηογϣϯ͸औΓܾΊ͕ͳ͍ʯ • $_SESSION͸εʔύʔάϩʔόϧͳͷͰ ʮಉ࣌NϦΫΤετ1ϓϩηεʯෆՄ • $_SESSIONʹ͋·΍͔͞ΕͨPHPerʹ͸ͩΔ͍ • ͱ͸͍͑ϛυϧ΢ΣΞͰ࣮૷͞Εͨ΋ͷ΋͋Δ

Slide 61

Slide 61 text

// ηογϣϯ new \WyriHaximus\React\Http\Middleware\SessionMiddleware( 'MySessionCookie', $cache, // Instance implementing React\Cache\CacheInterface [ // Optional array with cookie settings, order matters 0, // expiresAt, int, default '', // path, string, default '', // domain, string, default false, // secure, bool, default false // httpOnly, bool, default ] ),

Slide 62

Slide 62 text

༨ஊ • PSR-7ରԠͷηογϣϯ͸৭ʑ͋Δ͕ɺCookieʹ͍ΕΔ΍͕ͭ ଟ͍(ྫ:psr7-sessions/storageless) • PHPerͱͯ͠͸ɺΘ͔ΔɺΘ͔Δͷ͕ͩ…΢ʔϜ…ͱͳͬͯ͠· ͏͕ɺલड़ͷ͸େৎ෉ɺσʔλΛαʔόͰ࣋ͭ • σϑΥϧτͷετϨʔδ͸ΦϯϝϞϦͳͷͰɺࣗ෼ͰετϨ ʔδΛ࣮૷(React\Cache\CacheInterfaceΛ࣮૷ͨ͠΋ ͷ)͢ΔͱΑ͍ͩΖ͏ɻ

Slide 63

Slide 63 text

CSRFTokenͷ࿩ʹ໭Δ • Slim Framework 3ͷAddonͷCSRFGuard͸$_SESSIONΛͭ ͔͍ͬͯΔͷͰ͏͔͝ͳ͍ • ΑͬͯɺઌఔͷSessionMiddlewareΛ͔ͭͬͯࣗ෼Ͱ࣮૷͢ Δ

Slide 64

Slide 64 text

function (ServerRequestInterface $request, callable $next) { // read session $session_obj = $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME); $session = $session_obj->getContents(); if(!isset($session['csrf_token'])) { $session_obj->setContents(['csrf_token' => random_bytes(64)]); } $csrf_token = $session_obj->getContents()['csrf_token']; // POSTͳΒcheck token if(strtolower($request->getMethod()) === "post"){ $params = $request->getParsedBody(); if(!isset($params['csrf_token']) || $params['csrf_token'] !== $csrf_token){ error_log("invalid csrf_token"); return new Response(400, [], "invalid csrf_token"); } } // attributeʹtokenΛอ࣋ $request = $request->withAttribute('csrf_token', $csrf_token); return $next($request); },

Slide 65

Slide 65 text

csrfτʔΫϯΛformʹຒΊࠐΉॴ • csrfτʔΫϯΛΫϩʔδϟͰίϯςφʹ͍Ε͓ͯ͘ • Twig֦ுΛॻ͍ͯ௥Ճ͢Δ

Slide 66

Slide 66 text

// ίϯςφʹcsrf_tokenΛอ࣋͠ɺTwigϔϧύʹ౉͢ $csrf_token = $request->getAttribute('csrf_token'); $container['csrf_token'] = function ($c) use ($csrf_token){ return $csrf_token; }; $container->view->addExtension(new CsrfExtension($csrf_token));

Slide 67

Slide 67 text

csrf = $token; } public function getGlobals() { return ['csrf' => ['value' => $this->csrf] ]; } public function getName() { return 'my/csrf'; } }

Slide 68

Slide 68 text

Slide 69

Slide 69 text

༨ஊ Attributeʹ͍ͭͯ • PSR-7ͷreqʹ͸Attributeͱ͍͏ɺ࣍ͷϛυϧ΢ΣΞʹ౉͢ req΁͍Ε͓͚ͯΔ৘ใ͕͋Δ • (req಺ͳͷͰ)ࣗ෼ΑΓલͷϛυϧ΢ΣΞʹ͸΋Ͳͤͳ͍Αʂ • AttributeΩʔ໊͸ແ๏஍ଳࣗ༝ʢϑϦʔμϜʣ • ʢηογϣϯ͸ී௨͜͜ͱ͔͖·ͬͯ΄͍͠…ʣ

Slide 70

Slide 70 text

ϩΨʔ • লུ • echoͰ΋error_log()Ͱ΋monologͰ΋ಈ͘ • monologͳΒɺγϯάϧτϯʹ͓ͯ͘͠ͱΦγϟϨʢʁʣ • ྫ֎ͷ$e->getMessage()ͱ$e->getTraceAsString()ͩ ͠ͱ͖ΌOKͰ͠ΐ

Slide 71

Slide 71 text

ͱ͍͏͜ͱͰ • ͏͘͝Α͏ʹͳͬͨ

Slide 72

Slide 72 text

then(null, function (Throwable $e) { error_log("ERROR:" . $e->getMessage()); error_log($e->getTraceAsString()); return new Response( 500, array(), 'Internal Server error' ); }); }, // ੩తϑΝΠϧ഑৴ new \WyriHaximus\React\Http\Middleware\WebrootPreloadMiddleware(__DIR__."/htdocs"), // ηογϣϯ new \WyriHaximus\React\Http\Middleware\SessionMiddleware( 'MySessionCookie', $cache, // Instance implementing React\Cache\CacheInterface [ // Optional array with cookie settings, order matters 0, // expiresAt, int, default '', // path, string, default '', // domain, string, default false, // secure, bool, default false // httpOnly, bool, default ] ), // csrfରࡦ function (ServerRequestInterface $request, callable $next) { // read session $session_obj = $request->getAttribute(\WyriHaximus\React\Http\Middleware\SessionMiddleware::ATTRIBUTE_NAME); $session = $session_obj->getContents(); if(!isset($session['csrf_token'])) { $csrf_token = base64_encode(random_bytes(64)); $session_obj->setContents([ 'csrf_token' => $csrf_token, ]); }else{ $csrf_token = $session['csrf_token']; } // check token if(strtolower($request->getMethod()) === "post"){ $params = $request->getParsedBody(); if(!isset($params['csrf_token']) || $params['csrf_token'] !== $csrf_token){ error_log("invalid csrf_token"); return new Response(400, [], "invalid csrf_token"); } } $request = $request->withAttribute('csrf_token', $csrf_token); return $next($request); }, // ΢ΣϒΞϓϦ function (ServerRequestInterface $request) { // SlimॳظԽ $container = new \Slim\Container; $app = new \Slim\App($container); // reactͷrequestΛಥͬࠐΉ $container['request'] = function ($container) use ($request) { return $request; }; // Register Twig View helper $container['view'] = function ($c) { $view = new \Slim\Views\Twig(TEMPLATES_DIR_PATH, []); $view->addExtension(new \Slim\Views\TwigExtension($c['router'], '/')); return $view; }; // csrf_tokenΛอ࣋͠ɺTwigϔϧύʹ౉͢ $csrf_token = $request->getAttribute('csrf_token'); $container['csrf_token'] = function ($c) use ($csrf_token){ return $csrf_token; }; $container->view->addExtension(new \Tinitter\Misc\TwigExt\CsrfExtension($csrf_token)); // SlimʹϧʔτΛొ࿥ \Tinitter\Route::registration($container); // ࣮ߦ return $app->run(true); } )); $socket = new React\Socket\Server(8888, $loop); $server->listen($socket); $loop->run();

Slide 73

Slide 73 text

ʮ໛༷͔ͳʁʯ • ·͋ɺ੾ͬͨுͬͨͷίʔυͰ͚͢ͲɺGithubʹ͋͛ͯ͋Δͷ ͰɺޙͰݟ͍ͯͩ͘͞ɻ • https://github.com/uzulla/Tinitter/tree/ reactize

Slide 74

Slide 74 text

DEMO • ͱ͍ͬͯ΋·͋ɺݟͯͳʹ͕ҧ͏Θ͚Ͱ΋… • phpͰ௚઀ىಈ͠ͱΔͳʂͱ͍͏࿩Ͱ͢ɻ

Slide 75

Slide 75 text

͜Εͷ͍͍ͱ͜Ζ • httpdෆཁʂʂ࠷ߴʂʂʢʁʣ • (ଟ͘ͷ৔߹)ςετ΍ϞοΫָ͕ɺαϒϦΫΤετ΍ϦϓϨΠ Έ͍ͨͳࣄ΋ָ • ͍ͭʹPHPʹ΋γϯάϧτϯ͕…ʂ • ϦΫΤετຖͷʮnewʯΛ࡟ݮͰ͖Δʢ͔΋ʣྫ͑͹ςϯϓ ϨʔτɺϧʔλɺઃఆͷϩʔυɺϩΨʔɺDB઀ଓ౳ • ʢͨͩɺطଘͷFW͸·ͣߟྀͯ͠ͳ͍ͷͰࣗ࡞ෆՄආʣ

Slide 76

Slide 76 text

͜ΕͷͭΒ͍ͱ͜Ζ • εʔύʔάϩʔόϧ($_GET,$_POST,$_SESSION,$_COOKIE) ͸࢖͑ͳ͍ • ࣮ߦίʔυ͕ίέΔͱαʔόʔ͕མͪΔ • PHP7͔Β͸ΤϥʔपΓ͕ྫ֎Ͱ੔උ͞ΕͨͷͰɺ͕Μ͹Ε͹ େৎ෉ʢͨͿΜʣ • ʮexitͨ͠Βࢮ͵ʯʮ͠ͳ͍Ͱʯ • ʮdieͨ͠Βࢮ͵ʯʮ͠ͳͳ͍Ͱʯ

Slide 77

Slide 77 text

• max_execution_time, memory_limitͰอޢ͕Ͱ͖ͳ͍ • ʢ౰ͨΓલ͕ͩʣൿ఻ͷhtaccess͸͔ͭ͑·ͤΜ͆ • ࣗ࡞ͨ͠ΓɺલஈͷhttpdͰ͔͚͹ྑ͍ • ҆શੑ΍ߴ଎ԽΛߟ͑ΔͱPDOར༻ͨΊΒ͏ • react΍amphp͕ఏڙ͢Δ΋ͷΛ͔ͭ͏ • ʮmysql͸͋ΔͬΆ͍͚ͲϙεάϨ͸ʁʯʮ…redisͱ͔͸ ͋ΔΑʯʮ΋͏͓ͬͨͧ͜͏ʂʂʯ(ຊՈͰͳ͚Ε͹͋Δࣄ ΋͋Δ)

Slide 78

Slide 78 text

஫ҙ఺ • ʢϑϨʔϜϫʔΫ౳Ҏ֎ͷɺຊ࣭ͷʣॲཧ͕࣌ؒ୹͍ͳΒ଎͘ ͳΔʢ͜ͱ΋͋Δʣ • ॏ͍ॲཧ͸େࠩͳ͍͠ɺΉ͠ΖѱԽ͢Δ͜ͱ͕ଟ͍ʢݸਓͷײ ૝ʣ • APIͳΒ͍͚Δ…ࣄ΋…͋Δ

Slide 79

Slide 79 text

ʮPHPͷྑ͍఺͕ͳ͍͡Όͳ͍ʁʯ • ͸͍ • PHPͷྑ͍఺Ͱ͋Δʮϩόετʯʮઃܭ͕ద౰Ͱ΋͋Δఔ౓ಈ ͘ʯ͕݁ߏͳ͘ͳΔ • ϑΝΠϧΛߋ৽ͨ͠Β࠶ىಈ͕ඞཁʹͳΔ • ϒϩοΩϯάͨ͠ΒࢮɺόονతͳॲཧΛͨ͠Β٧·ΔʢͷͰ ੾Γ཭͢ʣ

Slide 80

Slide 80 text

ͦΕͰ΋… • httpd͍Βͳ͍ɺphp͸php(-cli)͚ͩͰ͍͍ɻ • νϡʔχϯά͢Ε͹ɺܰྔͳ༻్ͳΒૣ͍ʢ͜ͱ΋͋Δʣ • ʮຊ෺ͷγϯάϧτϯ͕΄͔ͬͨ͠Μͩ…ʯ • ׽ͷϩϚϯ͕͋Δʢಠࣗղऍʣ

Slide 81

Slide 81 text

httpdΛphpʹ౷ҰͰ͖ ͨͧʂʢʁʣ

Slide 82

Slide 82 text

͸͍

Slide 83

Slide 83 text

͔͜͜Β༨ஊ εϥΠυͰݟͯΔਓ޲͚ ͕࣌ؒ༨ͬͨΒ࿩͠·͢

Slide 84

Slide 84 text

͜͏͍ͬͨΠϕϯτۦಈͷͳʹ΍Βྫ • reactPHP • areys • swoole

Slide 85

Slide 85 text

reactPHP • https://github.com/reactphp/http • ఀ଺͍͕ͯͨ͠ɺ෮׆ʂ • …ͱ͓΋͍͖΍ɺਖ਼݄ऴΘͬͨΒ·ͨࢭ·ͬͨ • ͱ͸͍͑ɺhttpd͍ͩͿྑ͘ͳͬͨɻͪΐͬͱલ·Ͱ͸ɺʮॻ ͘͜ͱ΋Ͱ͖ΔΑʂʯͱ͍͏αϯϓϧίʔυͬΆ͍ײͩͬ͡ ͨɻ • POSTͷ࣮૷͕؆қ͗͢Δ౳͕͋ͬͨ

Slide 86

Slide 86 text

reactPHP ͸ PSR-7ରԠ • PSR-7ରԠͰ࣮૷͞ΕͨͷͰɺPSR-7ରԠͷFW͕৐Δʂ • …ͱ͍͏ͷ͸ӕͰ͢ɻଟ͘ͷFW͸άϩʔόϧΫϦʔϯͰͳ͍ (ಛʹɺηογϣϯͳͲ) • ଟগؾΛ͚ͭΕ͹͍͚Δ • PSR-7ඇରԠͰ΋Laravel(ྫ:mnvx/laravel-reactphp)΍ Symfony͸όΠϯσΟϯά͕͋Δʢ͕…ʣ

Slide 87

Slide 87 text

ͦͷଞ • process manager͸ແ͍ͷͰɺӡ༻ʹ͸ผ్ systemd,supervisord,daemontoolsͳͲΛ࢖͏ • Α͘΋ѱ͘΋γϯϓϧ • ʮߴ଎ʯͱ͍͏ͷ͸ӕͰ͢ɻC֦ுͷeventͳͲΛ͍Εͯɺಛఆ ͷ༻్Ͱɺ஫ҙਂ͔͚͘͹ʮ͸΍͍ࣄ΋͋Δʯͱࢥ͏ • ΠϕϯτۦಈηʔϑͳίʔυΛॻ͘ͷ͸݁ߏ͍ͨ΁ΜɺಛʹDB ઀ଓͱ͔ɻ

Slide 88

Slide 88 text

aerys • https://github.com/amphp/aerys • amPHPͰ࣮૷͞Εͨhttpd • process manager͕͋Δʂ • PSR-7ͳʹͦΕ͏·͍ͷʁ(ಠࣗͷreq,res) • ݁ߏ׆ൃ

Slide 89

Slide 89 text

• Static file serving • WebSockets • Dynamic app endpoint routing • Name-based virtual hosting • Full TLS support • Customizable GZIP output compression • HTTP/2.0 support (pushͳͲ) • Middleware hooks

Slide 90

Slide 90 text

• άϨʔεϑϧϦελʔτ • ϓϩηεϚωʔδϟʢϫʔΧ਺มߋͳͲʣ • Unix domain sock • ࣮ߦϢʔβʔมߋ • Ϧόϓϩ • ηογϣϯʢRedisʣɺCookie • ͔ͳΓϓϩμΫγϣϯϨσΟײ͋ΔʢݸਓͷײͰ͢ʣ

Slide 91

Slide 91 text

route("GET", "/", function(Request $req, Response $res) { $res->end("

Hello, world.

"); }); $root = root($docrootPath = __DIR__);// static file serving $fallback = function(Request $req, Response $res) { $res->end("

Fallback \o/

"); }; return (new Host)->expose("*", 1337)->use($router)->use($root)->use($fallback);

Slide 92

Slide 92 text

swoole • https://github.com/swoole/swoole-src • จ۟ͳ͠ʢʁʣͷproduction readyʂʢςϯηϯτ͕࣮༻͠ ͍ͯΔʣ • high performance for PHP. • த՚+C֦ுύϫʔͰ̎̌̌ສഅྗʢʁʣ • ͪΌΜͱߋ৽͞ΕͯΔʂ͍͢͝ʂʂ

Slide 93

Slide 93 text

• ӳޠυΩϡϝϯτ͔ͳΓ੔උ͞Εͨʢͪΐͬͱલ·Ͱதࠃޠ͹ ͔Γͩͬͨʣ • Linux,OSX,WindowsͰ΋ಈ͘ʢͱॻ͍ͯ͋Δ͕ͪΐ͍ඍົʣ • Ϗϧυ͸ָʢґଘ͕ͳ͍ʣ • IDE helper΋͋Δ

Slide 94

Slide 94 text

׬શʹ༨ஊͳSwooleͷ͍͢͝ͱ͜Ζ • ͳΜͱʂʂʂPHPͳͷʹʂʂʂCoroutineͱChan(nelʣ͕͋ ΔʂʂʂGo͔Αʂʂʂ

Slide 95

Slide 95 text

$chan = new Channel(1024 * 256); co::create(function () use ($chan){ while(1){ co::sleep(0.5); $chan->push("A"); } }); co::create(function () use ($chan) { while(1){ $ret = $chan->pop(); if(!$ret){ co::sleep(0.1); continue; } echo $ret; } });

Slide 96

Slide 96 text

• ͳ͓ɺOSXͩͱڍಈ͕͓͔͔ͬͨ͠ɻ·ΕʹηάΔɺsleep͕ ࢭ·Δɺcoroutine͕ฒ૸͠ͳ͍ɻ • ʢͨΊͨ͠ͷ͕ͪΐͬͱલͳͷͰ΋͏࣏ͬͯΔ͔΋ʣ • LinuxͩͱͪΌΜͱ͏͘͝งғؾ • ඇಉظ͚ͩͰ͸ͳ͍ɺϚϧνεϨουϚϧνϓϩηεͰ࢖͑Δ ϩοΫ΍mmapɺ҆શͳϩʔΧϧredisΈ͍ͨͳtableɺϓϩη εϚωʔδϟ΋͋Γ…ຊՈʹऔΓࠐ·Εͯ͘Εʂʂʂʂ • ਖ਼௚ɺ֦ு͞Ε͗͢Ͱ͸

Slide 97

Slide 97 text

• HHVMͱ͕ͪ͏ํ޲ੑͰʢPHPͷ··ʢʁʣͰʣਐԽ͍ͤͯͬ͞ ͍ͯΔΑ͏ʹΈ͑ͯɺݸਓతʹ͸޷ײ͕͋Δ • ΈΜͳ͓ࠔΓʹͳΔDNSϦκϧόʢͷඇಉظͷ΍ͭʣ·Ͱ૷උ • ʮPHPʹcoroutinͳ͍΍Ζʯʮͤ΍ͳʯ • ࣮ࡍ݁ߏߴ଎ͩ͠ɺເͱϩϚϯ͕͍͢͝

Slide 98

Slide 98 text

͜ΕͰؾܰʹwaitΛ͔͚Δ͜ͱ΋Ͱ͖Δ͠ɺདྷ೥ͷISUCON͸͜Ε ͩʂʢҧ ʢISUCON༑ୡืू͍ͯ͠·͢ʣ

Slide 99

Slide 99 text

΍ͬͨʂ͜ΕͰ͍͚Δʂʂ • C֦ுͳΜͩΑͳ͋… • ʮʢ͜͜Ͱ͍͏ʣେ౷Ұײʯ͸ബ͍ʢผʹ౷Ұ͠ͳ͍͍͚ͯ͘ Ͳʣ

Slide 100

Slide 100 text

࠶ܝʣଞͷݴޠ͸ɺC֦ு͸ʮlibʯଆ ʹೖΔ • phpͩͱɺΠϯετʔϧͨ͠phpͷԼͷmodulesʹ͸͍Δ • php.iniʹextensions=hoge.so • Ͱ΋·͋ɺdlؔ਺΍࣮ߦ࣌Ҿ਺͔ͭ͑͹͍͍ΜͩΑͶʂ • ͱ͍͏͜ͱͰɺಥવͰ͕͢ʮ΄͔ͷݴޠͷC֦ுͷී௨ʯΛճऩ ͢Δ͜ͱ͕Ͱ͖·ͨ͠Ͷɺʢಥવͷճऩʣ

Slide 101

Slide 101 text

ʢISUCON஥ؒืूதͰ ͔͢ΒͶʁʣ

Slide 102

Slide 102 text

͜͜·Ͱ༨ஊ

Slide 103

Slide 103 text

͸͍

Slide 104

Slide 104 text

ࢲʮͲ͏Ͱ͔͢ʁhttpdʹͳΓ·ͨ͠Αʯ ʁʮϑΝΠϧ͕ͨ͘͞Μ͋Δͷ͕ͳ͋…ʯ ࢲʮͳΜͩͱʯ

Slide 105

Slide 105 text

̍ϑΝΠϧʹ͠ͳ͚Ε͹ʢ࢖໋ײʣ • ΍ͬͨΔ͔ʂ

Slide 106

Slide 106 text

౷Ұ͍ͯͧ͘͠ʂ

Slide 107

Slide 107 text

Phar • PHPͷίʔυ΍σʔλΛύοΫͰ͖Δ΋ͷ • composer.phar͸͝ଘ஌Ͱ͠ΐ͏ • file_get_contents΍fopenʹphar://ʙΛ౉͢ͱத͕ݟΕ Δ • stubʹmainతͳ΋ͷΛηοτ͢Ε͹ɺpharϑΝΠϧΛ௚઀ phpͰ࣮ߦͰ͖Δ • php hoge.phar

Slide 108

Slide 108 text

pharͷ࡞Γํ • boxΛ͔ͭ͏ͱ௒؆୯ • https://github.com/box-project/box2 • ʮpharΛͭ͘Δͷ͸໘౗ͳͷΛղܾ܅ʯ • box.jsonΛॻ͚ͩ͘ʂ • ͪΌΜͱComposerͰ͍Εͨ΋ͷ΋ಈ͘

Slide 109

Slide 109 text

ͱΓ͋͑ͣɺ͖ͬ͞ͷ reactPHPͰ࡞ͬͯΈΔ

Slide 110

Slide 110 text

box.json { "directories": ["htdocs","lib","templates","vendor"], "files": ["config.php","main.php"], "main": "main.php", "output": "react_tinitter.phar", "stub": true }

Slide 111

Slide 111 text

$ php -d phar.readonly=false ~/bin/box.phar build Building... • -d phar.readonly=false͸σϑΥϧτͰ͸Pharͷʮॻ͖ࠐ Έʯ͸ېࢭ͞Ε͍ͯΔͷͰࢦఆɻphp.iniͰ΋͍͍͚Ͳͬͪ͜ ͷ΄͏͕ૣ͍ΑͶ

Slide 112

Slide 112 text

• ͔ͳΓ͔͔࣌ؒΓ·͢ $ time php -d phar.readonly=false ~/bin/box.phar build Building... real 4m13.202s user 1m51.980s sys 1m44.934s

Slide 113

Slide 113 text

real 4m13.202s

Slide 114

Slide 114 text

• ʮ…͖ͬͱxdebug͕͸͍ͬͯΔ͔Βͩ…ʯ • ʮ֦ுΛɺ-dͰ௥Ճ͸Ͱ͖Δ͕ɺ࣮ߦ࣌ʹ࡟আ͸…ʯ • ʮ….gitҎԼ·ͰύοΫ͞Εͯͳ͍͔͜Εʁʁʯ

Slide 115

Slide 115 text

͔͠͠ɺੈք͸autoload͚ͩͰ͸ͳ ͍ • ex: ςϯϓϨʔτϑΝΠϧɺίϯϑΟά • (Tinitterͷ৔߹͸sqlite΋) ྫ PHP Warning: file_get_contents(public/index.html): failed to open stream: No such file or directory in phar:///tmp/example.phar/bin/main on line 26

Slide 116

Slide 116 text

Phar::interceptFileFuncs(); • ࠷ߴؔ਺file_get_contents΍fopenͳͲΛԣऔΓ͠·͢ • ͕͢͞C֦ுɺͦ͜ʹγϏΕΔಌΕΔ • /Ͱ࢝·Βͳ͍ʢ૬ରύεʣ৔߹ɺݱࡏͷPhar಺͔ΒಡΉ • __DIR__΋͔ͭ͑Δ(phar:///tmp/example.phar/bin) • ʮେମʯͳΜͱ͔ಈͧ͘ɻ • reactͷ੩తϑΝΠϧ഑৴ΞυΦϯ΋ಈ͘

Slide 117

Slide 117 text

͔͠͠PDO͕͏͔͝ͳ͍ $ php react_tinitter.phar Slim Application Error: Type: PDOException Code: 14 Message: SQLSTATE[HY000] [14] unable to open database file

Slide 118

Slide 118 text

Phar::mount() • Phar֎ͷϑΝΠϧ΍σΟϨΫτϦΛ͔͋ͨ΋Phar಺ʹ͋Δ͔ͷ Α͏ʹࢀর͢Δ͜ͱ͕Ͱ͖Δʢલड़ͱ૊Έ߹ΘͤΔࣄʹͳΔʣ • ྫɿpharͷ֎ʹ͋Δsqlite.dbΛɺதʹ͋ΔΑ͏ʹ͢Δ Phar::mount( 'sqlite.db', dirname(Phar::running(false)).'/sqlite.db' );

Slide 119

Slide 119 text

• ͜ͷ࣌ɺ__DIR__͕Phar಺෦ʹ͔Θͬͯ͠·͏ͷͰ஫ҙ • Α͘΍ΔPDO("sqlite:".__DIR__."/sqlite.db")Έ͍ͨ ͳͷ͸ɺ૬ର౳ʹॻ͖׵͑·͠ΐ͏

Slide 120

Slide 120 text

Phar::running() • Pharʹ͠ͳ͍ͱಈ͔ͳ͍ͱ͔ແཧͳͷͰɺPharԽ͞Ε͍ͯΔ͔ ൱͔Λ஌ΔͨΊʹ͔ͭ͏ • __FILE__ Έ͍ͨͳ΋ͷ͕ฦ͞ΕΔɺ֎ͳΒfalse • Ҿ਺ʹfalseͰɺPharࣗମͷύεΛฦ͢ $isUnderPhar = (Phar::running()!=="") ;

Slide 121

Slide 121 text

ͱ͍͏͜ͱͰ • ϒʔτετϥοϓʹͳΔmain.phpͷઌ಄ʹॻ͘

Slide 122

Slide 122 text

Ͱ͖ͨʂ • DEMO • ͯ͠΋ɺผʹ୅ΘΓө͑͠ͳ͍͔Βͳ…

Slide 123

Slide 123 text

ίʔυ͕େ౷ҰͰ͖ͨʂ

Slide 124

Slide 124 text

͸͍

Slide 125

Slide 125 text

ࢲɿʮ͜ΕͰ̍ϑΝΠϧʹͳͬͨʂʯ ʁɿʮ͑ͬɺίʔυͱόΠφϦͱผ͡ΌΜʯ ࢲɿʢ΋͸΍ͦΕ͸೉บͳͷͰ͸ʁʁʁʁʣ

Slide 126

Slide 126 text

சੜɺ͞Βʹ౷Ұͩʂʂ • ͱ͍͏͜ͱͰͭͳ͍͖͛ͯ·͠ΐ͏

Slide 127

Slide 127 text

ग़མͪʣ΍ͬͨʂେ౷Ұͩʂʂ $ cat /usr/bin/php main.php > one-pack-php $ ./one-pack-php -r \ "eval('?>'.substr(file_get_contents('one-pack-php'), strlen(file_get_contents('/usr/bin/php'))));" • ̍ϑΝΠϧʹόΠφϦͱPHP͕͸͍ͬͨͧʂʂ

Slide 128

Slide 128 text

ղઆ cat php main.php > one-pack-php ! [PHPόΠφϦ] + [PHPίʔυ] = [PHPόΠφϦ|PHPίʔυ]

Slide 129

Slide 129 text

$ ./one-pack-php \ # ͭͳ͛ͨPHP -r \ # Ҿ਺ͰPHPίʔυࢦఆɺҎԼPHPίʔυ "eval('?>'. // '

Slide 130

Slide 130 text

ʮ΍ͬͨͥʂ౷Ұͩʂʯ

Slide 131

Slide 131 text

օ͞Μͷײ૝ • ʮ͏ͦͩΖʂʁʯ • ʮ͏ͦͰ͸ͳ͍ʯ

Slide 132

Slide 132 text

օ͞Μͷײ૝̎ • ʮਖ਼ؾͷࠫଡͰ͸ͳ͍ʯ • ʮ͔ͨ͠ʹʯ

Slide 133

Slide 133 text

ී௨ͷͨΊʹ͸ඇৗࣝΛ͠ͳ͚Ε͹…ʢʣ

Slide 134

Slide 134 text

΍ͬͨʂ͜ΕͰউརʂʂ • ͱ͓΋͍͍͕ͨ…

Slide 135

Slide 135 text

ͨΜͳΔPHPιʔεͳΒಈ͕͘… • phar͸fsʹ͓͔Ε͍ͯͳ͍ͱopenͰ͖ͳ͍ • ʢphp ext/pharͷιʔεΛΈΔͱ…ʣ • ͙͙͙

Slide 136

Slide 136 text

ͦͷ͜Ζࢲ͸ YAPC::Okinawaͷҝɺ ԭೄʹ͍ͨ

Slide 137

Slide 137 text

• ʮ…͏ͬ͢Βؾ͍͍͕ͮͯͨɺ͜Ε͸ແཧͳͷͰ͸…ʯ • (ԭೄ໊෺ίʔώʔ๐੝ΛҿΉ) • ʮ͍ͬͯ͏͔ɺfs͕ͳ͍؀ڥͳ͍΍Ζɺfs͔͓ͭ͏ʯ

Slide 138

Slide 138 text

• ͱ͍͏͜ͱͰਖ਼ؾʹͳΓPharͱPHP࿈݁Ҋ͸ϘπʹͳΓ·ͨ͠ • ʢʮ౷Ұʯͷֶͼ͸͋ͬͨʣ • ʢ؇ืɿΑ͍ಀ͛ಓʣ

Slide 139

Slide 139 text

୅Ҋ bash ࣗݾղౚʹΑΔ࣮ߦ • ͪ͜Β͸ඇৗʹ༝ॹਖ਼͍͠खஈͰ͢Ͷ • php, codeΛղౚ࣮ͯ͠ߦ

Slide 140

Slide 140 text

#!/bin/bash # head.sh function finish () { cd - rm -rf $RUNPACKDIR exit } RUNPACKDIR=`mktemp -d /tmp/runpackphp.XXXXX` trap 'finish' {1,2,3,15} ENDLINE=$(grep --line-number --text "^##ENDLINE$" $0 | sed 's/:.*//') : $((ENDLINE++)) tail -n +$ENDLINE $0 | tar xzf - --directory $RUNPACKDIR cd $RUNPACKDIR $RUNPACKDIR/php $RUNPACKDIR/main.php cd - exit ##ENDLINE

Slide 141

Slide 141 text

#!/bin/sh # build.sh cd ../ tar czf --exclude-vcs selfbash/ball.tgz \ htdocs lib templates vendor \ sqlite.db main.php config.php php cd - cat head.sh ball.tgz > selfbash.sh chmod +x selfbash.sh

Slide 142

Slide 142 text

Ͱ͖ͨʂ • demo

Slide 143

Slide 143 text

΋͠࢖͏ةಞͳਓ΁ͷ஫ҙ • ্ͷίʔυͷtar͸gnu tarͰ͢ɺosxͰ΍ΔͳΒbrewͰ͍Ε ΔͳΓ͍ͯͩ͘͠͞ • ʢ͋Δ͋Δ…ʣ • sqlite.db·ͰಥͬࠐΉͱɺ౰વ࠶ىಈͷͨͼʹר͖໭Γ· ͢ɻMysqlͱ͔࢖͍ͬͯͩ͘͞ɻ

Slide 144

Slide 144 text

ʮ͍ͭʹʂେ౷Ұʂͩʂʯ

Slide 145

Slide 145 text

͸͍

Slide 146

Slide 146 text

ࢲɿʮ͜ΕͰͲ͏Ͱ͔͢ʂʯ ʁɿʮ͍ͬͯ͏͔ɺPHP͸Dockerͱ͔ʹͯ͠΋ΠϝʔδαΠζͰ ͔͗͢ΔΘɺPHP͸͜Ε͔ͩΒʯ • ʢ͍ͬͯ͏͔͖ͬ͞ͷͰಥવPHP͕ϫϯόΠφϦͰίϐʔͯ͠Δ ॴ͕෍ੴͰ͢Ͷʣ

Slide 147

Slide 147 text

சੜখͯ͘͞͠΍Δʂʂ • ੎͍Ͱɺ̍όΠφϦͰɺখ͍͞PHPΛͭ͘Δ • ʢಥવͷϧʔϧɿҰݸͷϑΝΠϧͰ·ͬ͞Βͳ؀ڥʹ΋͍ͬͯ ͬͯ͏͚͝͹OKʣ

Slide 148

Slide 148 text

ઌऔΓΦν৘ใ $ ./configure --disable-all --enable-mbstring --enable-json $ make all $ cp sapi/cli/php /path/to/php

Slide 149

Slide 149 text

Ͱ͖ͨʂʂ • ʮMac΍LinuxͰ͸͍͚ͨΑʂʯ • ʮ͍΍…Զ͸Docker͔ͩΒ…ʯ • ʮνοʯ

Slide 150

Slide 150 text

alpineͰಈ͘࠷খͷPHPΛ࡞͍ͬͯ͘ • ຌ༱ʹUbuntuͩͱ1GB͘Β͍͋Δɺ͔ͨ͠ʹ͖ͼ͍͠ • ެࣜϨγϐͩͱ60Mbyte͘Β͍ • ͑ͬɺ͜ΕͰσΧΠͷʁ

Slide 151

Slide 151 text

Φν ʢ7.2.2ͷྫͰ͢ʣ ./configure \ --disable-cgi --without-pear \ --enable-ftp --enable-mbstring --enable-mysqlnd \ --with-openssl --with-zlib \ --disable-libxml --disable-dom --disable-xml \ --disable-simplexml --disable-xmlreader --disable-xmlwriter • ެࣜͷDockerFileΛվ଄͢Δͱָ • όΠφϦΛ͵͍ͯɺૉͷ΋ͷʹίϐʔ͔ͯͭ͠͏ • SSL͸࠷ॳ͔Β͋Δɺlibxml͕Ͱ͔͍ΜͩΑͳ͋…

Slide 152

Slide 152 text

݁Ռ $ docker image list REPOSITORY TAG IMAGE ID CREATED SIZE miniphp latest fa69ea70ab4c 2 weeks ago 11.4MB php-7.2.2-cli-alpine3.7 1.0 0a93147b91ab 2 weeks ago 62.5MB

Slide 153

Slide 153 text

miniphp 11.4MB php-7.2.2-cli-alpine3.7 62.5MB • ʮ11Mbyteͩͧɺ΋͏ΏΔͯ͘͠Ε…ʯ

Slide 154

Slide 154 text

ͨͩɺ๻͸ผʹDockerΛ͔ͭΘͳ͍ͷͰ • ࣮ࡍʹ͜Μͳ͜ͱΛීஈ͸͠ͳ͍ • ͱΓ͋͑ͣ࡞ͬͨΜͩ

Slide 155

Slide 155 text

ʢ͜ͷ݁Ռ͸·ͩ΋͍ͬͯͬͯͳ͍ͷͰɺଓ͖͕͋Δ͔͸Θ͔Γ· ͤΜʣ

Slide 156

Slide 156 text

ͱ͍͏͜ͱͰɺ • PHP͕httpdʹͳΔΑ͏ʹͯ͠ • PHPΛϫϯόΠφϦʹͯ͠ • ࣗݾղౚܗࣜͰύοΫͯ͠ • ̍ϑΝΠϧʹ͢Δ͜ͱʹΑΓ • ଞͷݴޠͷਓ͕͍͏ʢʁʣʮී௨ʯͬΆ͍ʢಠࣗݟղʣφχΧ ͕Ͱ͖ͨ

Slide 157

Slide 157 text

ʮ͜Ε͕ϫΠͷେ౷Ұ΍ʂʯ

Slide 158

Slide 158 text

! • ·ͱΊʹೖ͍͖ͬͯ·͢

Slide 159

Slide 159 text

·ͱΊ • ແ೉ʹapache+mod_phpɺ͋Δ͍͸nginx+php-fpmΛ࢖͍· ͠ΐ͏ • ͦΕͰ΋νϟϨϯδ͢Δਓɺͥͻ๻ͱ͓࿩͠·͠ΐ͏ • ʮདྷ೥͋ͨΓͷISUCONʹSwooleͰग़Δ༑ୡΛืू͍ͯ͠· ͢ʂʯ

Slide 160

Slide 160 text

ʮԶ͸઴࢝͘Ίͨ͹͔Γ͔ͩΒͳɹ ɹ͜ͷ͸ͯ͠ͳ͘ԕ͍େ౷ҰΛΑ…ʯ (to be continued...) (࣭໰Ͳ͏ͧ) (ͳ͚Ε͹༨ஊ)