amuck-landowner

How to Optimize Apache for 128MB VPS?

vanarp

Active Member
In general Apache is known to consume much more memory than Nginx. But I keep coming across comments like "if configured properly Apache works fine too".

so I am looking for how to optimize Apache on a 128MB VPS and yet handle as much traffic possible. It will also be useful if you can provide any stats from a lowend apache server you might have.

Note: I am already using Nginx as my default webserver and trying to see if I can get Apache too working for my needs.
 

wlanboy

Content Contributer
Like lighttpd it is about the modules you are loading:

  • Go to /etc/apache2/mods-enabled
  • Run sudo a2dismod [name of module] for everthing you do not need
  • Restart apache
Next step is "apache2.conf/httpd.conf"

Keepalive is eating a lot of RAM:

  • MaxKeepAliveRequest
  • KeepAliveTimeout
And last but not least the number of running threads:

  • StartServers      
  • MinSpareServers
  • MaxSpareServers
  • ServerLimit      
  • MaxClients       
  • MaxRequestsPerChild  
Best values for a 128MB vps? Don't know - depends on the static/dynamic ratio.
 

vanarp

Active Member
@wlanboy, Thank you for presenting the various configurable items.

What are the most used or most unused Apache modules in general?

I use my VPSes for running CMS like WP with page caching enabled. So it is mostly (say ~80%) static content that the webserver will have to serve.

Also I prefer to run multiple sites on one VPS and migrate only those gaining serious traffic to their own VPS.

Nginx very much working for my requirement. Just want to explore how much Apache supports this kind of requirement.
 

SeriesN

Active Member
Verified Provider
For a low memory system, Nginx will treat you better than apache, regardless of how well you optimize it.
 

MannDude

Just a dude
vpsBoard Founder
Moderator
The thread is about Apache though, not Nginx. I'd do what wlanboy said and play around with the values until you've found something that works well for you :)
 

TheLinuxBug

New Member
@vanarp, I would say use Varnish + Apache+ Optimized MySQL.  Varnish has a very small foot print and can elevate the speed at which your static content is delivered from Apache, it also allows you to keep your required servers and childs down quite low as it will allow for reusing of open connections which eliminates the need for so many processes.  Please be careful with Varnish though if you have dynamic content, if you are not familiar with the VCLs it can get confusing.  If you decide to give it a try and get stuck, let me know and I would be happy to help you troubleshoot and provide you with some references.

Edit/Tip: Also please note that Debian 6 still uses Varnish 2.1.x and not 3.0, if you do an apt-get install you are installing the 2.x series.  If you are not careful with what you're reading, this can get confusing as the current (latest) version is 3.x. 

Cheers!
 
Last edited by a moderator:

Marc M.

Phoenix VPS
Verified Provider
The thread is about Apache though, not Nginx. I'd do what wlanboy said and play around with the values until you've found something that works well for you :)
@MannDude true, I was just suggesting a simple solution. Apache on its own isn't very big and doesn't take up allot of space, it's all the other modules and cruft that people enable that takes up lots of RAM, then they pick the wrong MPM and run Apache with mod_php. Yeah, 128MB RAM ain't gonna cut it on that. Use MPM_Event (works in 2.2 & 2.4) nad mod_fcgid, write a wrapper for it and use sensible settings. Don't load any PHP modules that you don't need either.
 

wlanboy

Content Contributer
As everyone was saying this is not an good idea and that this should not work ... well I have tried it and it is working:

Debian 7 on a 128 MB RAM KVM.

Apache2 + php5-cgi + MySQL hosting a wordpress blog with 9 plugins and about 5000 posts:

nx3vrd.jpg

2mod08x.jpg

What did I do? Looking to my usage logs of the blog

  1. Not more than 5 concurrent users (that are logged in)
    MaxKeepAliveRequests 10
  2. Not more than 10 concurrent guests
    <IfModule mpm_prefork_module>
        StartServers          1
        MinSpareServers       1
        MaxSpareServers      10
        MaxClients           20
        MaxRequestsPerChild   0
    </IfModule>
    <IfModule mpm_worker_module>
        StartServers          1
        MinSpareThreads       1
        MaxSpareThreads      10
        ThreadLimit          5
        ThreadsPerChild      10
        MaxClients           50
        MaxRequestsPerChild   0
    </IfModule>
    <IfModule mpm_event_module>
        StartServers          1
        MinSpareThreads       1
        MaxSpareThreads       10
        ThreadLimit          5
        ThreadsPerChild      10
        MaxClients           50
        MaxRequestsPerChild   0
    </IfModule>
Looking to the RAM after some http benchmarking (10 concurrent callers):


total used free shared buffers cached
Mem: 124548 113608 10940 0 3900 44012
-/+ buffers/cache: 65696 58852
Swap: 223228 7328 215900


Not that bad. The php5-cgi handling is at least as good as the one of lighttpd.

@vanarp

Thank you for your question.

Looks like I have to rebuild my apache image in my head - the one painted with Apache 1.X.
 

TheLinuxBug

New Member
@wlanboy,

You should add Varnish in front of  that, you can pretty much use the out of the box vcl with varnish.  The only thing you may need to research is if you want to have the actual client ip show in your logs, then you will need mod_rpaf for Apache and you will need to use the code to pass the X-Forwarder-For header.

Though, looking at your memory usage, you may want a small bit more ram or to run the varnish as a proxy on a secondary server. You may be able to cut a small bit of memory usage out of MySQL with tuning to fit Varnish in also.  With WordPress sites even with a lot of plugins your talking a 2-3 second or more load time cut and it will cut some of the usage on Apache off by caching things in the proxy.  Will mean less MySQL requests also.

Edit: I prefer to cache in memory if it is spare, however, you may be able to use drive cache instead and not need as much ram.  Either way, check it out and let me know if it speeds up your site :)

Decided to add a few quick pointers to make your install a little bit easier:

To install on .deb based system:


# apt-get install varnish
# varnishd -V
varnishd (varnish-2.1.3 SVN )
Copyright (c) 2006-2009 Linpro AS / Verdens Gang AS
 
(check your version and make sure any documentation you check on the net is for the right version. Debian 6 had version 2.1.x not sure what Debian 7 comes with in its repos.)

Want to see client ips in Apache logs?

Apache mod_rpaf:

Get mod_rpaf for Apache: http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

This article give the general idea of how to get it installed: http://www.bxtra.net/articles/2011-02-13/how-to-install-modrpaf-for-apache-22

Varnish V3:

This is the VCL which needs added to version 3.x (note this goes under the "sub vcl_recv {"):


if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}

Varnish V2:

In Varnish V2.x is it a little easier (like above, under "sub vcl_recv {"):


set req.http.X-Forwarded-For = req.http.rlnclientipaddr;

All Versions:

Remember to exclude your admin paths, install folders, etc using the following code example (this would also be under "sub vcl_recv {"):


# Do not cache these paths.
if (req.url ~ "^/server-status$" ||
# (Below) This will take folder forums and everything under it and not cache it
req.url ~ "^/forums" ||
# This will not cache anything on the page install.php
req.url ~ "install.php" ||
req.url ~ "upgrade.php" ||
req.url ~ "sitemap.xml.gz" ||
req.url ~ "sitemap.xml") {
# This tells varnish not to cache these items, but instead to pass you through to the backend
return (pass);
}
}


Hope this helps out!

Cheers!
 
Last edited by a moderator:

Mun

Never Forget
You can run apache in low memory environments. A few things, turn down keep alive to ~ 5 seconds. Reduce the number of max connections, I think it is set at 125 by default which is far too much for that vps.  Remove any extra fat on the VPS such as bind. Use caching methods such as APC. Even in low memory environments they help a ton, and reduce total load. Also if you are using wordpress install quick cache and others. Finally it might be advantageous to setup cloudflare in front of your VPS.

Mun
 

Master Bo

Member
Well, not much hype, actually. Apart from some really useful things, such as mod_security, Apache is inferior to Nginx in almost every aspect in most typical setups.

Note that if you run PHP-driven CMS, Apache isn't the only resources eater. Make sure you don't load unnecessary PHP modules, and make sure you tune SQL server as well. Personally, I prefer using MariaDB as MySQL drop-in replacement.
 

Master Bo

Member
I mean Apache module, ModSecurity.org implementation (mod_security package in case of RedHat/CentOS/Fedora).

I know there's Nginx implementation, but it's still not a stable release.
 
Last edited by a moderator:

vanarp

Active Member
@TheLinuxBug, I read in a few places that people are running sites on Apache while using Nginx as a reverse proxy. What do you think works better - Varnish or Nginx proxy?
 
Top
amuck-landowner