In this recipe, we will learn some performance tuning configurations that may help to squeeze out the last bit of performance from the available hardware. Before diving into performance tuning, we need to evaluate our servers and set a benchmark which can be used to measure improvements after any changes. We will be using a well known HTTP benchmarking tool, Apache Bench (ab). Various other benchmarking tools are available and each one has its own feature set. You can choose the one that best suits your needs.
Getting ready
You will need two systems: one with the web server software installed and another to run Apache Bench. You will need root access or access to an account with similar privileges.
You will also need to modify a few network parameters to handle a large network load. You will also need to set a higher open files limit, in limits.conf, on both systems. Check the Tuning TCP Stack recipe in article 2, Networking.
How to do it…
Install the Apache Bench tool. This is available with the package apache2-utils:
$ sudo apt-get install apache2-utils
If you need to, you can check all the available options of the ab tool as follows:
$ ab -h
Now we are ready to generate network load. Execute the following command to start ab:
$ ab -n 10000 -c 200 -t 2 -k "http://192.168.56.103/index.php"
It will take some time to complete the command depending on the parameters. You should see similar results to the following (partial) output:
Additionally, you may want to benchmark your server for CPU, memory, and IO performance. Check the Setting performance benchmarks recipe in article 13, Performance Monitoring.
Now that we have a benchmark for server performance with stock installation, we can proceed with performance optimization. The following are some settings that are generally recommended for performance tuning:
Apache related settings:
Remove/disable any unused modules
Enable mod_gzip/mod_deflate
Turn HostnameLookups off
Use IP address in configuration files
Use persistence connection by enabling keepalive, then set keepalive timeout
Limit the uses of AllowOverride or completely disable it with AllowOverride none
Disable ExtendedStatus; this is useful while testing but not in production
Nginx related settings:
Set worker_processes to the count of your CPU cores or simply set it to auto
Set the number of worker_connections to test multiple values to find the best match for your servers
Set the keepalive_requests and keepalive_timeout values; these reduce the overhead of creating new connections
Enable idle connections with upstream servers by setting the keepalive value
Enable log buffering with buffer and flush parameters to access_log; this will reduce IO requests while logging
Reduce the log-level - you can set it to warn the user or display an error while in production
Set the sendfile directive to use an efficient sendfile() call from the operating system
Enable caching and compression
Make sure that you track the performance changes after each set of modifications; this way you will have exact knowledge regarding what worked and what not
You should also tune the TCP stack. The details of the TCP stack settings are covered in article 2, Networking.
There's more…
Various other tools are available for benchmarking different features of the web server. The following are some well known tools, as well as a few latest additions:
Httperf: A web server benchmarking tool with some advanced options
Perfkit: a cloud benchmark tool by Google
Wrk: https://github.com/wg/wrk
H2load: HTTP2 load testing tool at https://nghttp2.org/documentation/h2load-howto.html
See also
Apache performance tuning guide at https://httpd.apache.org/docs/2.4/misc/perf-tuning.html
Nginx performance tuning guide at https://www.nginx.com/blog/tuning-nginx/