How to setup a password authentication for apache2 in centos ?
Introduction:
[edit]Securing your Apache web server with password authentication is crucial for protecting sensitive content or restricting access to specific areas of your website. In this guide, we'll walk you through the steps to set up password authentication for Apache2 on CentOS, ensuring that only authorized users can access your website.
Prerequisites:
[edit]Before you begin, make sure you have the following:
1. Access to a CentOS server with Apache2 installed.
2. Basic knowledge of working with the Linux command line.
3. Administrative privileges (sudo access) on the server.
Steps to Set Up Password Authentication for Apache2 on CentOS:
[edit]Check Apache2 Installation: Log in to your CentOS server via SSH and verify if Apache2 is already installed. If not, install it using the yum install httpd command.
Restart Apache2 Service: After installing Apache2, start the service manually using systemctl restart httpd.service or service httpd restart.
Add New User: Use the htpasswd command to add a new user for HTTP authentication. For the first user, include the -c option to create the authentication file. Subsequent users can be added without the -c option. Example: sudo htpasswd -c /etc/httpd/.htpasswd username
Configure Apache Virtual Host: Edit the Apache virtual host configuration file for your domain (typically located in /etc/httpd/conf.d). Add the necessary authentication directives within the <Directory> block. Example configuration:
Explain
<VirtualHost *:80>
ServerName www.example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/example
ErrorLog /var/www/html/example/error.log
CustomLog /var/www/html/example/access.log combined
<Directory "/var/www/html/example">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory> </VirtualHost>
Create .htaccess File: In your website's document root directory, create a .htaccess file if it doesn't already exist. Add the necessary directives to enforce authentication. Example directives:
AuthName " Restricted Area"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
Restart Apache: After making configuration changes, restart the Apache service to apply the changes:
systemctl restart httpd.service
Congrats, that's all now you can able to see the authentication of your httpd or apache on your browser like this.
After entering your username and password you can able to go the apache.
Here we have routed the file like the user to do this you can go the file in the location /etc/hosts open this hosts file and make the changes like
youripaddress domainname
xxxxxxxxxxxx www.kumaran.com
That's all after you hit the browser with the domain name it will redirect you to that page.
Conclusion:
By following these steps, you have successfully set up password authentication for Apache2 on your CentOS server. This additional layer of security helps protect your website's sensitive content and restrict access to authorized users only. Remember to regularly review and update your password authentication settings to maintain the security of your web server.
