If you scale the the hardware of a machine, it does not mean that webserver will utilize the resources accordingly. Mostly web server have a default value in terms of how much resoruce they can utilize from the machine. as, In Apache default limit of connection is 150.
We always need to modify the configuration as per the availability of resources, this is the main challenge in vertical scaling.
To figure out the Root Cause of Load, first, you should check
if there is an error like “The process running on port 80 is not Apache.” then please check the output of the command “/usr/sbin/apache2 -V “. The first line of the output should be Server version: Apache/2.4.29
This kind of output will be given. This will also provide you with general improvement points that can be done on the server.
if you do not want to run this script on the server, you can use the following command to view the apache memory utilization
For the connection configuration, you must be knowing about the MPM module, accordingly, you can set the directives.
Prefork vs Worker vs Event
Prefork MPM launches multiple child processes. Each child process handle one connection at a time.Prefork is the default MPM used by Apache2 server. Prefork MPM always runs few minimum (MinSpareServers) defined processes as spare, so new requests do not need to wait for new process to start.
Worker MPM generates multiple child processes similar to prefork. Each child process runs many threads. Each thread handles one connection at a time.Worker MPM uses low memory in comparison to Prefork MPM.
Event MPM, Introduced in apache2.4. This MPM allows more requests to be served simultaneously by passing off some processing work to supporting threads. Using this MPM Apache tries to fix the ‘keep alive problem’ faced by other MPM. When a client completes the first request then the client can keep the connection open, and send further requests using the same socket, which reduces connection overload.
Warning :As PHP is not thread-safe, the common suggestion is to install Apache with the “prefork” MPM
Directives and their meaning
ServerLimit 60 # Declares the maximum number of running Apache processes
StartServers 5 # The number of processes to start initially when starting the Apache daemon
MinSpareThreads 25 # The minimum number of idle threads available to handle request spikes
MaxSpareThreads 75 # The maximum number of idle threads
ThreadsPerChild 30 # How many threads can be created per server process
MaxConnectionsPerChild 2000 # Defines the number of connections that a process can handle during its lifetime. This can be used to prevent possible Apache memory leaks (if set to 0 the lifetime is infinite)
First of all, whenever an apache is started, it will start 5 child processes which is determined by StartServers parameter.
Then each process will start 30 threads determined by ThreadsPerChild parameter so this means 5 process can service only 150 concurrent connections/clients i.e. 30×5=150.
Now if more concurrent users comes, then another child process will start, that can service another 30 users. But how many child processes can be started is controlled by ServerLimit parameter, this means that in the configuration above, I can have 60 child processes in total, with each child process can handle 30 thread, in total handling 60×30=1800 concurrent users.
One must define these directive on basis on MPM module and hardware resources, otherwise, this can floodup the server
Time Out Configuration
This directive also play a vital role in tuning the performance of the webserver.
Please visit following article to understand and tune these parameters
It is always a good idea to run Nginx in front and run apache in the background utilizing a reverse proxy, especially if you are using SSL , then offloading SSL at Nginx level will have more improvement.
Use Apache eventMPM (and not the default Prefork or Worker)
Unless you are doing development work on the server, set ExtendedStatus Off and disable mod_info as well as mod_status.
Set DirectoryIndex correctly so as to avoid content-negotiation. Here’s an example from a production server:
DirectoryIndex index.php index.html index.htm
Consider reducing the value of TimeOut to between 30 to 60 (seconds).
For the Options directive, avoid Options Multiviews as this performs a directory scan. To reduce disk I/O further use
Options -Indexes FollowSymLinks
Compression reduces response times by reducing the size of the HTTP response. Install and enable mod_deflate – refer to the documentation or man pages and Add this code to the virtual server config file within the <directory> section for the root directory (or within the .htaccess file if AllowOverrides is On):