95.11 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

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>


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0