NSLU2-Linux
view · edit · print · history

Goal:

Compile a C program to make a CGI executable for thttpd - all on the NSLU2.

Prerequisites:

Installation of Unslung.
Successful completion of NativeNSLU2Toolchain, which provides a development environment on your NSLU2.

Steps:

Note: The locations used in this example may vary for your installation.

1) Create the C source file, webhello.c, using the editor of your choice. Here's the code:

    #include <stdio.h>
    #include <time.h>

    int main()
    {
        time_t tim = time(NULL);

        printf("Content-type: text/html\n"   /* Necessary to specify the type */
	       "\n"                          /* This blank line is critical! */
	       "<html>\n"
	       "<body>\n"
	       "Hello, World!<br>\n");       /* Do the hello thing... */

        /* Print out the current time */
        printf("The time is %s<br>\n", asctime(localtime(&tim)) );

	printf("</body>\n"
	       "</html>\n");

	return 0;
    } 

2) Compile the C source file:

    gcc webhello.c -o webhello.cgi
    strip webhello.cgi

3) Move the executable into the web hierarchy:

    mv webhello.cgi /home/www/

4) Identify the CGI pattern to thttpd. This can be done two ways:

    On the command line:

        /usr/bin/thttpd -c '**.cgi'

    Or, in the config file as:

        cgipat=**.cgi

5) Access the CGI file through a browser using:

     http://www.ByteRed.com/webhello.cgi

That's it!

6) Final Note:

For simple CGI scripts, you can use a shell script like following:

 #!/bin/ash
 echo "Content-type: text/plain"
 echo ""
 echo "Hello World!"
 set

Don't forget to chmod +x scriptname.cgi
(Tested with Linksys R29 Firmware? - the webserver pages are at /home/httpd/html/Management/ in the ramdisk - so it shall work on Unslung.HomePage, too.)

view · edit · print · history · Last edited by mwester.
Based on work by RXPKW, MK, UXAOX, fixer, ZluZm, tino, tman, and michaelebrownmindspringcom.
Originally by michaelebrownmindspringcom.
Page last modified on May 09, 2008, at 01:25 AM