install htpasswd (password generator)
install apache2-utils through
sudo apt-get install apache2-utils
This above package has htpasswd command.
configure basic authentication
go to apache2.conf file and edit
change "AllowOverride none" to "AllowOverride All" to .htaccess enable and working in the directory.
add a new line below the above edit lines "AllowOverride AuthConfig"
to enable apache2 to handle authentication. keep in mind these modules should be enables
- authn_core_module
- authz_core_module
you can check these modules (enable or not) through this command
sudo apache2ctl -M
create password file(create new user and password)
create your folder in /var/www/html/
sudo mkdir -m /var/www/html/my_protected_folder
htpasswd -c /var/www/html/my_protected_folder/.htpassword username
and enter prompted password. .htpassword file created with username and encrypted password.
- crate .htaccess file with these content
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /var/www/html/my_protected_folder/.htpassword
Require user username
change permission and ownership
sudo chown -R www-data:www-data /var/www/html/my_protected_folder
sudo chmod 660 /var/www/html/my_protected_folder/.htpassword
visit your page and enter username "username" and password which you wrote.