Raspberry Pi Uncategorized

Password-protect A lighttpd Web Server on a Raspberry Pi Using mod_auth

Password protect a lighttpd Web server.

Requirements For This Walkthrough



Set Up A lighttpd Web Server on the Raspberry Pi

If you haven’t already, set up a Web Server on the Raspberry Pi.

Create A Password File

This file can go anywhere, but for simplicity in this walkthrough, we’ll store it in /etc/lighttpd/.htpasswd .

Create the hideen .htpasswd  directory:

sudo mkdir /etc/lighttpd/.htpasswd

Next, create a script that will hash a user’s password and put everything in the right format for the file.  Make a new file called  (or whatever you want) with the following content:

hash=`echo -n "$user:$realm:$pass" | md5sum | cut -b -32`
echo "$user:$realm:$hash"

Make the file executable:

chmod 755 ./

Add A User to the Password File

Run the script with three arguments: a user, a realm, and a password (which will get hashed).

./ 'username' 'myrealm' 'password'

The output will look like this:


Create the the password file and paste in the output of the command above:

sudo nano /etc/lighttpd/.htpasswd/lighttpd-htdigest.user

Enable mod_auth

Add the following to the /etc/lighttpd/lighttpd.conf  (highlighted lines).  The directory you want to password protect will go on this line: auth.require = ( “/Directory/To/Protect/” =>

server.modules = (
#       "mod_rewrite",

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"             = "/var/run/"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80

index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/ " + server.port
include_shell "/usr/share/lighttpd/"
include_shell "/usr/share/lighttpd/"

auth.backend = "htdigest"                                                          
auth.backend.htdigest.userfile = "/etc/lighttpd/.htpasswd/lighttpd-htdigest.user" 
auth.require = ( "/path/to/protect/" =>
    "method"  => "digest",
    "realm"   => "myrealm",
    "require" => "valid-user"

Restart the Web Server

Restart lighttpd to apply all the changes:

sudo service lighttpd restart

Your site should now prompt for a password whenever you navigate to the directory you chose to password-protect.

11 replies on “Password-protect A lighttpd Web Server on a Raspberry Pi Using mod_auth”

Hey Jacob, I’ve really enjoyed you work!

Having some trouble getting this working on my Pi-Hole. Have followed your steps. but it won’t prompt for UN/PW.

the only difference I can see in the .conf is the root-document “var/www/html”

My goal is to protect the entire admin console for the pi-hole, any help would be much appreciated.

Did you restart lighttpd after making the changes? I haven’t tried this on the Pi-hole, but I’ll see if I can replicate the issue.

Hey Jacob, I figured this out. in the lighted.conf it needs to have the password requirements underneath the section about the admin page. So under the index page if that makes sense.

Good article.
Unfortunately this wouldn’t work for me.

When i ever execute this command “./ ‘username’ ‘myrealm’ ‘password'”
I get an output of “-bash: ./ /bin/sh^M: bad interpreter: No such file or directory”

Any ideas?

Very clear tutorial, but I’m not prompted for any password when I try to access the directory. I restarted the lighttpd server. Should the “realm” and “require” values stay “myrealm” and “valid-user” or should I change those to match something on my system?

Can someone please explain to me what “realm” is and how I determine what realm to use? I want to protect the entire website with access for only one user.

Full path /var/www/html
File /var/www/html/index.html
Sub /var/www/html/admin (pi-hole)


Leave a Reply to jacob.salmela Cancel reply