DEV Community

Jihad Sinnaour
Jihad Sinnaour

Posted on

Apache2 (Optimisations)

⚡ Installation

Global

apt-get install apache2-utils
Enter fullscreen mode Exit fullscreen mode

PHP-FPM

apt-get install php7.4-fpm
Enter fullscreen mode Exit fullscreen mode

⚡ Calculation

(1 CPU cores, 2 GB RAM)

Process size

Size per each single process.

wget https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py -O usage.py
chmod a+x usage.py
python usage.py
lscpu
top
top -c -p `pgrep apache -d','`
top -c -p `pgrep mariadb -d','`
top -c -p `pgrep php -d','`
Enter fullscreen mode Exit fullscreen mode
{Process size} = {Used RAM (Item)} / {Process (Item)}

# Apache:
# 50M / 5P
# 10M
# (~ 150)

# PHP-FPM:
# 200M / 5P
# 40M
# (~ 256M)

# MySQL:
# (~ 256M)
Enter fullscreen mode Exit fullscreen mode

MaxRequestWorkers

MaxRequestWorkers = ({Total RAM} - {Used RAM (Kenel, MySQL...)}) / {Process size}
# (2000 - 600) / 50M
# 28M
Enter fullscreen mode Exit fullscreen mode

PHP-FPM max-children

maxclients = ({Total RAM} - {Used RAM (Kenel, MySQL...)}) / {Process size}
# (2000 - 600) / 50M
# 28M
Enter fullscreen mode Exit fullscreen mode

⚡ Configuration

Global

a2enmod deflate # GZIP
a2dismod mod_ruby # Unused
a2dismod mod_perl # Unused
Enter fullscreen mode Exit fullscreen mode

@ /etc/apache2/apache2.conf

Timeout 120
KeepAlive On
MaxKeepAliveRequests 500
KeepAliveTimeout 3
Enter fullscreen mode Exit fullscreen mode

Cache

a2enmod cache
a2enmod cache_disk
a2enmod expires
a2enmod headers
Enter fullscreen mode Exit fullscreen mode

@ /etc/apache2/sites-available/{domain}.{tld}.conf

@ /etc/apache2/sites-available/000-default.conf

<IfModule mod_disk_cache.c>
    CacheQuickHandler off
    CacheLock on
    CacheLockPath /tmp/mod_cache-lock
    CacheLockMaxAge 5
    CacheIgnoreHeaders Set-Cookie
    CacheEnable disk
    CacheHeader on
    CacheDefaultExpire 800
    CacheMaxExpire 64000
    CacheIgnoreNoLastMod On
    ExpiresActive on
    ExpiresDefault A300
</IfModule>
Enter fullscreen mode Exit fullscreen mode

MPM (Multi-Processing)

a2dismod mpm_prefork
a2dismod mpm_event
a2enmod mpm_worker
Enter fullscreen mode Exit fullscreen mode

@ /etc/apache2/mods-available/mpm_worker.conf

<IfModule mpm_worker_module>
    StartServers 1 # {CORES}
    ServerLimit 150 # {RAM}
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadLimit 64
    ThreadsPerChild 25
    MaxRequestWorkers 150 # {RAM}
    MaxConnectionsPerChild 1000
</IfModule>
Enter fullscreen mode Exit fullscreen mode

PHP-FPM

a2enmod proxy_fcgi setenvif
a2enconf php7.4-fpm
Enter fullscreen mode Exit fullscreen mode

@ /etc/php/7.4/fpm/pool.d/www.conf

pm = dynamic
pm.max_children         256 # ({Total RAM} - {Used RAM (Kenel, MySQL...)}) / {Process size}
pm.start_servers        4 # ({CORES} * 4)
pm.min_spare_servers    2 # ({CORES} * 2)
pm.max_spare_servers    4 # ({CORES} * 4)
pm.max_requests         1000
Enter fullscreen mode Exit fullscreen mode

Restart

service apache2 restart
service php7.4-fpm restart
systemctl start apache-htcacheclean
Enter fullscreen mode Exit fullscreen mode

Test

PHP-FPM

php-fpm7.4 -t
Enter fullscreen mode Exit fullscreen mode

Cache

ls -la /var/cache/apache2/mod_cache_disk/
Enter fullscreen mode Exit fullscreen mode

ApacheBench

ab -n 1000 -c 100 -k -H "Accept-Encoding: gzip, deflate" [http[s]://]hostname[:port]/path
Enter fullscreen mode Exit fullscreen mode

Authors:

  • Jihad Sinnaour - Jakiboy (Initial work)

⭐ Support:

Please give it a Star if you like the project.

Top comments (0)