Managing a website is all fun and games, but before you know you have over thousands of visitors a day, which is why you need to make sure your website keeps up with everything. You need to optimize apache for high traffic and especially for a wordpress installation. There’s a tons of ways you can achieve this to get great results and your users can content with your website delivering the content instantly.
Now, if you haven’t already, read the official Apache Tuning Documentation. You can probably get good results just following that.
Working with High Traffic Servers
Before we start to optimize your apache for high traffic, first of all, before you dive into the guide and start copy pasting configurations to “optimize” your server, you should first be assessing the current situation for server. If you’re running a server with 2000-3000 requests/second it’s time to start thinking about dual servers and load balancing. Depending on what you’re serving you can easily get more out of any of those servers, but at those rates you’re serving something important (or at least high-traffic), so you want redundancy in addition to the ability to handle momentary load spikes.
You should seriously start considering a load balancing infrastructure.
If you are using Nginx, you are better off using our similar guide for optimizing Nginx.
Apache is an open-source and free web server software that powers around 46% of websites around the world. The official name is Apache HTTP Server, and it’s maintained and developed by the Apache Software Foundation.
Your Web Server is actually what is responsible for delivering your website’s content to your visitors. All the requests coming to your servers are handled by that piece of software and it is always great to have it working at full capacity.
Optimize Apache for High Traffic
The Apache Status module,
mod_status, provides performance information about your server in a detailed status page.
- Open your website’s configuration file. This file is located at
/etc/apache2/sites-available/hostname.example.com.confon Debian/Ubuntu systems or
/etc/httpd/conf.d/vhost.confon CentOS/Fedora systems.
- Add the following to the
<Location /server-status> SetHandler server-status Order Deny,Allow Deny from all Allow from localhost </Location>
mod_statusalso offers an option called ExtendedStatus, which provides additional information about each request made to Apache. To enable ExtendedStatus edit your Apache configuration file and add the following line:
ExtendedStatusconsumes additional system resources.
- Restart Apache:
sudo systemctl restart apache2
Now, let us look at a few parameters that you can tweak for the performance.
MaxRequestsPerChildis really only useful for containing resource leaks. 0 (Never kill a child) is certainly viable if all you’re serving are static resources.
10000 (ten thousand, the default) is fine in almost all circumstances. 50000 (fifty thousand) is what I use for pure static HTML sites.
MaxSpareServerscan be tuned. I generally set
MinSpareServersto the same value.
If there is a specific minimum number of spare servers you want to keep around, that is the number you should start with. A good value for this is your low-water-mark of simultaneous active connections.
MaxSpareServersshould be set to 75-80% of your high-water-mark of simultaneous active connections.
MaxClientscan possibly be increased. If you have lots of free RAM and lots of free CPU, increase these numbers.
If you’re running close to resource saturation, leave them as-is.
- Use graceful restarts
You say you are seeing “momentary extreme peaks” in your load when Apache restarts.
This tells me you’re probably not using graceful restarts.
Whatever is causing Apache to restart, have it send
SIGUSR1to Apache rather than
SIGHUP(or heaven forbid, actually stopping and starting the entire server). This is far less abusive and disruptive to the system than a regular restart of a full stop/start.
- Consider other MPMs – You are almost certainly using the
preforkMPM if you’re on a Unix system. Consider the Worker MPM instead. Tuning for the Worker MPM is a little different.
- The event module is only available on Apache 2.4 and is based off of the worker MPM. Like the worker, it creates multiple threads per child process, with a thread dedicated to KeepAlive connections that are handed down to child threads once the request has been made. This is good for multiple concurrent connections, especially those that are not all active at the same time but make the occasional request. The event MPM functions the same as worker in the event of SSL connections.
- Spend some cache – Apache has caching modules which can be used to hold frequently accessed data in RAM. This avoids a round-trip to the disk (or at least the filesystem layer) for frequently accessed data.
Configuring memory backed caching can give you a pretty big performance boost for a relatively small amount of memory.
- Module Values – Once you select your MPM, you will need to change the values inside the configuration. These settings are located in the
/etc/apache2/apache2.conffile on Debian/Ubuntu, and the
/etc/httpd/conf/httpd.conffile on CentOS/Fedora. Below, is an example configuration for the MPM prefork module:
<IfModule mpm_prefork_module> StartServers 4 MinSpareServers 20 MaxSpareServers 40 MaxRequestWorkers 200 MaxConnectionsPerChild 4500 </IfModule>
To use the worker or event modules, replace
<IfModule mpm_prefork_module> with
<IfModule mpm_worker_module> or
<IfModule mpm_event_module>, respectively. Next, you should alter the module settings you added in the previous step. To do this, you should take into consideration what each value does, and how best to change it. It is recommended to make incremental changes to your configuration settings and then monitor the effects.
Configuring Apache for maximum performance is tricky, there are no hard and fast rules or ‘one-size fits all’ values. Understanding the web server requirements is vital before experimenting with the settings.