Go to TogaWare.com Home Page.
GNU/Linux Desktop Survival Guide
by Graham Williams
Google

Password Protect Web Files


You can do this either on a per directory basis in /etc/apache/httpd.conf or through the use of a .htaccess file in the directory containing the files to protect. The following is either added to a <Directory> directive in /etc/apache/httpd.conf or into .htaccess in the relevant directory.



  AuthUserFile /home/kayon/public_html/.htpasswd
  AuthGroupFile /dev/null
  AuthName ByPassword
  AuthType Basic
  Require user kayon

Then create the password file:



  $ cd public_html
  $ htpasswd -c .htpasswd kayon

You will be prompted for a password. This stores the encrypted password in the file .htpasswd (the -c option says to create the file). You can manually edit this file, keeping the same format, and perhaps even cutting the user passwords from /etc/shadow to maintain the same passwords.

Note that you should take precautions to protect the actual encrypted passwords if you copy them from the shadow file (otherwise they may be subject to attempts to crack them). For example:



  # chown www-data.www-data .htpasswd
  # chmod o= .htpasswd

I would like to figure out a way to use the system passwords file without making /etc/shadow accessible to everyone (which is precisely the point of the shadow file) and without having to copy the encrypted passwords into .htpasswd. Then I would define the particular user group (AuthGroupFile) as the users who can actually access the relevent web pages.

Protecting a cgi-bin directory through the use of the .htaccess file make sure you have in /etc/apache-ssl/httpd.conf:



AllowOverride AuthConfig

Normally this is set to None. That is, in /etc/apache-ssl/httpd.conf you should have something like:



  <Directory "/home/kayon/projects/wwp/cgi-bin">
        Options None
        AllowOverride AuthConfig
        Order deny,allow
        Deny from all
        Allow from togaware.com
  </Directory>

Copyright © 1995-2014 Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.