If you’re using Linux Distribution like Ubuntu, make sure u have installed apache2 and apache2-utils. Then create new user with following command
sudo htpasswd -c /etc/apache2/.htpasswd new_user
And if you want to create another user just leave -c
flag so the command will look like this
sudo htpasswd /etc/apache2/.htpasswd second_new_user
In both cases, you’ll be prompted to enter password for each user.
However, this method is for your global apache2 configuration. Same process can be repeated for only one website. Just make sure, that you have .htaccess
file in your project and then repeat the command. Don’t forget to change path so the command will look like this.
sudo htpasswd -c /var/www/my_website/.htpasswd new_user
Enter the password and then configure .htaccess
like this
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /var/www/my_website/.htpasswd
Require valid-user
If you stick to global apache 2 authentication, then point the change the third line to AuthUserFile /etc/apache2/.htpasswd