Starting Lighttpd on demand

and freeing up memory for other uses

Introduction

The default web server on MBWE, lighttpd, is lightweight, but with the perl administration interface of the box, it is still using a huge part of the total memory of your MBWE.

 

Since a web server is designed for serving web pages fast, it is integral that the necessary parts of the server (including cgi interfaces like perl and php) are kept in the memory. However, you're probably using your MBWE's web interface pretty seldom and really don't need all that stuff to be kept in the memory all the time.

 

Well, as lighttpd (or any other decent web server) isn't designed to be used like that, turning parts of it on and off when needed, our only option is to only start the whole daemon on demand.

 

With other networking services, you could achieve this through inetd, but again, lighttpd isn't designed to be used like that. Older versions (>= 1.3) of apache support starting through inetd, but you probably don't want to be trying to figure out how to make and older version of apache work on your MBWE, or how to make the web interface work on the older version of apache.

 

One possible solution would be starting and stopping lighttpd using the spindown script's on_spinup and on_spindown functionality, but you probably won't be needing lighttpd every time the drives spin up...

 

Anyway, with a little bit of shell magic, starting lighttpd only on demand is not only possible but also pretty simple.

Prerequisites

Enabled SSH access.

A text editor and the know-how to use it. I recommend installing nano, but vi will also do just fine.

The how-to

  1. Make sure you are in superuser mode

    # su

    You can check your memory usage with commands free or top. Just check them before and after stopping lighttpd to see how much memory you're freeing...

    Stop lighttpd:

    # /etc/init.d/lighttpd.sh stop
  2. Prevent lighttpd from starting during boot by editing /etc/init.d/network_servers.sh and commenting out the following line:

    $SCRIPTS_PATH/lighttpd.sh start

    You don't have to comment out the line stopping lighttpd, because you'll want to stop it when shutting MBWE down, in case it is running at the time.

  3. What we are going to do, is that we'll use a shell script that is run on demand (with the help of inetd) when accessing it with a web browser, which starts lighttpd and redirects the browser to the real address of the web interface. Simple, eh?

     

    Now, to the shell script. To the browser, it looks like a web server, but on the inside it only echoes few http headers and either starts or stops lighttpd.

    You can either copy-and-paste the script from here...

    #!/bin/bash
    #
    # start_lighttpd
    # a shell script to start lighttpd on mbwe through inetd
    # written by kyyhkynen at gmail dot com
    # see http://kyyhkynen.net/stuff/mybook for further info
    #
    # you may use and modify this script any way you want as long as you keep this header attached
    # last modified May 20th, 2008
    ################
    # Configuration
    ################
    
    # the address you'll want the script to redirect your browser to after starting lighttpd
    # just use the same address you use to access your mbwe's web interface 
    # usually it is the name or ip address of your mbwe prepended with http://
    REDIRECT_ADDRESS="http://mbwe/"
    
    #######################
    # end of configuration
    # no need to edit anything below this line
    ######################
    
    # find out if lighttpd is already running
    LIGHTTPD_IS_RUNNING=`ps -A | grep ' lighttpd$' | wc -l`
    
    # if it is not running, start it and redirect browser
    if [[ "$LIGHTTPD_IS_RUNNING" -eq "0" ]]; then
    
    	/etc/init.d/lighttpd.sh start >/dev/null;
    
    	# for future purposes, read the request
    	read REQUEST ;
    
    	# and the request headers
    	while /bin/true; do
    		read HEADER ;
    		[ "$HEADER" == $'\r' ] && break ;
    	done ;
    
    	# make browser to go to the redirect address
    	echo -e "HTTP/1.1 302 Found\r";
    	echo -e "Location: ${REDIRECT_ADDRESS}\r";
    	echo -e "Connection: close\r";
    
    # if it is running, stop it and return 404 error
    else
           /etc/init.d/lighttpd.sh stop >/dev/null;
           echo -e "HTTP/1.1 404 Not Found\r";
    	echo -e "Status: 404 Not Found\r";
           echo -e "\r";
           echo -e "Lighttpd stopped.\r"
    fi
    
    echo -e "\r";
    echo -e "\r";
    echo -e "\r";
    

    ...or you can just get it with wget:

    # wget http://kyyhkynen.net/stuff/mybook/start_lighttpd

    Put the script somewhere convenient (I'm putting it into /usr/sbin/ and make sure it is executable.

    # mv start_lighttpd /usr/sbin
    # chmod a+x /usr/sbin/start_lighttpd

    Now, edit the script and set the REDIRECT_ADDRESS variable to where you want to go. That is most likely to the web interface of your MBWE, so set it to http://path-to-your-mbwe/.

  4. Next, edit /etc/inetd.conf and add following line to it:

    81 stream tcp nowait root /usr/sbin/start_lighttpd start_lighttpd

    Note the path to the script and the port number in the beginning of the line. This is the port you'll be connecting to with your web browser and which you're redirected to the real web interface from. I'm using 81, you can use any port you like as long it isn't reserved by any other service.

    Save the file and restart inetd with

    # /etc/init.d/inetd.sh restart

    You can once more make sure that your lighttpd is really not running with

    # ps -A | grep lighttpd
  5. Now just navigate with your browser of choice to http://[path-to-your-mbwe]:[port-youre-using] (http://mbwe:81 for me) and if everything goes well, you should be redirected to the web interface automatically with a slight delay while the lighttpd is started.

  6. Next we should make it stop, too. I'm not going to provide a definite solution for this, since there are multiple possible solutions for this, and the best one depends on how you're using your MBWE.

    Only thing common with the solutions is that you ultimately want to execute command

    /etc/init.d/lighttpd.sh stop

     

    Here are a few alternative ways how to do it:

    - you might stop it the same way it was started. Just navigate back to http://[path-to-your-mbwe]:[port-youre-using] and lighttpd will be stopped if it is running.

    - if you're using the spindown script, the best option would be to turn off lighttpd just before spinning the drives down. This is easily achieved using the on_spindown functionality of the script. For more information, see the guide.

    - you could create a cron job to stop lighttpd at specified time. The problem is that even though you could be using lighttpd at that time, it would be still be stopped.

    - if you know you won't be using lighttpd longer than a specified amount of time, you could fire up another shell script from the start_lighttpd script, that sleeps for the specified amount of time and then stops lighttpd before exiting.

  7. And there you have it; an on-demand-lighttpd and a few megabytes of precious free memory.

     

    You can also easily modify the script to do other stuff only by accessing a certain port on your MBWE with a web browser; for example remote shutdown or remote reboot would be really simple to implement...