Prerequisite: Make sure .htaccess overrides are allowed for the folder you are protecting!

In the folder being protected create two files:

.htaccess:\

AuthUserFile /path/to/file/.htpasswd
AuthType Basic
AuthName "Secured Access"
Require valid-user

or if you need to protect just specific file, change the last part of .htaccess to read like:\

  Require valid-user

For the creation of .htpasswd, in the corresponding folder run:\

htpasswd .htpasswd Johnnie

To change password in an existing .htpasswd for user Jannie:\

htpasswd .htpasswd Jannie