Password protect website with Apache

This is useful before making the website live or for demo purposes. The best method for not letting web crawlers index your half-baked pages is to put those behind a password. You can password protect individual pages within WordPress but to make this happen across the website you need to do it at the Apache level.

sudo /opt/bitnami/apache2/bin/htpasswd -cb /opt/bitnami/apache2/wordpress_users sampleusername samplepassword
  • Edit the /opt/bitnami/apache2/conf/vhosts/wordpress-vhost.conf file to add the following lines:
<VirtualHost 127.0.0.1:443 _default_:443>
    ...
    <Directory "/opt/bitnami/APPNAME">
      ...
      AuthType Basic
      AuthName MyAuthName
      AuthUserFile "/opt/bitnami/apache2/wordpress_users"
      Require valid-user

In the same file comment out Require all granted

# Require all granted
  • Edit the /opt/bitnami/apache2/conf/vhosts/wordpress-https-vhost.conf file to add the following lines and in same file comment out Require all granted.
<VirtualHost 127.0.0.1:443 _default_:443>
    ...
    <Directory "/opt/bitnami/APPNAME">
      ...
      AuthType Basic
      AuthName MyAuthName
      AuthUserFile "/opt/bitnami/apache2/wordpress_users"
      Require valid-user
# Require all granted
  • Restart the Apache server
sudo /opt/bitnami/ctlscript.sh restart apache

Now when you access the website in the browser it will ask for a username and password. You will see a screen like this

how to password protect website with Apache

About Author

Sorry, Comments are closed!