Set up Basic Authentication to limit access to specific web pages.
Before implementing Basic Authentication, ensure you're using a secure connection with SSL/TLS. Username and password are sent in plain text with Basic Authentication, so a secure connection is crucial.
Set Basic Authentication for a specific directory, for example, /var/www/html/auth-basic.
# Create and edit the Basic Authentication configuration
[root@www ~]# vi /etc/httpd/conf.d/auth_basic.conf
# Add the following configuration
<Directory /var/www/html/auth-basic>
SSLRequireSSL
AuthType Basic
AuthName "Basic Authentication"
AuthUserFile /etc/httpd/conf/.htpasswd
Require valid-user
</Directory>
# Add a user - create a new file with [-c]
[root@www ~]# htpasswd -c /etc/httpd/conf/.htpasswd cent
# Set and re-type the new password when prompted
# Create the directory and reload httpd
[root@www ~]# mkdir /var/www/html/auth-basic
[root@www ~]# systemctl reload httpd
# Create a test page for Basic Authentication
[root@www ~]# vi /var/www/html/auth-basic/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Test Page for Basic Authentication
</div>
</body>
</html>
Access the test page from any client computer with a web browser. Authentication will be required as per the settings.
http://[your-server's-domain-or-IP]/auth-basic/index.htmlThis setup ensures that access to the specified directory requires user authentication, enhancing the security of your web content.