Page 1 of 1

[HowTo] Optimize Apache on CentOS 7 for VestaCP

Posted: Tue Sep 11, 2018 6:49 pm
by xorro
Apache is a powerful and capable open-source web server, designed to provide a balance of flexibility, portability, and performance. Apache optimization is an important task for every system administrator.

You can improve Apache’s performance by customizing the Apache configuration without needing to add additional hardware like RAM, CPU etc.

This post describes various Apache configuration options that can be used to improve Apache performance without adding additional hardware resources to your system.

Requirements
  • A server running CentOS 7 or centos 7 + vesta cp
  • Apache installed and running or Vestacp installed with Apache/Apache + nginx
MaxKeepAliveRequests

MaxKeepAliveRequests is the maximum number of requests to serve on a TCP connection. It limits the number of requests allowed per connection. If it is set to 0, unlimited requests will be allowed. You can set it to any value you desire.

Keep this setting to a high value for maximum server performance. The recommended value of MaxKeepAliveRequests is 500.

To change this setting, edit the Apache configuration file:

Code: Select all

sudo nano /etc/httpd/conf/httpd.conf
Add the following line:

Code: Select all

MaxKeepAliveRequests 500
Save and close the file when you are finished.

KeepAliveTimeout

KeepAliveTimeout defines the number of seconds Apache will wait for the new request from connected clients before closing the connection. (Once the server receives a request, the Timeout directive applies instead.)

By default Keepalive is disabled in CentOS 7. If Keepalive is set to on, it is a good idea to set the KeepAliveTimeout value low.

The recommended KeepAliveTimeout can be between 1 to 5.

You can do this by editing Apache configuration file:

Code: Select all

sudo nano /etc/httpd/conf/httpd.conf
Add the following line:

Code: Select all

KeepAliveTimeout 5
Save and close the file when you are finished.

KeepAlive

KeepAlive sets whether the server allows more than one request per connection. It can be used to prevent any one client from consuming too much of the server’s resources.

By default KeepAlive is disabled in CentOS 7. Once the Apache server is getting requests from hundreds and thousands of IPs at once, this setting should be On.

You can enable this setting by editing Apache configuration file:

Code: Select all

sudo nano /etc/httpd/conf/httpd.conf
Add the following line:

Code: Select all

KeepAlive On
Save and close the file when you are finished.

Configure MPM Prefork

One reason for poor Apache performance is that Apache is having trouble coping with the load. The Apache MPM (Multi-Processing Module) can help.

mpm_prefork_module is included and enabled in the default Apache installation on CentOS 7. To confirm this run the following command:

Code: Select all

sudo apachectl -t -D DUMP_MODULES |grep mpm
You should see mpm_prefork_module (shared) if mod_deflate is installed and enabled.

You can make Apache performance better using the Apache MPM prefork module. To do this, set the following parameters in your Apache configuration file:

Code: Select all

sudo nano /etc/httpd/conf/httpd.conf
Add the following lines:

Code: Select all

KeepAlive Off
<IfModule prefork.c>
   StartServers        5
   MinSpareServers     5
   MaxSpareServers     10
   MaxClients          150
   MaxRequestsPerChild 3000
</IfModule>
Save and close the file, then restart Apache to reflect these changes.

Code: Select all

sudo apachectl restart
Explanations

StartServers: This directive sets the number of child server processes created on startup. It is a good idea to increase this number on a high-load server, so the server is ready to handle a lot of connections.

MinSpareServers: This directive sets the minimum number of idle child server processes. This value will need to be tuned for high-load servers.

MaxSpareServers: This directive sets the maximum number of idle child server processes. When there are more idle child server processes than defined by MaxSpareServers the idle process will be killed.

MaxClients: This directive sets the maximum number of simultaneous requests that Apache will handle. When this limit has been reached, any other connection attempts will be queued.

Number of MaxClients = (Total RAM memory – RAM memory used for other process except Apache process) / (Memory used by a single Apache process)

MaxRequestsPerChild: This directive sets how many requests a child process will handle before terminating. Once the limit has been reached, the child process will die.

Note: If this value is 0, then the process will never die.

AllowOverride

If AllowOverride is set to ‘All’, then Apache will attempt to open a .htaccess file in each directory that it visits.

For example:

Code: Select all

 DocumentRoot /vaw/www/html/website1
 <Directory /> 
 AllowOverride All
 </Directory>
If a request is made for URI /index.html, then Apache will attempt to open .htaccess file in /, /var/, /var/www/, /var/www/html/ and /var/www/html/website1/.

Therefore, it is good idea to add AllowOverride all for a specific directory only:

Code: Select all

DocumentRoot /vaw/www/html/example
 <Directory /var/wwww/html/example/admin> 
   AllowOverride All
 </Directory>
DNS Lookups

The biggest reason for Apache web server slowdowns is the time required to perform DNS lookups. Apache will record the full host name of each incoming client connection in its access.log file. Resolving each one eats up a significant chunk of time.

The HostnameLookups option enables DNS lookup so that hostnames can be logged instead of the IP address. By default HostnameLookups is Off in Apache.

You can verify that this is the case by editing the Apache config file:

Code: Select all

sudo nano /etc/httpd/conf/httpd.conf
Be sure the HostnameLookups line reads:

Code: Select all

HostnameLookups Off
Save and close the file when you are finished, then restart Apache to reflect changes.

Code: Select all

sudo apachectl restart

Re: [HowTo] Optimize Apache on CentOS 7 for VestaCP

Posted: Wed Sep 19, 2018 12:04 am
by pipoy
Wow thank you!

Re: [HowTo] Optimize Apache on CentOS 7 for VestaCP

Posted: Mon Apr 15, 2019 12:08 am
by dsystem
Good tips for optimizing Apache.

Vou deixai mais duas dicas aqui:

1) Reading this knowledge base is great for getting deeper into the subject. -> https://www.liquidweb.com/kb/apache-per ... ap-memory/

2) The Apache2Buddy script, similar to MySQLTuner, looks at the Apache configuration and makes suggestions based on Apache process memory and general RAM -> https://github.com/richardforth/apache2buddy

Re: [HowTo] Optimize Apache on CentOS 7 for VestaCP

Posted: Sat Jun 15, 2019 1:40 am
by adamjedgar
a great tutorial.

Would be good to add a how to change from mpm_prefork to mpm_event (or mpm_worker) as i believe these two are going to produce much greater cost savings in terms of system resource usage (particularly RAM i believe)