Tips for optimizing LAMP stack & Linux server

performance_banner
1. Get a dedicated server
Advantages– Whole server power for you
– You are very flexible in terms of new versions for webserver, database, php …
– There are not other customers which run “bad” scripts which slow down the shop
– You don’t share the same ip, very important for SEO.
If one customer makes a “bad business”, the ip can be blocked by google which affects your search ranking.
My recommendation:I am a big fan of the Amazon EC2 cloud which gives you everything you need (flexibility, scalability, security).

2. Split database and webserver (if possible)

A webserver and a database server have different requirements. A database server needs fast hard disks (e.g. SSD) and much memory and not that much CPU. A webserver needs more CPU and less memory.

3. Use a separate Backend Server (Multi Server Environment)

Run all cronjobs on this server. You can also handle all admin users there.

4. Use newest MySQL Version
5. MySQL Configuration
Proper MySQL configuration is one of the most important aspects in terms of performance. Optimizing the MySQL configuration can provide up to 65% performance improvement. MySQL by default is configured to use far fewer resources than the average hardware can accommodate. InnoDB, the primary table storage engine type can use the in-memory buffer pool to cache table indexes and data. Less disk I/O is needed to get data from hard drives when the value of the in-memory buffer pool is set higher. A general recommendation is to set this parameter up to 80% of the available RAM for a dedicated database server. If you are running webserver and database server on one server it’s recommended to split the entire memory pool into two parts.
Setting for the key parameter “innodb_buffer_pool_size”
Server Typeinnodb_buffer_pool_sizeCombined webserver and database server, 6 GB RAM 2-3 GBDedicated database server, 6 GB RAM5 GBDedicated database server, 12 GB RAM10 GBDedicated database server, 24 GB RAM18 GBFurther important MySQL configuration settings

innodb_thread_concurrency = 2 * [numberofCPUs] + 2
innodb_flush_log_at_trx_commit = 2
thread_concurrency = [number of CPUs] * 3
thread_cache_size = 32
table_cache = 1024
query_cache_size = 64M
query_cache_limit = 2M
join_buffer_size = 8M
tmp_table_size = 256M
key_buffer = 32M
innodb_autoextend_increment=512
max_allowed_packet = 16M
max_heap_table_size = 256M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1

6. Checkout MySQLTuner : MySQLTuner is a Perl script that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief format along with some basic performance suggestions.

7. Mount disk with noatime option
Linux has a special mount option for file systems called noatime. If this option is set for a file system in /etc/fstab, then reading accesses will no longer cause the atime information (last access time – don’t mix this up with the last modified time – if a file is changed, the modification date will still be set) that is associated with a file to be updated (in reverse this means that if noatime is not set, each read access will also result in a write operation). Therefore, using noatime can lead to significant performance gains.

8. Memory-based filesystem for dynamic dataBy storing dynamic data (var/cache, var/session) on a memory-based filesystem like RAMdisk or tmpfs, the disk I/O is decreased.

9. Host the shop in same country where your customers are

The nearest location to your customers means the fastest response.

10. Install the newest PHP Version
PHP 5.2.x is very old and slow. PHP 5.3.x is about 30-40% faster than PHP 5.2.x and has a lower memory footprint. PHP 5.4.x is about 20% than PHP 5.3.x.
Some key parameter for your php.ini

realpath_cache_size = 32k
realpath_cache_ttl = 7200
max_execution_time = 90
max_input_time = 90
memory_limit = 256M
default_socket_timeout = 90
pdo_mysql.cache_size = 2000
output_buffering = 4096

11. Install a byte code cache like APC
Many frameworks and platforms save it’s cache data in file system. This is fine for small sites with low traffic, but as you get more and more requests, reading and writing to the file system become slower from time to time. With APC you will not have such problems because all valus are stored in memory.
Example php.ini configuration

apc.enabled = 1
apc.optimization  = 0
apc.shm_segments = 1
apc.shm_size = 768M
apc.ttl = 48000
apc.user_ttl  = 48000
apc.num_files_hint = 8096
apc.user_entries_hint = 8096
apc.mmap_file_mask = /tmp/apc.XXXXXX
apc.enable_cli = 1
apc.cache_by_default  = 1
apc.max_file_size = 10M
apc.include_once_override = 0

12. Swap Apache for NGINX
NGINX is a high performance, high concurrency edge web server with the key features to build modern, efficient, accelerated web infrastructure.

13. Enable Gzip Compression in NGINX configuration or Apache .htaccess
Test your Site: http://www.gidnetwork.com/tools/gzip-test.php

14. Set expiration header for static files

Configuration for nginx vhost:location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
expires max;
access_log off;
}
Apache:Make sure the module “mod_expire” is enabled

15. Apache modules – use just necessary modules
16. Apache: Eliminating directory structure scans for the .htaccess
17. Enable KeepAlive
KeepAlive provides long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images.
NGINX: http://wiki.nginx.org/HttpCoreModule#keepalive_timeout (Default is 75 seconds, which is good)
Apache: http://httpd.apache.org/docs/current/misc/perf-tuning.html

18. Use a Content Delivery Network (CDN) for delivering static files like js, css and images to offload your server
Afterwards your server has only php request to handle this means all requests for js, css and images are handled by the CDN. You may save a lot of hosting costs.
My personal experienceI made very good experience with Amazon Cloufront because it’s very easy to integrate and not expensive and fully integrated in the Amazon Webservice.

19. Uninstall xdebug or zend debugger on production
It’s a perfect tool for a development or testing environment but not for production. In some of our projects xdebug had a performance impact of 10-15%, especially with high traffic.

Leave a Comment