Apache Performance Tuning Tips
Apache Specifics
- Remove unused modules – save memory by not loading modules that you do not need, including but not limited to mod_php, mod_ruby, mod_perl, etc.
- Use mod_disk_cache NOT mod_mem_cache – mod_mem_cache will not share its cache amongst different apache processes, which results in high memory usage with little performance gain since on an active server, mod_mem_cache will rarely serve the same page twice in the same apache process.
- Configure mod_disk_cache with a flat hierarchy – ensure that you are using CacheDirLength=2 and CacheDirLevels=1 to ensure htcacheclean will not take forever when cleaning up your cache directory.
- Setup appropriate Expires, Etag, and Cache-Control Headers – to utilize your cache, you must tell it when a file expires, otherwise your client will not experience the caching benefits.
- Put Cache on separate disk – place your cache on a separate physical disk for fastest access without slowing down other processes.
- Use Piped Logging instead of direct logging – directly logging to a file has issues when you want to rotate the log file. It must restart apache to use the next log file. This will cause significant slowness for your users during the restart. Particularly if you are using Passenger or some other app loader.
- Log to a different disk than disk serving pages – put your logs on physically different disks than the files you are serving.
- Utilize mod_gzip/mod_deflate – gzip your content before sending it off and then the client will ungzip upon receipt, this will minimize the size of file transfers, it generally will help all user experience.
- Turn HostnameLookups Off – stop doing expensive DNS lookups. You will rarely ever need them and when you do, you can look them up after the fact.
- Avoid using hostname in configs – if you have HostnameLookups off, this will prevent you from having to wait for the DNS resolve of the hostnames in your configs, use IP addresses instead.
- Use Persistent Connections – Set KeepAlive On and then set KeepAliveTimeout and KeepAliveRequests. KeepAliveTimeout is how long apache will wait for the next request, and KeepAliveRequests is the max number of requests for a client prior to resetting the connection. This will prevent the client from having to reconnect between each request.
- Do Not set KeepAliveTimeout too high – if you have more requests than apache children, this setting can starve your pool of available clients.
- Disable .htaccess – i.e. AllowOverride None This will prevent apache from having to check for a .htaccess file on each request.
- Allow symlinks – i.e. Options +FollowSymLinks -SymLinksIfOwnerMatch. Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.
- Set ExtendedStatus Off – Although very useful, the ExtendedStatus will produce several system calls for each request to gather statistics. Better to utilize for a set time period in order to benchmark, then turn back off.
- Avoid Wildcards in DirectoryIndex – use a specific DirectoryIndex, i.e. index.html or index.php, not index
OS Specifics
- Increase Swappiness – particularly on single site hosts this will increase performance. On linux systems increase /proc/sys/vm/swappiness to at least 60 if not greater. This will try to load as many files as possible into the memory cache for faster access.
- Increase Write Buffer Size – increase your write buffer size for tcp/ip buffers. On linux systems increase /proc/sys/net/core/wmem_max and /proc/sys/net/core/wmem_default. If your pages fit within this buffer, apache will complete a process in one call to the tcp/ip buffer.
- Increase Max Open Files – if you are handling high loads increase the number of allowed open files. On linux, increase /proc/sys/fs/file-max and run ulimit -H -n 4096.
Application Specifics
- Setup Frontend proxy for images and stylesheets – allow your main web servers to process the application while images and stylesheets are served from frontend webservers
- Use mod_passenger for rails – mod_passenger is able to share memory and resources amongst several processes, allowing for faster spawning of new application instances. It will also monitor these processes and remove them when they are unnecessary.
- Turn off safe_mode for php – it will utilize about 50-70% of your script time checking against these safe directives. Instead configure open_base_dir properly and utilize plugins such as mod_itk.
- Don’t use threaded mpm with mod_php – look at using mod_itk, mod_php tends to segfault with threaded mpm.
- Flush buffers early for pre-render – it takes a relatively long time to create a web page on the backend, flush your buffer prior to page completion to send a partial page to the client, so it can start rendering. A good place to do this is right after the HEAD section – so that the browser can start fetching other objects.
- Use a Cache for frequently accessed data – memcached is a great for frequently used data and sessions. It will speed up your apache render time as databases are slow.
And one more tip. Your Apache performance is only as good as your benchmarks measuring it.