Bottom to Top Stack Optimization with LAMP
-
Upload
katzgrau -
Category
Technology
-
view
1.756 -
download
3
description
Transcript of Bottom to Top Stack Optimization with LAMP
![Page 1: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/1.jpg)
Bo#om to Top Stack Op.miza.on
With LAMP + CodeIgniter
![Page 2: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/2.jpg)
Server side: It’s About I/O
Network I/O Disk I/O CPU
![Page 3: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/3.jpg)
Server/Client side: Minimize and Cache Assets
![Page 4: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/4.jpg)
Op.miza.on Roadmap
• System • MySQL • Apache • PHP • Framework • Applica.on
![Page 5: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/5.jpg)
Tes.ng
• Frontend – Yslow
• Backend – `ab` – vmstat
![Page 6: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/6.jpg)
LAMP Configura.on
• By default, for “compa.bility” • You’re responsible for your boxes
![Page 7: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/7.jpg)
System
• Don’t swap – Swap in itself isn’t bad, swapping out is
• Get rid of non-‐essen.al processes • a.me – Filesystem op.on to write ‘last-‐accessed’ .me to everything $ cat /
• If you’re a badass, check into XFS
![Page 8: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/8.jpg)
a.me $ cat /etc/fstab proc /proc proc defaults 0 0 /dev/sda1 / ext3 defaults,errors=remount-ro,noatime 0 1
/dev/sda2 none swap sw 0 0
![Page 9: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/9.jpg)
MySQL • Don’t “SELECT *” – If the data you need is in an index, MySQL will grab that
• Column indexes – Foreign key type matching – Be smart about column size – Integrity checks? Try NOT dele.ng instead
• Batched queries • Key Buffer • Query Buffer • Engine – InnoDB, or go crazy and use Percona XtraDB with XFS
![Page 10: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/10.jpg)
Apache
• Kill unneeded modules • Tune MaxClients • Eliminate .htaccess, if possible • Kill access logs • Disable ETags if not used • Expires Headers • Compression • Don’t serve sta.c content (if you can avoid it)
![Page 11: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/11.jpg)
Benchmarking
• Apache bench is most common – Tests Requests Per Second
• Use vmstat to watch swap usage • Send 1,000 requests, concurrency of 10
$ ab –n 1000 –c 10 http://getsparks.org
![Page 12: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/12.jpg)
Finished 10000 requests Server Soiware: Apache/2.2.12 Server Hostname: getsparks.org Server Port: 80 Document Path: / Document Length: 9865 bytes Concurrency Level: 10 Time taken for tests: 6.040 seconds Complete requests: 10000 … Requests per second: 1655.63 [#/sec] (mean) Time per request: 6.040 [ms] (mean) Time per request: 0.604 [ms] (mean, across all concurrent requests) Transfer rate: 16260.41 [Kbytes/sec] received Percentage of the requests served within a certain .me (ms) 50% 10 … 100% 60 (longest request)
![Page 13: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/13.jpg)
$ vmstat 5 procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 55152 53212 15128 69172 0 0 0 1 0 1 0 0 100 0 1 0 55152 54428 15132 69172 0 0 0 20 56 48 0 0 100 0
![Page 14: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/14.jpg)
Killing Apache Modules $ a2dismod Your choices are: alias auth_basic authn_file authz_default authz_groupfile authz_host authz_user autoindex cgi deflate dir env expires mime negotiation php5 rewrite setenvif status
![Page 15: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/15.jpg)
Tuning MaxClients “You can, and should, control the MaxClients se5ng so that your server does not spawn so many children it starts swapping.”
-‐ Apache.org Usually in h#pd.conf or apache2.conf Find average size of h#p process with `top` (x). How much RAM to dedicate to Apache? (n). MaxClients = floor(n / x) Play around with that and Apache Bench `ab`
![Page 16: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/16.jpg)
Adding Expires Headers <IfModule mod_expires.c> ExpiresActive on ExpiresByType image/gif "access plus X days" ExpiresByType image/jpeg "access plus X days" ExpiresByType image/png "access plus X days" ExpiresByType text/css "access plus X days" ExpiresByType text/js "access plus X days" ExpiresByType text/javascript "access plus X days" ExpiresByType application/javascript "access plus X days" ExpiresByType application/x-javascript "access plus X days" ExpiresDefault "access plus X days" </IfModule>
![Page 17: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/17.jpg)
mod_deflate
• Compress your stuff, comes with Apache
$ a2enmod deflate
# … configure in global or vhost config …
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# … restart Apache
$ apache2ctl -k restart
![Page 18: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/18.jpg)
Killing ETags
$ a2enmod header # open apache.conf or h#pd.conf, add: Header unset ETag FileETag None $ apache2ctl –k restart
![Page 19: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/19.jpg)
HTTP Accelerators
• Serving sta.c content is VERY expensive for Apache
• Varnish, Squid
$ apt-‐get install varnish # go to varnish-‐cache.org
![Page 20: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/20.jpg)
PHP
• APC – Caches PHP Op-‐code, can use as cache – Faster than memcache for one box
• PHP 5.3 • mysqlnd • Output buffering tuning, send data to client more frequently
![Page 21: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/21.jpg)
APC Install
$ apt-‐get install php-‐pear … $ pecl install apc Then take a look at cache size: apc.shm_size
![Page 22: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/22.jpg)
Framework (CodeIgniter)
• Page Caching – Personalized info, uh oh.
• Query Result Caching • Configura.on – Disable verbose logging – Output compression – config/database.php: autoinit?
• Minifying, combining assets – ‘assets’ Spark
![Page 23: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/23.jpg)
Applica.on
• Most op.miza.on does not occur within code • Use built-‐in PHP func.ons • Use associa.ve arrays (hashes, dic.onaries) • Batch DB Inserts, Selects • Don’t use references unless it makes sense – Like… $CI = & get_instance();
• require_once is expensive • count() elements before start ‘for’ loop
![Page 24: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/24.jpg)
Applica.on (cont’d)
• Don’t copy variables if you don’t have to foreach($db_result as $row) { $content = $row[‘content’]; $content = word_limited($content, 100); }
And the usual: require_once, etc.
![Page 25: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/25.jpg)
Find one or two numbers in an array that add to 10. The gross way — n2 $set = array(1, 6, 7, 2, 5, 0, 8); foreach($set as $a) { foreach($set as $b) { if($a + $b == 10) exit(“Found $a and $b”); }
}
![Page 26: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/26.jpg)
Find one or two numbers in an array that add to 10. The real way — n $set = array(1, 6, 7, 2, 5, 0, 8); $flipped = array_flip($a); foreach($set as $el) { $needed = 10 -‐ $el; if(array_key_exists($needed, $flipped)) exit(“Found $el and $needed”);
}
![Page 27: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/27.jpg)
When You Get Big
• More machines! with load balancing • memcached • Master-‐slave db setup – Writes to master, reads to slave
![Page 28: Bottom to Top Stack Optimization with LAMP](https://reader033.fdocuments.us/reader033/viewer/2022052505/556150c6d8b42a8a7d8b4f67/html5/thumbnails/28.jpg)
Takeaways
• GetSpark’s APC-‐based Output override • These slides
codefury.net/cicon2011 Kenny Katzgrau @_kennyk_