(dv) 4.0 - Making It Better :: General Apache Tuning
- This page was last modified on April 12, 2012, at 14:18.
From (mt) Community Wiki
This is part of the (dv) 4.0 - Making It Better series. Please see the parent article for related articles and information on downloading the script that includes options to automate the instructions included in this article. This series of articles is not directly supported by (mt) Media Temple.
The (dv) Dedicated-Virtual 4.0 server is a fairly forgiving environment for Apache. The prefork settings included in httpd.conf follow a standard linear progression with regards to available RAM and will normally produce suitable results for users. However, servers with heavy resource usage may see improved performance by scaling these settings more conservatively. Additionally, the default Apache build includes a number of modules that are most likely not in use. Removing these from the configuration will free up RAM to be available elsewhere on the server.
The information/suggestions in this article are not steps that need to be done in order. Each set of commands will make changes that may (or may not!) improve performance in a different way. However, before proceeding, the cardinal rule must first be taken care of: make a backup!
cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf-backupMIB
The following change simply scales Apache settings more conservatively against the available RAM that is outlined by server beancounters. As with any change of this nature, there is no way to guarantee with certainty it will improve server performance.
This command can be used to lessen the amount of resources that Apache can "run away" with at any one time. For busier servers, this allows you to observe its memory levels change, or if other processes immediately start to make use of the memory which is now available. From there, additional troubleshooting would be necessary to determine if those other processes were problematic in their own right or not.
First, a few variable calculations are required based on current server beancounters. The following commands take the available information from /proc/user_beancounters and returns a number. If your (dv) has 512M RAM, this number is 1; for 1G RAM, it is 2; for 2G RAM, it is 3; so on and so forth:
ramCount=`awk 'match($0,/vmguar/) {print $4}' /proc/user_beancounters`
ramBase=-16 && for ((;ramCount>1;ramBase++)); do ramCount=$((ramCount/2)); done
With this information, adjustments can now be made to the prefork settings in httpd.conf. The following is a single command:
perl -0 -p -i -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?StartServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \ -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MinSpareServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \ -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxSpareServers\s*?)\s\d+/\1\ '"$(($ramBase*2 + 1))"'/;' \ -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?ServerLimit\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \ -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxClients\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \ -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxRequestsPerChild\s*?)\s\d+/\1\ '"$(( 2048 + ($ramBase*256) ))"'/;' /etc/httpd/conf/httpd.conf
Again, these changes are only suggested settings. Particular users may find better results comparing and contrasting the original backup file with the values that this provides and creating customized settings.
These first few commands disable three modules that are loaded into Apache by default with their own configuration files. For users with standard PHP-based applications, these should not be needed, and so they can be prevented from loading into Apache.
mv /etc/httpd/conf.d/perl.conf /etc/httpd/conf.d/perl.conf-disabled
mv /etc/httpd/conf.d/python.conf /etc/httpd/conf.d/python.conf-disabled
The following change removes a number of other modules from Apache. For most users, nothing in this list is needed. However, certain applications or server configurations may require that one or more of these modules be made available.
This command will lessen the resources Apache is claiming each time it needs to spawn a process. Each module loaded requires a slightly bulkier process, which means a higher idle RAM usage. For servers using a large number of Apache processes without any need for these modules, this will free up this RAM for use elsewhere.
The following command is a single command. If you see a module listed that you do not want disabled, simply remove that line from the command before executing it. If you need to re-enable a module afterward, you can manually edit /etc/httpd/conf/httpd.conf and remove the comment (# symbol) in any LoadModule directive that needs to take effect.
perl -0 -p -i -e 's/\#?(LoadModule\ authn_alias_module\ modules\/mod_authn_alias\.so)/#\1/;' \ -e 's/\#?(LoadModule\ authn_anon_module\ modules\/mod_authn_anon\.so)/#\1/;' \ -e 's/\#?(LoadModule\ authn_dbm_module\ modules\/mod_authn_dbm\.so)/#\1/;' \ -e 's/\#?(LoadModule\ authz_owner_module\ modules\/mod_authz_owner\.so)/#\1/;' \ -e 's/\#?(LoadModule\ authz_dbm_module\ modules\/mod_authz_dbm\.so)/#\1/;' \ -e 's/\#?(LoadModule\ ldap_module\ modules\/mod_ldap\.so)/#\1/;' \ -e 's/\#?(LoadModule\ authnz_ldap_module\ modules\/mod_authnz_ldap\.so)/#\1/;' \ -e 's/\#?(LoadModule\ ext_filter_module\ modules\/mod_ext_filter\.so)/#\1/;' \ -e 's/\#?(LoadModule\ usertrack_module\ modules\/mod_usertrack\.so)/#\1/;' \ -e 's/\#?(LoadModule\ dav_module\ modules\/mod_dav\.so)/#\1/;' \ -e 's/\#?(LoadModule\ dav_fs_module\ modules\/mod_dav\.so)/#\1/;' \ -e 's/\#?(LoadModule\ status_module\ modules\/mod_status\.so)/#\1/;' \ -e 's/\#?(LoadModule\ info_module\ modules\/mod_info\.so)/#\1/;' \ -e 's/\#?(LoadModule\ dav_fs_module\ modules\/mod_dav_fs\.so)/#\1/;' \ -e 's/\#?(LoadModule\ speling_module\ modules\/mod_speling\.so)/#\1/;' \ -e 's/\#?(LoadModule\ cache_module\ modules\/mod_cache.so)/#\1/;' \ -e 's/\#?(LoadModule\ disk_cache_module\ modules\/mod_disk_cache\.so)/#\1/;' \ -e 's/\#?(LoadModule\ file_cache_module\ modules\/mod_file_cache\.so)/#\1/;' \ -e 's/\#?(LoadModule\ mem_cache_module\ modules\/mod_mem_cache\.so)/#\1/;' \ -e 's/\#?(LoadModule\ version_module\ modules\/mod_version\.so)/#\1/;' /etc/httpd/conf/httpd.conf
Any time you make changes to Apache configurations, you need to restart the server in order for them to take effect:
/etc/init.d/httpd restart
Always keep in mind that automated changes and scripted suggestions can only go so far. Some level of familiarity with Apache documentation can go a long way when troubleshooting web server performance.