lamp stack optimization

28
LAMP Stack Optimization for websites using a CMS

Upload: dave-ross

Post on 13-Jan-2015

5.387 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Lamp Stack Optimization

LAMP Stack Optimization

for websites using a CMS

Page 2: Lamp Stack Optimization

What's the LAMP Stack?

LAMP stands for:

• Linux• Apache• MySQL• PHP

These 4 elements are makes up the bulk of website environments today.

Page 3: Lamp Stack Optimization

What is Optimization?

Improving a system to reduce runtime, bandwidth, memory requirements, or other properties of a system.

In other words...

To make it faster!

Page 4: Lamp Stack Optimization

Metrics

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.

Metrics are key.

Page 5: Lamp Stack Optimization

Linux Optimization Overview

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.

However, Linux is 'pretty darn good' (TM) out of the box.

By selecting the 'server' edition for your hardware platform, you can't go wrong.

There are many 'tweaks' you can make to speed the system up and get incremental performance boosts.There are many 'tweaks' you can make to horribly destroy any semblence of performance.

Often they are the same tweaks....

Page 6: Lamp Stack Optimization

Linux Optmization Basics

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.

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.

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.

Many linux level optimizations are irrelevant in virtual servers as the 'real' OS could have totally different optimizations.

Page 7: Lamp Stack Optimization

Linux and the Disk Drives

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.

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.

Page 8: Lamp Stack Optimization

Linux and ways it slows Disk I/O

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.

Linux by default maintains File Access Stamps. Yep, every time somebody loads a page, Linux marks the last access date and time. 

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.

System & server logging can be very detrimental to I/O speeds.

Page 9: Lamp Stack Optimization

Ways to speed Disk I/O up

Use the latest stable version of Linux available. The latest kernel builds have many ext3 optimizations turned on by default

Turn off Disk Access Times. For the most part this isn't used on production web servers.

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.

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.

Page 10: Lamp Stack Optimization

Linux and RAM

I have yet to meet a Linux server that will not use almost all available RAM given enough time.

Linux Loves RAM and will put it to good use in buffers and caching. Also, A Linux server should never have to swap.

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.

Your other server software Love RAM too, so get more RAM.

Really. More is always better!

Really Really!

Page 11: Lamp Stack Optimization

Intermediate Linux Optimizations

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.

• Custom compile the kernel & applications for your specific CPU with all relevant code optimizations turned on.

• Adjusting open inode limits (number of open files at a time)

• Adjusting per-process file descriptor limits

• Local Port Range - Limits number of simultaneous network connections.

Page 12: Lamp Stack Optimization

Linux Metrics Tools

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.

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.

iptraf - Networking monitoring tool showing current traffic rates.

netstat & iostat - More tools to monitor network and disk activity.

Page 13: Lamp Stack Optimization

Easy Linux Optimizations for a CMS

• Prefer 'real' dedicated hosting over virtual hosting over shared hosting. The less you have to worry about "other peoples' stuff", the better. 

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

• Use a current version of the operating system. Many speed performance increases are added to newer versions of the OS.

Page 14: Lamp Stack Optimization

Apache Optimization Overview

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.

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.

Each child process can handle 1 'request' at a time.

Each child handles a finite number of requests before it's 'recycled' - ended and started new.

Page 15: Lamp Stack Optimization

Apache Optimization Basics

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.

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!

Page 16: Lamp Stack Optimization

Potential Apache Bottlenecks

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. 

Logging - will always have a performance impact. However, the penalties are often outweighed by the insights gained by the logging.

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.

Each of these are likely useful enough to warrant their use.

Page 17: Lamp Stack Optimization

Apache Performance Tips

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.

Adding expires headers using mod_expires for static content like css, javascript files and images can greatly reduce server load.

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.

Page 18: Lamp Stack Optimization

Apache Metrics

mod_status - Configure this correctly to have a secure look at what the server is currently doing including simultaneous connections.

top - This linux tool will show you what apache is using for RAM and cpu usage.

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.

Page 19: Lamp Stack Optimization

Easy Apache optimizations for a CMS

Enable caching in the CMS application everywhere that you can.

Configure the cache expiration for files that are outside of the CMS, such as images, css files & javascript files.

Configure ETags to allow faster file changed lookups for files that are outside of the CMS, such as images, css files & javascript files.

Page 20: Lamp Stack Optimization

MySQL Optimization Overview

The database is the heart and soul of a CMS web application.

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.

You may be able to configure your CMS so it won't access the DB at all in many circumstances by turning on cacheing.• Page Caching - save the whole page for use again later• Module Caching - save individual modules and blocks for

reuse on subsequent page loads• Data Caching - save the data from a query for reuse

Optimizing MySQL Query Caching is the next best step.

Page 21: Lamp Stack Optimization

MySQL Basic Optimization Tips

MySQL can use memory to cache various aspects of the data.

Query CacheThe 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'Key CacheThis is the cache for the indexes. This is probably even more important the the Query Cache as it can be used for many queries.

Page 22: Lamp Stack Optimization

MySQL Memory allocation

Total RAM installed- Normal OS memory requirements w/ system caches & buffers- Max # Apache instances * (apache + max PHP)- Other website stuffMemcached?APC?Persistant Connections?- Other services memory requirementsemail & spam filteringftpdns- Transient ServicesBackups?= Available memory to give to MySQL

Page 23: Lamp Stack Optimization

MySQL Metrics

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.

MySQL status command -  See the current query rate and other useful statistics.

Many 3rd party tools like PHPmyAdmin, SQLYog & MonYog have built in statistics displays/analysis.

MonYog can give valid optimization suggestions.

Page 24: Lamp Stack Optimization

MySQL Example CMS Configuration 

Seems to work for ~20 Joomla websites and a vBullitin forum on a 2GB RAM dedicated host.[mysqld]log-long-formatlog-slow-queries = /var/log/mysql/mysql-slow.loglong_query_time = 5log_queries_not_using_indexesset-variable    = open_files_limit=6144set-variable    = key_buffer=384Mset-variable    = max_allowed_packet=16Mset-variable    = table_cache=3072set-variable    = sort_buffer=8Mset-variable    = read_buffer_size=2Mset-variable    = read_rnd_buffer_size=8Mset-variable    = record_buffer=1Mset-variable    = myisam_sort_buffer_size=64Mset-variable    = thread_cache=16set-variable    = query_cache_size=256Mset-variable    = query_cache_limit=2Mset-variable    = tmp_table_size=64Mset-variable    = join_buffer_size=1Mset-variable    = query_prealloc_size=16k

Page 25: Lamp Stack Optimization

PHP Optimization Overview

PHP is a scripting language - meaning it's compiled 'just in time'. It's compiled every time the script is run.

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.

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.

Page 26: Lamp Stack Optimization

PHP Optimization Basics

The more code that is loaded, the slower the code will run.

Limit the amount of memory used by PHP and scripts.

Utilize the available op-code cacheing.

Page 27: Lamp Stack Optimization

PHP opcode Cacheing

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.

Available toolso Zend Platformo APC (APC GUI)o XCacheo eAcceleratoro ionCube Encodero PHP Accelerator

Page 28: Lamp Stack Optimization

PHP optimizations for a CMS

Use the suggested RAM requirements by the CMS.

Install and use an opcode caching tool.