1. Optimize hardware and operating system
a) RAM
Ensure you have enough RAM space to increase apache web server performance especially during caching. If RAM fills up the requests will be redirected to swap space which increases latency. This translates to users having to reload their request, hence increasing the load further. To resolve this issue i.e. limit the number of child process spawned by the Apache web server beyond which they are redirected to swap space, you have to set the MaxRequestWorkers. To get the best value for this do the following procedure:
- Use the top command to check the average RAM size used by apache process
- Divide the total available RAM size by the average RAM size used by apache to get the number of MaxRequestWorkers
To increase the performance of Apache, ensure you have faster CPU, faster network card, and faster disks.
b) Write Buffer Size
Increase the write buffer size by increasing the value in the two files:
/proc/sys/net/core/wmem_max /proc/sys/net/core/wmem_default
c) Maximum Open Files
If apache web server is handling high loads, it’s recommended to increase the maximum number of allowed open files by increasing the value in the file:
/proc/sys/fs/file-max
Then run the command:
$ ulimit -H -n 4096
2. Tune Run-Time Configuration
a) Disable .htaccess using AllowOverride
Enabling AllowOverride
makes apache try to open .htaccess
whenever a request is made. This really reduces the performance of the Apache web server. To resolve this, use the option AllowOverride None
as shown below:
DocumentRoot "/www/var/html" <Directory "/"> AllowOverride None </Directory>
b) Disable DNS Lookups using HostnameLookups
When HostnameLookups
is enabled i.e. turned ON
, it increases latency for each request to be completed after performing DNS lookup. There are two solutions to this:
- For logging files with resolved names, do post-processing of the logs on a different machine to prevent slow-down of performance in production server.
- If you don’t necessarily need name resolution then use IP addresses, and disable DNS lookup altogether by using the option below:
HostnameLookups off
3. Optimize Compile-Time Configuration
a) Multi-Processing Modules (MPMs)
MPM help apache be able to bind to network ports, utilize children processes and threads alternately to accept and handle requests. There are 3 different types of MPMs and each is suitable for different requirements.
worker: Utilizes multiple child processes, each with many threads, while each thread is handling one connection at a time. This is suitable for high traffic servers owing to its smaller memory footprint than prefork.
event: Similar to the worker MPM in regards to threading, but allows for simultaneous serving of more requests through passing off some processing to supporting threads. This helps free up memory which is allocated to other threads. Not suitable for use with non-thread-safe modules like mod_php.
prefork: Utilizes multiple child processes, each with one thread. Each process handles one connection at a time. It’s faster than a worker but uses more memory. Suitable for debugging applications using non-thread-safe modules like mod_php.
To check your MPM version, run command
$ sudo httpd -V
Output:
[linuxuser@centos7-apache ~]$ sudo httpd -V [sudo] password for linuxuser: Server version: Apache/2.4.6 (CentOS) Server built: Apr 20 2018 18:10:38 Server's Module Magic Number: 20120211:24 Server loaded: APR 1.4.8, APR-UTIL 1.5.2 Compiled using: APR 1.4.8, APR-UTIL 1.5.2 Architecture: 64-bit Server MPM: prefork threaded: no forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_PIDLOG="/run/httpd/httpd.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="conf/mime.types" -D SERVER_CONFIG_FILE="conf/httpd.conf"
The current MPM is prefork, if you want to change it, edit as follows:
For Centos 7
$ sudo vim /etc/httpd/conf.modules.d/00-mpm.conf
For Ubuntu 16.04
$ sudo vim /etc/apache2/mods-available/<mpm>.load
Where <mpm>
is mpm_prefork
, mpm_event
or mpm_worker
b) Remove Unnecessary Modules
Some of the modules that you may not need in your web server include but not limited to mod_mime
, mod_dir
, mod_php
, mod_perl
, mod_ruby
, e.t.c. This will help save RAM memory used by the web server.
In Centos 7, to disable or unload a module, comment out< the line that starts with LoadModule in the main config file or files inside /etc/httpd/conf.modules.d/
In Ubuntu 16.04, to disable or unload a module, run the command:
$ sudo a2dismod module_name
To enable a module
$ sudo a2enmod module_name
Then restart apache service.
Check out the top 3 Best web hosting services
- Your query to the best web hosting can end by clicking on this link.