Here, I explain how to provide securized access to a file (or folder) on an Apache server with a basic authentication.
Let's say we are on folder /srv/http
and we would like to restrain access to hello.txt
to user alice only.
cd /srv/http
touch hello.txt
htpasswd -c .htpasswd alice
New password: # Add password here.
Re-type new password: # Add password here again.
Adding password for user alice
Put this in a .htaccess
file in the same folder to configure the access.
<Files "hello.txt">
AuthName "This file needs a password"
AuthType Basic
AuthUserFile "/srv/http/.htpasswd"
Require valid-user
</Files>
Done!
Open the file in your browser and check if a prompt is shown to ask a user and password.