Apache running in 48 MB of RAM

Inspired by recent advice to automatically uninstall Apache to fix memory consumption, I edited a Linode on my account that I use for Minecraft to only have 48 MB of RAM available to it, then installed Ubuntu 10.10, prefork Apache2, and PHP 5. The server has 13 MB of RAM committed and 26 MB free and has never touched swap since boot, even under a heavy ApacheBench to a PHP page.

This is not an example of how to run Apache in an efficient manner, but it is a small demonstration that things are not always as they seem and that the tool can be crafted for the job at hand.

phpinfo: http://minecraft.jedsmith.org/

free -m/ps (live): http://minecraft.jedsmith.org/memory.php

I'm tempted to install phpBB and see if it will run. Now that I've posted this, someone will probably OOM it, so have fun. :)

8 Replies

Thanks muchly for posting this, I too have been noticing a recent growth in the "Well your first mistake is apache. Burn apache, then throw the ashes into a volcano, and then blow up the volcano." responses.

If you wouldn't mind, could you give some advice as to how you configured that system to minimize memory usage?

Borderline on-topic… just which way to "sum" memory usage of things like php-cgi subprocesses is the correct one (if any exists)? Seems to me that RSS column does add the "shared" part to all instances, as these sum up to about 4x than my total memory use…

Anyway… apache isn't that bad on memory, especially with worker/fastcgi instead of prefork/mod_* . And inb4 "if you do fastcgi already, there's no reason for you to stick to apache": there's also no need to switch. Not to mention that apache is the most familiar and most configurable one out there.

PS. Jed, is a Li512 enough for minecraft server, or does it still try to alloc over a gigabyte at start?

@jed:

free -m/ps (live): http://minecraft.jedsmith.org/memory.php

The Monte Carlo hit counter is a nice touch. I wound up reloading the page a bunch of times to watch it change.

I don't claim that Apache can't be made memory efficient with a properly tweaked config, only that it's far more effort to do so than use a more or less stock lighttpd or nginx config, which have sane configurations out of the box.

Sure, you could spend hours figuring out and tweaking Apache, or you could spend five minutes installing lighttpd or nginx and be done with it. I also favour fastcgi, although that can also be done with Apache. But again, it's a lot easier to set up fastcgi with lighttpd than apache since it's the default way of handling PHP in lighttpd.

Setting up perl with lighttpd however, is much less fun. At least with fastcgi.

EDIT: Also, Minecraft rocks. Too bad the author chose Amazon over Linode ;) I've forced myself to stop playing it, as it was consuming my life.

@jed:

I edited a Linode on my account that I use for Minecraft to only have 48 MB of RAM available to it, then installed Ubuntu 10.10, prefork Apache2, and PHP 5.

It's worth pointing out a few things this demo doesn't have, unlike many of the bulkier PHP deployments: no CMS/framework, no images/CSS/JavaScript objects, and no time-consuming SQL queries. A framework adds bulk, objects multiply concurrent requests, and SQL queries lengthen the time per request. Since prefork requires a fully-stocked forked Apache process for each concurrent request, whether it requires PHP or not. That's where nginx and lighttpd come in.

How happy is PHP with mpm_worker/multithreading these days? That would be really, really useful.

@Erasmus Darwin:

The Monte Carlo hit counter is a nice touch. I wound up reloading the page a bunch of times to watch it change.

It looks really familiar.

mod_php still needs prefork.

I'm using worker with modfastcgi (NOT modfcgid!) and I'm happy about everything except one little detail that may be a deal-breaker for you: fastcgi's process manager cuts the FCGI socket and SIGTERMs the PHP tree (and anything else you'd have hanging off FCGI) when you SIGUSR1 apache for a graceful config reload. In result, every connection that was in the middle of a PHP-processed request receives HTTP 500.

I'm not running anythig mission-critical, so setting up logrotate to the time of lowest traffic and warning users when reloading apache's config during the day works for me.

@hoopycat:

@Erasmus Darwin:

The Monte Carlo hit counter is a nice touch. I wound up reloading the page a bunch of times to watch it change.

It looks really familiar.
It should, it's yours (I'm filegetcontents()ing the URL).

@Guspaz:

Sure, you could spend hours figuring out and tweaking Apache, or you could spend five minutes installing lighttpd or nginx and be done with it.
The configuration here took about 60 seconds, of just turning all the prefork values down to near-minimum.

@rsk:

PS. Jed, is a Li512 enough for minecraft server, or does it still try to alloc over a gigabyte at start?
The node was 1 GB before, and it makes me nervous to run Minecraft on it. SMP Alpha is buggy enough anyway that it isn't worth it to me.

@akerl:

If you wouldn't mind, could you give some advice as to how you configured that system to minimize memory usage?
Surprisingly, I just turned the prefork values down to almost the bare minimum, and disabled all but four modules (authz_host, dir, mime, and php5).

Clearly it's not going to be very performant, but it does work. I even stopped atd, cron, and some other services (as you can see in the ps output).

@hoopycat:

It's worth pointing out a few things this demo doesn't have, unlike many of the bulkier PHP deployments: no CMS/framework, no images/CSS/JavaScript objects, and no time-consuming SQL queries. A framework adds bulk, objects multiply concurrent requests, and SQL queries lengthen the time per request.
This.

@Guspaz:

Sure, you could spend hours figuring out and tweaking Apache, or you could spend five minutes installing lighttpd or nginx and be done with it.
For an awfully large percentage of the cases discussed in these forums, simply dropping MaxClients (or equivalent setting depending on worker model) is all it would take to get the lion's share of the improvement with very minimal time and effort.

– David

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct