Lamp Stack Optimization


Published on

Mike Creuzer's presentation on LAMP optimization for the October, 2009 meeting of the Suburban Chicago PHP & Web Development Meetup

Published in: Technology
1 Comment
  • Quick question on this - is there a way or does anyone have any experience with integration with Salesforce? Company using Lamp Stack as part of content management system and would like it to talk to Salesforce. Thanks
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Optimization definition:
  • Source
  • ext3 can be optomized, but the latest versions of linux have many of the optimizations in place. old ext3 instructions & file access time turn off instructions. Use buffered logging when ever apropriate.
  • file access time turn off instructions.
  • Easy optimizations are things you can do without modifying the CMS. We pay the premium for RAID disk drives and extra RAM. 
  • Unix apache uses prefork MPM 
  • These are discussed in detail at the end of
  • Lamp Stack Optimization

    1. 1. LAMP Stack Optimization for websites using a CMS
    2. 2. What's the LAMP Stack? <ul><li>LAMP stands for: </li></ul><ul><ul><li>Linux </li></ul></ul><ul><ul><li>Apache </li></ul></ul><ul><ul><li>MySQL </li></ul></ul><ul><ul><li>PHP </li></ul></ul><ul><li>These 4 elements are makes up the bulk of website environments today. </li></ul>
    3. 3. What is Optimization? <ul><li>Improving a system to reduce runtime, bandwidth, memory requirements, or other properties of a system. </li></ul><ul><li>In other words... </li></ul><ul><li>To make it faster! </li></ul>
    4. 4. Metrics <ul><li>The key to optimization is getting a handle on what's going on so you know if what you are doing is making things better or making them worse. </li></ul><ul><li>Metrics are key. </li></ul>
    5. 5. Linux Optimization Overview <ul><li>The other three pieces relay on Linux in order to share resources, so you would think that optimizing Linux would be the best place to start. </li></ul><ul><li>However, Linux is 'pretty darn good' (TM) out of the box. </li></ul><ul><li>By selecting the 'server' edition for your hardware platform, you can't go wrong. </li></ul><ul><li>There are many 'tweaks' you can make to speed the system up and get incremental performance boosts. </li></ul><ul><li>There are many 'tweaks' you can make to horribly destroy any semblence of performance. </li></ul><ul><li>Often they are the same tweaks.... </li></ul>
    6. 6. Linux Optmization Basics <ul><li>Linux just controls application's access to the CPU, RAM, Disk Drives, and network. By giving Linux faster hardware, you will get more performance out of it. </li></ul><ul><li>In my experience, webservers and CMS websites tend to be I/O bound. So by providing faster disks and more RAM so it needs to access the disk less, you will improve performance. </li></ul><ul><li>Virtualization is freaking awesome! But it can come at a performance penalty as one virtual server has no control over what the other virtual servers are doing on the hardware. </li></ul><ul><li>Many linux level optimizations are irrelevant in virtual servers as the 'real' OS could have totally different optimizations. </li></ul>
    7. 7. Linux and the Disk Drives <ul><li>Disk seeks. It takes time for the disk to find a piece of data. With modern disks, the mean time for this is usually lower than 10ms, so we can in theory do about 100 seeks a second. This time improves slowly with new disks and is very hard to optimize for a single table. The way to optimize seek time is to distribute the data onto more than one disk. </li></ul><ul><li>Disk reading and writing. When the disk is at the correct position, we need to read the data. With modern disks, one disk delivers at least 10–20MB/s throughput. This is easier to optimize than seeks because you can read in parallel from multiple disks. </li></ul>
    8. 8. Linux and ways it slows Disk I/O <ul><li>Linux ext3 is a journeled file system. That is, it keeps a 'journal' of all the writes it's about to do, so if the system crashes, it can check just those files, and not the entire disk. There is an overhead to doing these extra disk writes. </li></ul><ul><li>Linux by default maintains File Access Stamps. Yep, every time somebody loads a page, Linux marks the last access date and time.  </li></ul><ul><li>RAID is good, software RAID is slow! Software raid means that every write to disk is done to each disk separately, in sequence. This can slow the system down dramatically. </li></ul><ul><li>System & server logging can be very detrimental to I/O speeds. </li></ul>
    9. 9. Ways to speed Disk I/O up <ul><li>Use the latest stable version of Linux available . The latest kernel builds have many ext3 optimizations turned on by default </li></ul><ul><li>Turn off Disk Access Times. For the most part this isn't used on production web servers. </li></ul><ul><li>Hardware RAID using fast drives.  By talking to more then 1 drive at a time the RAID controller card can mitigate the speed differences between the system bus speeds and disk drive cable speeds. </li></ul><ul><li>Use buffered logging & increase the size of the buffers. This allows many log updates to occur in a single write. The risk is loss of the last log entries on system crash. </li></ul>
    10. 10. Linux and RAM <ul><li>I have yet to meet a Linux server that will not use almost all available RAM given enough time. </li></ul><ul><li>Linux Loves RAM and will put it to good use in buffers and caching. Also, A Linux server should never have to swap. </li></ul><ul><li>Let it have lots of RAM. Yes, it will boot into a small memory footprint, but after 30-90 days it will use much more. </li></ul><ul><li>Your other server software Love RAM too, so get more RAM. </li></ul><ul><li>Really. More is always better! </li></ul><ul><li>Really Really! </li></ul>
    11. 11. Intermediate Linux Optimizations <ul><li>There are many other Linux optimization techniques. These too advanced to discuss here, as they can have negative performance impacts as well. But here some are so you know what to research. </li></ul><ul><ul><li>Custom compile the kernel & applications for your specific CPU with all relevant code optimizations turned on. </li></ul></ul><ul><ul><li>Adjusting open inode limits (number of open files at a time) </li></ul></ul><ul><ul><li>Adjusting per-process file descriptor limits </li></ul></ul><ul><ul><li>Local Port Range - Limits number of simultaneous network connections. </li></ul></ul>
    12. 12. Linux Metrics Tools <ul><li>top - Shows current cpu usage, memory usage, running processes and 'load average'. A great tool to get a 'feel' for how the server is running. </li></ul><ul><li>Load average is kinda a fuzzy metric to what it REALLY means, but the best answer I found is that it's the total resource Que time. The time it would take for all queued process/threads to finish executing their current tasks. </li></ul><ul><li>iptraf - Networking monitoring tool showing current traffic rates. </li></ul><ul><li>netstat & iostat - More tools to monitor network and disk activity. </li></ul>
    13. 13. Easy Linux Optimizations for a CMS <ul><ul><li>Prefer 'real' dedicated hosting over virtual hosting over shared hosting. The less you have to worry about &quot;other peoples' stuff&quot;, the better.  </li></ul></ul><ul><ul><li>Use a server with specifications that are 'good enough'. More RAM is a much better investment than a 0.2ghz faster CPU (1.6->1.8, 2.6->2.8) that many plans offer for their 'better' hosting packages. </li></ul></ul><ul><ul><li>Use a current version of the operating system. Many speed performance increases are added to newer versions of the OS. </li></ul></ul>
    14. 14. Apache Optimization Overview <ul><li>Apache 2.x has many pre-enabled speed optimizations over the 1.x series. So running the latest version will often give you better performance. </li></ul><ul><li>Apache running in Linux has a 'parent' or 'root' process that creates 'child' processes that do the actual work. Each of these child processes has it's own memory allotment, creates connections to resources, etc. </li></ul><ul><li>Each child process can handle 1 'request' at a time. </li></ul><ul><li>Each child handles a finite number of requests before it's 'recycled' - ended and started new. </li></ul>
    15. 15. Apache Optimization Basics <ul><li>The trick to optimizing apache is understanding how many simultaneous connections the server is handling and have enough child processes to handle them all. Yet, you don't want TOO many child processes or you can use up all the available memory & other resources. </li></ul><ul><li>PHP memory allotments are assigned inside each apache child process. So, if you set PHP to use 128mb of RAM, you can potentially have dozens/hundreds of apache threads that can grow to over 128MBs of RAM usage each! </li></ul>
    16. 16. Potential Apache Bottlenecks <ul><li>AllowOveride - lets you use .htaccess to change stuff. Apache will look for a .htaccess file in EACH directory working it's way form the root directory to the current directory.  </li></ul><ul><li>Logging - will always have a performance impact. However, the penalties are often outweighed by the insights gained by the logging. </li></ul><ul><li>Apache modules - any extra code that's loaded that's not needed just uses RAM and compute cycles. Also, modules such as PHP can really chew up performance. </li></ul><ul><li>Each of these are likely useful enough to warrant their use. </li></ul>
    17. 17. Apache Performance Tips <ul><li>Enable client side caching. This could be the single biggest apache-level performance tip out there. PHP pages CAN and often SHOULD issue cacheing instructions in the headers. </li></ul><ul><li>Adding expires headers using mod_expires for static content like css, javascript files and images can greatly reduce server load. </li></ul><ul><li>Configuring the ETag allows quick-lookups of last modified dates allowing very fast 304 responses back to the client. An ETag on apache is a hash of the file info and modification date of static files. It can reduce server traffic even without explicit cache controls. </li></ul>
    18. 18. Apache Metrics <ul><li>mod_status - Configure this correctly to have a secure look at what the server is currently doing including simultaneous connections. </li></ul><ul><li>top - This linux tool will show you what apache is using for RAM and cpu usage. </li></ul><ul><li>access & error logs. Allow insight into how apache is being used by the users. Tools like AWstats and Webalizer can make analysis on the aggregate easier. </li></ul>
    19. 19. Easy Apache optimizations for a CMS <ul><li>Enable caching in the CMS application everywhere that you can. </li></ul><ul><li>Configure the cache expiration for files that are outside of the CMS, such as images, css files & javascript files. </li></ul><ul><li>Configure ETags to allow faster file changed lookups for files that are outside of the CMS, such as images, css files & javascript files. </li></ul>
    20. 20. MySQL Optimization Overview <ul><li>The database is the heart and soul of a CMS web application. </li></ul><ul><li>On some CMSes out there, you can have dozens of DB requests per page load, so optimizing the database can be the single most effective optimization you can do. </li></ul><ul><li>You may be able to configure your CMS so it won't access the DB at all in many circumstances by turning on cacheing. </li></ul><ul><ul><li>Page Caching - save the whole page for use again later </li></ul></ul><ul><ul><li>Module Caching - save individual modules and blocks for reuse on subsequent page loads </li></ul></ul><ul><ul><li>Data Caching - save the data from a query for reuse </li></ul></ul><ul><li>Optimizing MySQL Query Caching is the next best step. </li></ul>
    21. 21. MySQL Basic Optimization Tips <ul><li>MySQL can use memory to cache various aspects of the data. </li></ul><ul><li>Query Cache </li></ul><ul><li>The results of a query are saved for later. So if you do that same query again, the results are right there at the server's 'finger tips' </li></ul><ul><li>Key Cache </li></ul><ul><li>This is the cache for the indexes. This is probably even more important the the Query Cache as it can be used for many queries. </li></ul>
    22. 22. MySQL Memory allocation <ul><li>Total RAM installed </li></ul><ul><li>- Normal OS memory requirements w/ system caches & buffers </li></ul><ul><li>- Max # Apache instances * (apache + max PHP) </li></ul><ul><li>- Other website stuff </li></ul><ul><li>Memcached? </li></ul><ul><li>APC? </li></ul><ul><li>Persistant Connections? </li></ul><ul><li>- Other services memory requirements </li></ul><ul><li>email & spam filtering </li></ul><ul><li>ftp </li></ul><ul><li>dns </li></ul><ul><li>- Transient Services </li></ul><ul><li>Backups? </li></ul><ul><li>= Available memory to give to MySQL </li></ul>
    23. 23. MySQL Metrics <ul><li>Slow Query log - Buy turning on this log, you can see what queries are taking a long time to perform. You can also set this to log queries that aren't using any index to run. </li></ul><ul><li>MySQL status command -  See the current query rate and other useful statistics. </li></ul><ul><li>Many 3rd party tools like PHPmyAdmin, SQLYog & MonYog have built in statistics displays/analysis. </li></ul><ul><li>MonYog can give valid optimization suggestions. </li></ul>
    24. 24. MySQL Example CMS Configuration  <ul><li>Seems to work for ~20 Joomla websites and a vBullitin forum on a 2GB RAM dedicated host. </li></ul><ul><li>[mysqld] </li></ul><ul><li>log-long-format </li></ul><ul><li>log-slow-queries = /var/log/mysql/mysql-slow.log </li></ul><ul><li>long_query_time = 5 </li></ul><ul><li>log_queries_not_using_indexes </li></ul><ul><li>set-variable    = open_files_limit=6144 </li></ul><ul><li>set-variable    = key_buffer=384M </li></ul><ul><li>set-variable    = max_allowed_packet=16M </li></ul><ul><li>set-variable    = table_cache=3072 </li></ul><ul><li>set-variable    = sort_buffer=8M </li></ul><ul><li>set-variable    = read_buffer_size=2M </li></ul><ul><li>set-variable    = read_rnd_buffer_size=8M </li></ul><ul><li>set-variable    = record_buffer=1M </li></ul><ul><li>set-variable    = myisam_sort_buffer_size=64M </li></ul><ul><li>set-variable    = thread_cache=16 </li></ul><ul><li>set-variable    = query_cache_size=256M </li></ul><ul><li>set-variable    = query_cache_limit=2M </li></ul><ul><li>set-variable    = tmp_table_size=64M </li></ul><ul><li>set-variable    = join_buffer_size=1M </li></ul><ul><li>set-variable    = query_prealloc_size=16k </li></ul>
    25. 25. PHP Optimization Overview <ul><li>PHP is a scripting language - meaning it's compiled 'just in time'. It's compiled every time the script is run. </li></ul><ul><li>This is great when you make lots of frequent small changes. Not so good when you have a great many lines of code that get run over and over again. </li></ul><ul><li>PHP is run inside of apache, so each apache thread will contain all the memory allocated to php. Many CMSs will suggest a 32mb memory limit because dynamic image resizing takes that much RAM. Each apache instance can potentially increase it's memory footprint by that much, so watch apache instances and PHP RAM max allocations. </li></ul>
    26. 26. PHP Optimization Basics <ul><li>The more code that is loaded, the slower the code will run. </li></ul><ul><li>Limit the amount of memory used by PHP and scripts. </li></ul><ul><li>Utilize the available op-code cacheing. </li></ul>
    27. 27. PHP opcode Cacheing <ul><li>Opcode Cacheing is a way to dynamically save the compiled machine code for reuse the next time the code is ran without needed to recompile every time. </li></ul><ul><li>Available tools </li></ul><ul><ul><ul><li>Zend Platform </li></ul></ul></ul><ul><ul><ul><li>APC  ( APC GUI ) </li></ul></ul></ul><ul><ul><ul><li>XCache </li></ul></ul></ul><ul><ul><ul><li>eAccelerator </li></ul></ul></ul><ul><ul><ul><li>ionCube Encoder </li></ul></ul></ul><ul><ul><ul><li>PHP Accelerator </li></ul></ul></ul>
    28. 28. PHP optimizations for a CMS <ul><li>Use the suggested RAM requirements by the CMS. </li></ul><ul><li>Install and use an opcode caching tool. </li></ul>