SHTTPD as a light web server

Introduction

Simple HTTPd, a.k.a. shttpd, is a single-EXE web server available for Windows and Linux that can not only hand out static files but also run scripts through CGI. It also supports Apache's digest mechanism to protect directories with an .htpasswd file.

Setup

Release 1.38 on Windows was only GUI based so ignored command-line arguments, but 1.39 and above do read command-line arguments and they supersed those set in shttpd.conf.

Since 1.38 didn't use command-line arguments, it also didn't suppor the -A switch which made it possible to create user accounts in an .htpasswd file. As a result, we had to use Apache's htdigest.exe for this. Starting with 1.39, this feature is also available in the Windows version of Shttpd, so htdigest.exe is no longer required.

Tips

Which files do I need for PHP5 and SQLite3/PDO_SQLite?

In addition to shttpd.exe and shttpd.conf:

Here's part of the php.ini:

[PHP]
extension_dir = ".\"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll

Creating configuration file on the fly before launching Shttpd

Since Shttpd expect document_root and cgi_interpreter to contain the full path to the items, if we want to provide users with a ZIP file that they can just unzip to have a running web app, a work-around is to write an applet that they'll double-click to create the shttpd.conf on the fly, launch Shttpd, and die silently.

Here's the PowerBasic version:

#COMPILE EXE
 
FUNCTION PBMAIN() AS LONG
    DIM config_file AS INTEGER
 
    config_file = FREEFILE
    OPEN "shttpd.conf" FOR OUTPUT AS #config_file
    PRINT# config_file, "document_root " & CURDIR$
    PRINT# config_file, "listen_ports 6666"
    PRINT# config_file, "list_directories 1"
    PRINT# config_file, "cgi_interpreter " & CURDIR$ & "\php-cgi.exe"
    PRINT# config_file, "auto_start 1"
    CLOSE config_file
 
    PID& = SHELL("shttpd.exe",1)
END FUNCTION

.htpasswd

The .htpasswd file must include the "realm", or SHTTPD will just loop endlessly when you're trying to access a protected resource:

admin:localhost:413bc7f363027891e6f4749eada79a73

If the version of SHTTPD that you have doesn't handle the -A switch to create/edit passwords, download Apache's htdigest.exe:

C:\>htdigest.exe -c .htpasswd localhost admin

shttpd.conf

The path to the CGI interpreter must be absolute, not relative

Resources