web scaling with nginx / php · php versions support 3 ปแต่ละ version - fully support 2...

57
Web Scaling with NginX / PHP 35 th WUNCA 21 กรกฏาคม 2560

Upload: others

Post on 09-Apr-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Web Scaling with NginX / PHP

35th WUNCA 21 กรกฏาคม 2560

Page 2: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

สาหรบทานทมาแลว...ตดตงเครองมอประกอบการบรรยาย ดงน

1. Virtual Box2. Download ไฟล ova จากทใดทหนงดงน

a. ตาม link ทไดรบทาง email ( https://goo.gl/ATU2MF )b. \\103.1.161.67

3. Import appliance ไฟล ova ○ ตก Reinitialize the MAC address of all network cards○ Set Network Adapter

4. ตดตงโปรแกรม cmder http://cmder.net/ หรอโปรแกรมอน ๆ สาหรบ ssh

5. ตดตงโปรแกรม Redis Desktop Manager https://redisdesktop.com/

6. Slide ประกอบการบรรยาย https://goo.gl/buwLZ3

Page 3: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ทมวทยากร

ภทรชย ไชยมงคล (ออฟ)

วทยากรนกวชาการคอมพวเตอร

[email protected]

ฝายเทคโนโลยสารสนเทศเพอการศกษาสานกคอมพวเตอร มศว

มหทธวฒน รกษาเกยรตศกด (หนง)

ผชวยวทยากรนกกวชาการคอมพวเตอร ชานาญการพเศษ

[email protected]

ฝายระบบคอมพวเตอรและเครอขายสานกคอมพวเตอร มศว

ธญยธรฐ พงษเฉลม (ตน)

ผชวยวทยากรนกวชาการคอมพวเตอร

[email protected]

ฝายระบบคอมพวเตอรและเครอขายสานกคอมพวเตอร มศว

Page 4: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ผลงานทมงาน

SWU Mobile ระบบยนยนตวตน งานรบนสตใหม มศว

Page 5: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

● เวบไซตลม● เวบไซตทางานชา● เพมฮารดแวรแลว Server แรงแลวทาไมเวบยงลมอก

คณเคยพบปญหาเหลานมย?

Page 6: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

"It's time for web servers to handle ten thousand clients simultaneously"

- Daniel Kegel, C10K manifest author http://www.kegel.com/c10k.html

Page 7: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

● NginX และ Apache Web Server● PHP และ PHP-FPM● Redis สาหรบ Caching Database● Web Scaling Topics

หวขอบรรยาย

Page 8: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ความรเบองตนมพนฐาน...

● การใชงาน Linux เบองตน● การบรหารจดการ Web Server ● การใชงาน command line ได● การใชงาน Virtual Machine

Page 9: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

เครองมอประกอบการบรรยายตดตง VM ประกอบการบรรยาย

Virtual Box

● CPU 1-2 cores● RAM 512 - 1024 MB● Network Adapter

○ 1 NAT○ 2 Bridge

● Storage ○ ไฟล vmdk ทไดเตรยมไวให

■ ตดตง CentOS 7■ NginX, Apache, PHP, Redis■ Wordpress + sample app

Command line tool

Cmder http://cmder.net/

Page 10: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

NginX Webserver

Page 11: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

NginX vs Apache

NginX

- ถกสรางมาในป 2002 เพอแกปญหา C10k- asynchronous, non-blocking,

event-driven connection handling algorithm

- ถกออกแบบมาใหมประสทธภาพสงใชงานทรพยากรเครองไดคมคา รองรบการเตบโตของเวบไซตไดด

- เหมาะสาหรบเวบไซตทมการใชงาน พรอมกนสง (High Concurrency)

Apache

- ถกสรางมาตงแตยค 1990 เหมาะสาหรบ Internet ในยคเรมตน

- multi-processing modules (MPM) ถกออกแบบมาใหสราง process ขนมาใหมตอ connection ทเพมขน

- เปน Web Server ทเหมาะกบการใชงานทวไปม feature หลากหลาย มสวนขยายใหใชงานมาก

- ม Scalability นอยเนองจากการใชงาน CPU และ Memory ทมากขนเมอม connection เขามามาก

Page 13: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่
Page 14: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

การตดตง NginXyum install epel-release

yum install nginx

chkconfig nginx on

nginx -v

service nginx start

Page 15: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ไฟล configNginX

● /etc/nginx/nginx.conf● /etc/nginx/conf.d/*.conf

○ default.conf○ ssl.conf

Apache (httpd)

● /etc/httpd/conf/httpd.conf● /etc/httpd/conf.d/*.conf

Page 16: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/nginx/nginx.conf

Page 17: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/security/limits.conf

Set max open filesulimit -a

Page 18: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/nginx/conf.d/default.conf

Page 19: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

https/etc/nginx/conf.d/ssl.conf

Page 20: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Generate ssl keycd /etc/sslsudo openssl genrsa -out secure.key 2048sudo openssl req -new -key secure.key -out secure.csrsudo openssl x509 -req -days 365 -in secure.csr -signkey secure.key -out secure.crt

Page 21: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Nginx config references & resourceshttps://github.com/h5bp/server-configs-nginx *** Best practice NginX config

https://github.com/denji/nginx-tuning

http://www.aosabook.org/en/nginx.html *** NginX Architecturehttp://www-01.ibm.com/software/webservers/httpservers/doc/v1319/9atperf.htm Apache Process Model

Page 22: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Apache Bench (ab)ab -n 1000 -c 10 http://192.168.1.1/index.html

-n จานวน request

-c จานวน concurrent user

Page 23: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Stress Test: Static fileshtml, javascript, css, image

Page 24: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

PHP

Page 25: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

PHP VersionsSupport 3 ปแตละ version

- Fully support 2 ปแรก - Critical security เทานน ปท 3

ออกเวอรชนใหม ทกชวงปลายป

*** PHP 7.2 ธนวาคม 2017

New features และการ migration

http://php.net/manual/en/appendices.php

http://php.net/eol.php

http://php.net/supported-versions.php

Page 27: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่
Page 28: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่
Page 29: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่
Page 30: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่
Page 31: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

การตดตง php-fpmyum install php71w-common php71w-mysql php71w-opcache

php -v

service php-fpm start

Page 32: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/nginx/conf.d/default.conf

Page 33: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ไฟล config phpphp

● /etc/php.ini● /etc/httpd/conf.d/php.conf

php-fpm

● /etc/php-fpm.conf● /etc/php-fpm.d/www.conf

Page 34: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/php-fpm.conf

Page 35: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/php-fpm.d/www.conf

Page 36: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

PHP Resourceshttps://serversforhackers.com/t/php Video การ config PHP-FPM, NginX, Apache

http://www.phptherightway.com

https://laracasts.com/skills/php

Page 37: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Stress Test: php 5.6phpinfo, wordpress

Page 38: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

PHP 7.1yum install plugin-replace

yum replace php5w-common --replace-with=php71w-common

แก httpd ใหรจก php7 /etc/httpd/conf.d/php.conf

restart service: httpd, nginx, php-fpm

Page 39: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Stress Test: php 7.1phpinfo, wordpress

Page 40: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

งานรบนสตใหม มศว พ.ย. 2556 - Unique Visitors 117,432

NginX / PHP-FPM

Oracle

Page 41: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Redis

Page 42: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

RedisRedis คอ in-memory data structure store, สามารใชเปน database, cache และ message broker

- รองรบโครงสรางขอมลหลายแบบ เชน strings, hashes, lists, sets, sorted sets with range queries, bitmaps, และอน ๆ

- Redis รองรบเกบเกบขอมลลงดสก- ม high availability สง และสามารถทา Redis Cluster ได

Page 43: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Redisการดาเนนการทรองรบ เชน

- appending to a string;- incrementing the value in a hash; - pushing an element to a list; - computing set intersection, union and difference- getting the member with highest ranking in a sorted set.

Redis ม api สามารถใชงานไดจากภาษาโปรแกรมสวนใหญ:

- PHP, Ruby, Python, Swift, Objective-C, Node.js, Java, Go, PL/SQL, VB, C, C++, C# และอน ๆ

Redis สามารใชงานไดบนระบบปฏบตการ Linux, OS X

ไมมการสนบสนนอยางเปนทางการบน Windows แต Microsoft มการพฒนา Redis Win-64 port.

Page 44: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Redis:6379

PHP-FPM

Mysql:3306

NginX

Page 45: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

การตดตง redisyum install redis

redis-cli -v

Page 46: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/redis.conf

Page 47: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

redis-cli

Redis CLI / tools

Page 48: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

ตวอยางการใชงาน redis ใน PHP/var/www/html/query/index-cache.php /var/www/html/query/config/cache.php

Page 49: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Stress Test: PHP + Redis

Page 50: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Web Scaling

Page 51: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Apache127.0.0.1:81

NginX127.0.0.1:80

proxy_pass: phpClient static files

proxy_pass NginX + Apache

Page 52: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

proxy_pass

/etc/nginx/nginx.conf

/etc/nginx/conf.d/proxy_pass.conf

Page 53: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Load Balancing NginXNginX /

PHP-FPM192.168.1.101

NginX192.168.1.100Client

Load balancer

App 1

NginX / PHP-FPM

192.168.1.102

App 2

Redis / MySQL

192.168.1.103

Cache / Database

Page 54: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

/etc/nginx/conf.d/load_balancer.conf

Page 55: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

Resourcehttps://serversforhackers.com/s/load-balancing-with-nginx Video การ config load balance NginX

http://nginx.org/en/docs/http/load_balancing.html

https://www.nginx.com/resources/admin-guide/load-balancer/

Page 56: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่

NginX Apache

Request / Sec Time / Request (ms) Request / Sec Time / Request (ms)

HTML 1263 8 832 11

Javascript 1000 8 400 23

CSS 1000 9 700 14

Image 1000 7 200 48

PHP 5.6 PHPinfo 700-800 700 14

PHP 5.6 WordPress 15 600 14 600

PHP 7.1 PHPinfo 800 12 770 12

PHP 7.1 WordPress 33 290 29 330

Database 461 21 428 23

Redis 573 17 500 17

Redis WordPress 44 222 44 226

Proxy Pass 3700 2 - -

Page 57: Web Scaling with NginX / PHP · PHP Versions Support 3 ปแต่ละ version - Fully support 2 ปแรก - Critical security เท่านัน ปที 3 ออกเวอร์ชันใหม่