1. Create a .htpasswd file
First, you need to create a .htpasswd file. This file will store your username(s) and password(s). To create this file, you can use a tool like htpasswd. Here’s the command to use: \`\`\`bash htpasswd -c /path/to/.htpasswd username \`\`\` The `-c` flag tells htpasswd to create a new file. `/path/to/.htpasswd` is the full file path where you want the .htpasswd file to be saved, and `username` is the username you want to create. You’ll be prompted to enter a password.1. Set up .htaccess
Once the .htpasswd file is created, you’ll need to set up your .htaccess file. This file tells Apache to secure a specific directory. Add the following lines in your .htaccess file: \`\`\`bash AuthType Basic AuthName “Restricted Content“ AuthUserFile /path/to/.htpasswd Require valid-user \`\`\` `AuthType` indicates the type of authentication that is being used. `AuthName` is the message for the login prompt. `AuthUserFile` indicates the path of the .htpasswd file. `Require valid-user` tells Apache that only authenticated users can access this directory.1. Change .htaccess and .htpasswd permission
For security reason, you have to change the .htaccess and .htpasswd files permissions. It will make files inaccessible from the outer world. \`\`\`bash chmod 644 .htaccess chmod 644 .htpasswd \`\`\`1. Check Apache Configuration
Apache needs to be correctly configured to read .htaccess file. Check that the Apache configuration file (usually called httpd.conf) has the following lines : \`\`\`bash AccessFileName .htaccess \`\`\` And \`\`\`bash AllowOverride All \`\`\` If not, you should add them and restart Apache.1. Test
Finally, try to access the secured folder via web browser. It should prompt you for a username and password before allowing access.Remember that the .htaccess and .htpasswd files are very powerful and sensitive, and a small mistake can cause big problems, so handle with care.