We all know and love Apache. It’s great, it allows us to run websites with minimal configuration and administration.
However, this same ease of flexibility and lack of tuning is typically what leads Apache to becoming a memory hog. Follow these easy tips and gain a significant performance boost from Apache.
1. 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.
2. Use mod_disk_cache NOT mod_mem_cache
mod_mem_cache will not share its cache among 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.
3. 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.
4. 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.
5. Put Cache on separate disk
Place your cache on a separate physical disk for fastest access without slowing down other processes.
6. 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.
7. Log to a different disk than disk serving pages
Put your logs on physically different disks than the files you are serving.
8. 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.
9. 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.
10. 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.
11. 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.
12. Do Not set KeepAliveTimeout too high
If you have more requests than apache children, this setting can starve your pool of available clients.
13. Disable .htaccess
AllowOverride None This will prevent apache from having to check for a .htaccess file on each request.
14. Allow symlinks
Options +FollowSymLinks -SymLinksIfOwnerMatch. Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.
15. 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.
16. Avoid Wildcards in DirectoryIndex
Use a specific DirectoryIndex, i.e. index.html or index.php, not index.
17. 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.
18. Increase Write Buffer Size
Icrease 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.
19. 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.
20. 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
21. Use mod_passenger for rails
mod_passenger is able to share memory and resources among several processes, allowing for faster spawning of new application instances. It will also monitor these processes and remove them when they are unnecessary.
22. 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.
23. Don’t use threaded mpm with mod_php
Look at using mod_itk, mod_php tends to segfault with threaded mpm.
24. 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.
25. 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. If you can’t get numbers or a graph to show that your performance has increased or load decreased, then you don’t know if your tuning has done you any good. A great way to ensure that you are performing better than before is to use these techniques to hook up apache monitoring to Monitis hosted monitoring.
Do you know more tips? Disagree with some of the tips? Please share your comments bellow.