Table of Contents

,

Password protect site with htaccess in Apache

Create the Password File

  apt-get install apache2-utils
  htpasswd -c /etc/apache2/.htpasswd <username>

In vhost conf add:

  AuthType Basic
  AuthName "Restricted Content"
  AuthUserFile /etc/apache2/.htpasswd
  Require valid-user

Example protecting the directory:

  <Directory /var/www/example>
          AllowOverride All
          Options FollowSymlinks
          AuthType Basic
          AuthName "Restricted Content"
          AuthUserFile /etc/apache2/.htpasswd
          Require valid-user
  </Directory>
  

Example protecting the URL:

  <Location /admin>
      AuthUserFile /var/www/htpasswd/.htpasswd
      AuthName "Password Protected Area"
      AuthType Basic
      Require valid-user
  </Location>

Do it for both non-ssl and ssl vhost configuration

Check conf and reload web server

apachectl -t
systemctl reload apache2

See also

References