Password protection for a specific directory on the Apache web server can be configured by using a `.htaccess` file. Here is how you can do it:
1. Create a password file: The first step in the process is to create a text file, which stores the usernames and their corresponding passwords.
This text file should ideally not be stored in a public accessible directory. The Apache `htpasswd` tool is used for this purpose. For example, if you want to create a user called ‘admin’, you can use this command: \`\`\`bash htpasswd -c /etc/apache2/.htpasswd admin \`\`\` You’ll be prompted to supply and confirm your password for the user.1. Setting up `.htaccess` file: Next you’ll need to set the configuration in a `.htaccess` file in your directory. Here’s an example:
\`\`\`bash AuthType Basic AuthName “Restricted Content“ AuthUserFile /etc/apache2/.htpasswd Require valid-user \`\`\` In this example, `AuthType Basic` refers to the type of authentication being used, `AuthName` provides a message for the authentication dialogue, `AuthUserFile` specifies the location of the password file, and `Require valid-user` allows any valid user to access the directory.1. Configure Apache to Allow .htaccess Overrides: Go to Apache configuration file `apache2.conf` or `httpd.conf` in `/etc/apache2/`:
\`\`\`bash nano /etc/apache2/apache2.conf \`\`\` Then, look for a section having your directory information – something like this: \`\`\`bash1. Restart Apache: Once all the configurations are done, remember to restart the Apache2 service so that the changes can take effect.
\`\`\`bash systemctl restart apache2 \`\`\`After these steps, when you try to access the given directory in a browser, it should ask for a username and password.