{"id":1072,"date":"2013-03-15T10:55:04","date_gmt":"2013-03-15T10:55:04","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=537"},"modified":"2013-03-15T10:55:04","modified_gmt":"2013-03-15T10:55:04","slug":"tips-for-optimizing-lamp-stack-linux-server","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/tips-for-optimizing-lamp-stack-linux-server\/","title":{"rendered":"Tips for optimizing LAMP stack & Linux server"},"content":{"rendered":"

\"performance_banner\"
\n1. Get a dedicated server<\/strong>
\nAdvantages\u2013 Whole server power for you
\n\u2013 You are very flexible in terms of new versions for webserver, database, php \u2026
\n\u2013 There are not other customers which run \u201cbad\u201d scripts which slow down the shop
\n\u2013 You don\u2019t share the same ip, very important for SEO.
\nIf one customer makes a \u201cbad business\u201d, the ip can be blocked by google which affects your search ranking.
\nMy recommendation:I am a big fan of the Amazon EC2 cloud which gives you everything you need (flexibility, scalability, security).
\n<\/p>\n

2. Split database and webserver (if possible)<\/strong><\/p>\n

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.<\/p>\n

3. Use a separate Backend Server (Multi Server Environment)<\/strong><\/p>\n

Run all cronjobs on this server. You can also handle all admin users there.<\/p>\n

4. Use newest MySQL Version<\/strong>
\n5. MySQL Configuration<\/strong>
\nProper 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\u2019s recommended to split the entire memory pool into two parts.
\nSetting for the key parameter \u201cinnodb_buffer_pool_size\u201d
\nServer 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<\/p>\n

\ninnodb_thread_concurrency = 2 * [numberofCPUs] + 2\ninnodb_flush_log_at_trx_commit = 2\nthread_concurrency = [number of CPUs] * 3\nthread_cache_size = 32\ntable_cache = 1024\nquery_cache_size = 64M\nquery_cache_limit = 2M\njoin_buffer_size = 8M\ntmp_table_size = 256M\nkey_buffer = 32M\ninnodb_autoextend_increment=512\nmax_allowed_packet = 16M\nmax_heap_table_size = 256M\nread_buffer_size = 2M\nread_rnd_buffer_size = 16M\nbulk_insert_buffer_size = 64M\nmyisam_sort_buffer_size = 128M\nmyisam_max_sort_file_size = 10G\nmyisam_max_extra_sort_file_size = 10G\nmyisam_repair_threads = 1\n\n<\/pre>\n

6. Checkout MySQLTuner <\/strong>: 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.<\/p>\n

7. Mount disk with noatime option<\/strong>
\nLinux 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 \u2013 don\u2019t mix this up with the last modified time \u2013 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.<\/p>\n

8. Memory-based filesystem<\/strong> 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.<\/p>\n

9. Host the shop in same country where your customers are<\/strong><\/p>\n

The nearest location to your customers means the fastest response.<\/p>\n

10. Install the newest PHP Version<\/strong>
\nPHP 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.
\nSome key parameter for your php.ini<\/p>\n

\nrealpath_cache_size = 32k\nrealpath_cache_ttl = 7200\nmax_execution_time = 90\nmax_input_time = 90\nmemory_limit = 256M\ndefault_socket_timeout = 90\npdo_mysql.cache_size = 2000\noutput_buffering = 4096\n\n<\/pre>\n

11. Install a byte code cache like APC<\/strong>
\nMany frameworks and platforms save it\u2019s 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.
\nExample php.ini configuration<\/p>\n

\napc.enabled = 1\napc.optimization\u00a0 = 0\napc.shm_segments = 1\napc.shm_size = 768M\napc.ttl = 48000\napc.user_ttl\u00a0 = 48000\napc.num_files_hint = 8096\napc.user_entries_hint = 8096\napc.mmap_file_mask = \/tmp\/apc.XXXXXX\napc.enable_cli = 1\napc.cache_by_default\u00a0 = 1\napc.max_file_size = 10M\napc.include_once_override = 0\n\n<\/pre>\n

12. Swap Apache for NGINX<\/strong>
\nNGINX is a high performance, high concurrency edge web server with the key features to build modern, efficient, accelerated web infrastructure.<\/p>\n

13. Enable Gzip Compression<\/strong> in NGINX configuration or Apache .htaccess
\nTest your Site: http:\/\/www.gidnetwork.com\/tools\/gzip-test.php<\/p>\n

14. Set expiration header for static files<\/strong><\/p>\n

\nConfiguration for nginx vhost:location ~* ^.+\\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {\nexpires max;\naccess_log off;\n}\nApache:Make sure the module \u201cmod_expire\u201d is enabled\n\n<\/pre>\n

15. Apache modules<\/strong> \u2013 use just necessary modules
\n16. Apache:<\/strong> Eliminating directory structure scans for the .htaccess
\n17. Enable KeepAlive<\/strong>
\nKeepAlive 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.
\nNGINX: http:\/\/wiki.nginx.org\/HttpCoreModule#keepalive_timeout (Default is 75 seconds, which is good)
\nApache: http:\/\/httpd.apache.org\/docs\/current\/misc\/perf-tuning.html<\/p>\n

18. Use a Content Delivery Network (CDN)<\/strong> for delivering static files like js, css and images to offload your server
\nAfterwards 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.
\nMy personal experienceI made very good experience with Amazon Cloufront because it\u2019s very easy to integrate and not expensive and fully integrated in the Amazon Webservice.<\/p>\n

19. Uninstall xdebug or zend debugger on production<\/strong>
\nIt\u2019s 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.<\/p>\n","protected":false},"excerpt":{"rendered":"

1. Get a dedicated server Advantages\u2013 Whole server power for you \u2013 You are very flexible in terms of new versions for webserver, database, php \u2026 \u2013 There are not other customers which run \u201cbad\u201d scripts which slow down the shop \u2013 You don\u2019t share the same ip, very important for SEO. If one customer…<\/p>\n

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57,67,46,53,44,90,91,60,66],"tags":[48,70,71,62,55,64],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1072"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=1072"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1072\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=1072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=1072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=1072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}