Step 1: create the password file
Use the htpasswd generator for Apache to create a line for each user, or run the command on your server:
htpasswd -cB /home/site/.htpasswd adminUse -c only for the first user, because it creates the file and overwrites any existing one.
Step 2: store it safely
Keep the file outside the public web folder, and note its full path.
Step 3: add the .htaccess rules
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/site/.htpasswd
Require valid-userSave this as .htaccess inside the folder you want to protect.
Step 4: allow overrides
Apache must be allowed to read authentication rules from .htaccess. In the server configuration, the folder needs AllowOverride AuthConfig, or AllowOverride All.
Step 5: test
Open the folder in a private browser window. You should see a login box. If you get a 500 error, check the AuthUserFile path first.
Use HTTPS
Basic authentication does not encrypt credentials. Serve the protected area over HTTPS so they are protected in transit. For the strongest stored hashes, choose bcrypt with the bcrypt generator.
Create your password file. Generate bcrypt, APR1 or SHA-1 entries in your browser, with the matching Apache config.