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

Post on 09-Apr-2020

6 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Web Scaling with NginX / PHP

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

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

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

ทมวทยากร

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

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

phattarachai@g.swu.ac.th

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

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

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

mahuttha@.g.swu.ac.th

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

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

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

tunyaton@g.swu.ac.th

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

ผลงานทมงาน

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

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

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

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

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

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

หวขอบรรยาย

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

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

เครองมอประกอบการบรรยายตดตง 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/

NginX Webserver

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 เขามามาก

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

yum install nginx

chkconfig nginx on

nginx -v

service nginx start

ไฟล 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

/etc/nginx/nginx.conf

/etc/security/limits.conf

Set max open filesulimit -a

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

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

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

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

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

-n จานวน request

-c จานวน concurrent user

Stress Test: Static fileshtml, javascript, css, image

PHP

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

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

php -v

service php-fpm start

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

ไฟล config phpphp

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

php-fpm

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

/etc/php-fpm.conf

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

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

http://www.phptherightway.com

https://laracasts.com/skills/php

Stress Test: php 5.6phpinfo, wordpress

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

Stress Test: php 7.1phpinfo, wordpress

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

NginX / PHP-FPM

Oracle

Redis

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 ได

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.

Redis:6379

PHP-FPM

Mysql:3306

NginX

การตดตง redisyum install redis

redis-cli -v

/etc/redis.conf

redis-cli

Redis CLI / tools

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

Stress Test: PHP + Redis

Web Scaling

Apache127.0.0.1:81

NginX127.0.0.1:80

proxy_pass: phpClient static files

proxy_pass NginX + Apache

proxy_pass

/etc/nginx/nginx.conf

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

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

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

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/

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 - -

top related