4

I have an "archive" server for old sites that no longer are being used, but I want to keep them online as part of my CV/Portfolio.

They require different apache and php versions and setups, so I am running multiple apaches on different ports, behind a nginx reverse proxy.

However, it is not very often that those sites have visitors. It might take days or even weeks between the visits, so I think it is a big wast of memory and cpu to have all those apache instances running all the time.

What I would like to do is to make nginx start the appropriate apache server on demand. Maybe something like this:

  1. Incoming http request to nginx.
  2. nginx checks if an apache server is responding on it's tcp port.
  3. If apache is not responding: run some script to start apache.
  4. When apache starts responding, reverse proxy the http request to apache.

I want the apache daemon to be loaded with PHP as module. I want the experience to be fast and good when a visiter is surfing the sites, however, a couple of seconds of loading time on the first request is no problem. Some of the sites are very AJAX intensive, so loading apache for every request is not an option.

I do not find any obvious way to do this. Does anyone have any ideas or experiences on a similar setup? Are there other reverse proxy softwares (than nginx) that would do this?

(Of course, I would also need a way to shut down apache on inactivity, but that is quite simple with a cron job just checking if something has happend in apache's access.log.)

Btw.. The server is running Debian Lenny.

Edit / my solution:

I solved this problem by scripting with https://github.com/nodejitsu/node-http-proxy for node.js.

// I'm using http-proxy to make the proxying:
var server = httpProxy.createServer(function (req, res, proxy) {
  var domain = getDomain(req);
  proxy.proxyRequest(req, res, {
    host: '127.0.0.1',
    port: configuration[domain].port
  });
}).listen(80);

// And a error handler, which will start apache on deman:

server.proxy.on('proxyError', function(err, req, res) {
  if (err.errno == 'ECONNREFUSED') {
    // Start apache using exec()
  }
});
8
  • Unless there is a need for running Multiple Apache instances, why not run one PHP instance for each version required in CGI mode via an init script, then simply have each site defined in Nginx and use a different version of PHP per site cutting out Apache all together? Apr 21, 2012 at 22:45
  • 1
    I think an average Apache install only has a memory footprint of about 25~30MB, and you can cut this down to 15MB or less if you disable all the unneeded modules. Also, Apache doesn't use much CPU when just listening. Though if you're running MPM, you can reduce MinSpareServers and MaxSpareServers if you want to minimize the number of idle child processes running. Apr 22, 2012 at 3:43
  • CGI is too slow. I do not want the experience to be bad when the site has loaded.
    – Alfred Godoy
    Apr 22, 2012 at 7:54
  • I'm in general agreement with Lèse majesté that you should just leave the servers running, perhaps in pared down mode. The startup time is too prohibitive for your occasional visitor to have nginx do the startup. On the other hand, if it's a pretty much static site, you might just cache everything with Varnish
    – Old Pro
    Apr 22, 2012 at 8:00
  • Those who are visiting those sites are customers who want to have a look at their previous projects, examples used when discussing new projects with customers, and people who know why they want to see what I have done. A couple of seconds on the first request is no problem. It is not supposed to be occasional visitors surfing those sites.
    – Alfred Godoy
    Apr 22, 2012 at 8:12

2 Answers 2

5

An alternative approach might be to start apache via a script-call defined in inetd.conf.

This approach is detailed in the article Starting Lighttpd on demand, which although Lighttpd-oriented, might possibly be made to apply to your version of apache.

The article above contains the script for starting the Lighttpd web-server on reference to a given port, where lighttpd needs probably be substituted to apachectl. It also details some ideas on how it can also be automatically shut-down so it doesn't stay running forever.

If this approach is applicable to your case, you might consider adding your inetd.conf configuration-line and scripts to your post for future readers.

1
  • yah .. make inetd react on localhost:1234 and then refer to localhost:1234 from within nginx.conf .. i like this approach.
    – akira
    Apr 22, 2012 at 15:47
1

You could run apache via inetd (or xinetd). See the ServerType directive in the apache config docs.

When running a TCP service via inetd, you'll start up a process per request. So when there are no requests, there are no apache processes. inetd can listen on all of the appropriate ports.

3
  • ServerType was removed in Apache 2.0 and is not supported.
    – Old Pro
    Apr 22, 2012 at 7:56
  • That is a good idea, however, that would make the sites very slow and give a bad experience. Some of them are very AJAX intensive... In another situation this would be a good solution, for me however, I want the apache daemon loaded with PHP module for speed.
    – Alfred Godoy
    Apr 22, 2012 at 7:59
  • Added this in my question.
    – Alfred Godoy
    Apr 22, 2012 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .