You can configure basic authentication in Apache by following these steps:
1. Firstly, make sure your Apache server is installed with the appropriate module `mod_auth_basic`. This is usually included by default.
1. Enable authentication by defining it in your directory settings in your Apache configuration file (httpd.conf), which is usually located in /etc/httpd/httpd.conf or /etc/apache2/apache2.conf.
Here’s how you do it:
```
```
1. Here is what those directives do:
- AuthType: This sets the type of authentication that will be used. In this scenario, we will be implementing basic authentication.
- AuthName: This text is displayed on the login dialog users will see when attempting to access the site.
- AuthUserFile: This sets the path to the password file that will be used for authentication.
- Require: This specifies that only authenticated users will be allowed access.
1. Now you need to create the .htpasswd file (AuthUserFile) mentioned in the configuration above. You can create it anywhere, but it is recommended to not to keep inside the web-accessible folders. Let’s create a user named `user1`.
```
htpasswd -c /etc/httpd/.htpasswd user1
```
It will ask for the password. Provide a suitable password. This will create the user and the .htpasswd file itself.
1. Once done, restart the Apache service for changes to take effect:
```
systemctl restart httpd
```
(or `service apache2 restart`)
By following these steps, when trying to access your website, the user will be prompted to enter their credentials. If the username and password entered match what’s inside the .htpasswd file, they will be able to access the website. If not, access will be denied.
Please replace “/var/www/html/your-directory”, “/etc/httpd/.htpasswd”, “user1”, and your paths according to your configuration.