This page looks best with JavaScript enabled

Optimizing Apache Memory Usage using mpm prefork

 ·  ☕ 3 min read  ·  ✍️ Chris Titus

This guide goes over optimizing apache memory usage by configuring mpm prefork module to the best values for your server. Afterword, you should notice substantial increases in performance especially if you are using free micro instances from Google Cloud or AWS.

Commands used:

  • Check Apache Memory usage
    • ps -ylC apache2 --sort:rss | awk '{sum+=$8; ++n} END {print "Tot="sum"("n")";print "Avg="sum"/"n"="sum/n/1024"MB"}'

      Note: My result was between 68-71 MB with stock modules loaded, however, your results will vary.

  • Launch Memory manager/resource viewer
    • htop‘ OR you can use ‘top‘ for fewer options and visuals
  • 10/4/2018 UPDATE: Follow this Github script if you want to take the guesswork out, due to this being a curl script you may want to review the code first.
    • curl -sL https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl | perl

Values to calculate:

Total System Memory Free – Buffer / Apache2 MB usage = MaxRequestWorkers, ServerLimit, and MaxClients

apache2

My Settings: Server Instance (Standard) 1 dedicated vCPU with 3.5 GB of Memory

Please note that the below settings is done from a stock apache configuration. With all things considered these values are very conservative and therefore can be used if you are hosting with these specs or greater. In future articles I’ll go through disabling un-needed modules to reduce apache’s footprint, thus allowing you to increase these numbers.

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of requests a server process serves
StartServers 3
MinSpareServers 5
MaxSpareServers 10
ServerLimit 45
MaxClients 45
MaxRequestWorkers 45
MaxConnectionsPerChild 2000

45 processes x 68 MB per = 3060 MB Free Memory needed for the above configuration

Comparatively, a microserver with 600-700 MB of ram and shared CPU you will be looking at 1-3 Start/Min/Max Start and Spare servers, and no more than 10 for ServerLimit/MaxClients/MaxRequestWorkers. There are other modifications you need to make in culling down apache modules, therefore you can increase these values further.

Video Walkthrough

apache-yt
Note: YouTube Video - Hold Ctrl + Left Click to open in new window

Contact Me

I live stream on Chris Titus Tech YouTube Channel every Friday at 10 AM CST and archive clips to Titus Tech Talk. I also regularly publish to christitus.com, but if you’d like to contact me directly or want to contribute to help keep these articles and videos being made consider joining the CTT members.

Two Memberships exist:

  • ChrisTitus.com Members Section (CC Only)
    • Full Archive of All Unlisted Live Streams
    • Direct Members Only Email
    • Monthly Members Only Video
    • Starting at $2 Per Month (100% of Proceeds goes to Chris Titus Tech)
  • YouTube Chris Titus Tech Membership (All Payments Accepted)
    • YouTube Emojis for Comments and Live Chat
    • YouTube Badges that changes based on membership time for comments and chat.
    • All YouTube comments are highlighted when I review comments daily.
    • Immediate Access to Full Live Streams
    • $4.99 Per Month (70% of the Proceeds goes to Chris Titus Tech)
Share on

Chris Titus
WRITTEN BY
Chris Titus
Tech Content Creator