CH3SNAS:Tutorials/lighttpd
From NAS-Tweaks
The lighttpd daemon is a lightweight web server. Its main task is to respond to HTTP requests. In other words, it allows HTTP clients (such as Internet Explorer and Firefox) to retrieve hyperlinked HTML pages stored on a web site stored on the CH3SNAS.
In terms of complexity, lighttpd lies between the basic GoAhead HTTP server (which comes with the CH3SNAS) and an Apache server.
Contents |
Example uses
The lighttpd server allows you to build a web site which is accessible via your local area network (LAN). If you configure your router's firewall properly, you can also let others access the website via the Internet. Opening your firewall to give access to outside HTTP requests obviously requires paying some attention to security.
A basic web site consists of a collection of HTML pages which provide text, links, images, documents, etc. HTML pages can be written with an ASCII editor. There are numerous books and online tutorials on how to create basic and advanced web pages directly in HTML. This can be worth trying if you want to understand how the World Wide Web works: HTML is the main technology behind WWW.
In recent years, however, the average web page has gotten increasingly sophisticated in terms of its technical and visual design. Furthermore, the content of many web sites has become dynamic: what you see in your browser often changes daily (the weather, the news, a blog, photos). Such dynamic HTML pages are generated (by running programs or scripts) whenever the set of HTML pages needs to be updated, or are created on the fly whenever the server receives a page request.
Some examples of what you can do with a web site consisting of basic, static HTML served via lighttpd:
- a basic home page telling who you are
- a set of pages about your hobby, your recipes, or reporting on your holiday trip (all with embedded pictures)
Using this approach you "program" your page using the HTML language.
Examples of ways to generate fancier static HTML pages using software:
- Microsoft Word can generate a HTML version of a document (including formatting, links and pictures)
- professional photographers may use Photoshop Lightroom to generate an online gallery of sample pictures
Using this approach you use software to generate the HTML pages. This means you don't need to learn (much) HTML, but instead need to learn how to use the (often fancy) software. All-in-all this can save you some typing and helps generate a uniform look for the web site.
Examples of applications involving generating dynamic HTML pages:
- a Blog or Forum contains HTML forms that allow users to type in text that is appended to certain pages. Example: Nas-Tweaks uses a PHP server and software by Simple Machines for its forum. Often this approach uses a database like mySQL to store and manage the raw data used to generate the HTML.
- many well-known Web 2.0 sites that provide a service for you (WikiPedia, Google Maps, YouTube, Flickr)
Using using this approach, software generates HTML pages on demand, keeps the pages updated, and allows interaction with the user. If you go overboard on this, and manage to get the users of your website to provide content which keeps you web site interesting, you can call this Web 2.0 and tell your friends you may become rich - someday. This kind of software is often known as Content Management Systems and includes engines like Drupal, Joomla, Mediawiki (as used by Wikipedia) and Wordpress (as used for blogs).
Setting up Lighttpd
Directories
The default lighttpd configuration needs a small directory tree below the top directory of the Volume_1 share. You can make these folders using Samba or on the ssh command line:
mkdir /mnt/HD_a2/www mkdir /mnt/HD_a2/www/pages mkdir /mnt/HD_a2/www/logs
The folder /mnt/HD_a2/www/pages is the default location for the web site itself. The location is set via a parameter called server.document-root in the /ffp/etc/lighttpd.conf file. This means that any directories below server.document-root are visible via the HTTP server (careful), while any other directories are not visible.
The folder /mnt/HD_a2/www/logs is for log files in which lighttpd can record what happened.
HTML test pages
Within the www folder, you should create a home page named index.html containing the following ASCII text. Note that the indentation is only for readability and can be (and often is) omitted. You don't need to worry about using Linux or Windows end-of-line conventions here (both work). You may have to adapt the line CH3SNAS:81 to reflect the network name of your NAS or use its IP address instead (e.g. 192.168.0.20:81).
<html><head><title>Hello world</title>
<style type="text/css"><!--h1 {text-align:center;font-family:Arial, Helvetica, Sans-Serif;}p {text-indent:20px;}--></style></head><body bgcolor="#ffffcc" text="#0000ff"><h1>Hello, CH3SNAS Tweak'n World!</h1><p>
<A HREF="page1.html">Link to local page1</A><p>
<A HREF="page2.html">Link to local page2</A><p>
<A HREF="http://CH3SNAS:81">Configure the CH3SNAS</A><p>
<A HREF="http://nas-tweaks.net/CH3SNAS:Tutorials/lighttpd">external link to the Lighttpd tutorial</A>
</body></html>
The HTML code consists of a head and a body section. The head contains the title (shown at the top of the browser window) and, in this case, defines formatting styles for Header1 and new paragraph (<p>).
The body starts with a header, followed by four links ("<A>nchors") to other pages, to the original HTML configuration page for the CH3SNAS (on port 81 instead of the default 80) and a link to a remote web site.
Then create page1.html in the same directory containing:
<html><head><title>Page #1</title>
<style type="text/css"><!--h1 {text-align:center;font-family:Arial, Helvetica, Sans-Serif;}p {text-indent:20px;}--></style></head><body bgcolor = "#ffffcc" text = "#0000ff"><h1>Page 1</h1><p>
<A HREF="index.html">Home</A><p>
<A HREF="page2.html">Link to page2</A><p>
</body></html>
Similarly you can create page2.html in the same directory:
<html><head><title>Page #2</title>
<style type="text/css"><!--h1 {text-align:center;font-family:Arial, Helvetica, Sans-Serif;}p {text-indent:20px;}--></style></head><body bgcolor = "#ffffcc" text = "#0000ff"><h1>Page 2</h1><p>
<A HREF="index.html">Home</A><p>
<A HREF="page1.html">Link to page1</A><p>
</body></html>
Enabling lighttpd
This tutorial assumes that fun_plug is installed and that you have installed the fun_plug packages (these include lighttpd). You can test whether lighttpd is installed using
which lighttpd
on the ssh command line. If it is installed, it will respond with the location of the lighttpd script file: /ffp/sbin/lighttpd.
Waking up daemons
Next we need to enable the lighttpd server from the command line (see the packages tutorial for a detailed explanation):
chmod a+x lighttpd.sh
and enable a script which disables the standard HTML server called webs:
chmod a+x kickwebs.sh
The standard "webs" server then restarts automatically within a few minutes, but moves to port 81 because the standard HTTP port (80) is already occupied by the lighttpd server. Note that the chmod commands only take effect on the next reboot. If you would reboot now, however, nothing changes. If you want to try, you can see test this manually using:
cd /ffp/start sh kickwebs.sh start sh lighttpd.sh start
Unless you have installed the server before, you will get the error message /ffp/etc/lighttpd.conf: Required file not found or not readable. Which, by Linux standards is an unusually clear error message: a file with configuration settings for lighttpd is missing in the /ffp/etc directory (see the packages tutorial for more explanation).
Lighttpd.conf
We can resolve the missing /ffp/etc/lighttpd.conf file with:
cd /ffp/etc cp examples/lighttpd.conf .
This copies one of the two supplied configuration files from the /ffp/etc/examples subdirectory to the /ffp/etc (or ".") directory.
Port insight
Next we can try starting lighttpd manually again to see how we are doing:
cd /ffp/start sh lighttpd.sh start sh lighttpd.sh status
As, this suggests that lighttpd is now happily running, we try typing http://CH3SNAS:81 in the address bar of your Web browser. Note that if your network name for the device differs from CH3SNAS, use that name or its IP address instead. You should now get the standard CH3SNAS configuration screen. Please remember the :81 port number in case you need to access it in the future (it is one more than the standard port 80 used for HTTP servers - which you can look up on the internet). You can close that screen - it was only to demonstrate that kickwebs.sh did its job.
We now try entering http://CH3SNAS:8080 in the address bar of the Web browser. If all goes well, you will see the expected web page (see Image:Picture). Before getting lost in the rich abundance of this entertaining and enlightening web site, note that port 8080 is not really the right port number for a HTTP server. So now open the file /ffp/etc/lighttpd.conf using a Linux-friendly ASCII editor (e.g. nano or NotePad++) and carefully find the text lines
## bind to port (default: 80) server.port = 8080
and replace them with
## bind to port (default: 80) #server.port = 8080
which turns the second line into a comment, thus causing the default value of 80 to be used, or more directly:
## bind to port (default: 80) server.port = 80
Now save (nano's: Ctrl-O writeOut command) and exit the editor.
To boot or not to boot
Remember that you now have a lighttpd running on port 8080 (yes, it was running on port 8080, and it still is running). So you can fix this by rebooting the CH3SNAS. This shuts down the server and lets the fun_plug script start it up under port 80 (using the edited lighttpd.conf file). Tip: you can reboot the CH3SNAS from an ssh shell (via PuTTY) using the reboot command (don't be suprised if the terminal window compains about a lost connection during the rebooting). Alternatively, you can use your web browser (port 81 remember!) or the button on the CH3SNAS if you prefer a directer approach.
If you want to get lighttpd to run on port 80 without rebooting, you can also use:
cd /ffp/start sh lighttpd.sh restart sh lighttpd.sh status
Testing Lighttpd
At this point, you can enter any of the following into your browser's address bar (assuming CH3SNAS is the correct network name of your NAS):
-
http://CH3SNAS:80/index.html -
http://CH3SNAS:80- index.html is a default for browsers -
http://CH3SNAS- port 80 is default for HTTP -
CH3SNAS- http is the default protocol for web browsers
Notes
The log files
The directory /mnt/HD_a2/www/logs contain log files which respectively record the access to the server and any errors (including starting and stopping) reported by the server.
-rw-r--r-- 1 root root 0 Oct 19 17:06 access.log -rw-r--r-- 1 root root 48 Oct 19 17:06 error.log
The file access.log contains timestamped records which are appended whenever a file on the server is accessed via HTTP (which file, which IP address, HTTP statuscode, browser used). Here is a schematic line from the access.log file:
client_IP server_URL - [date:time +0200] "GET /path/file_1.jpg HTTP/1.1" status_code file_length "-" "client_browser_type"
If you want to clear the log files, one quick-and-dirty way to do so is to delete them and restart the CH3SNAS. The lighttpd server will regenerate the log files when it is started. A quicker approach is to restart the lighttpd server using:
rm /mnt/HD_a2/www/logs/access.log cd /ffp/start sh lighttpd.sh restart


