How to authenticate web access to perticular user?

Ans :

Step1 : Check if the apache package is installed or not.  If it’s not installed install it.

#rpm -qa | grep httpd

#yum install httpd

Step2 : Create DNS CNAME entry for this virtual host as which should point to our server name

Step2a : Create a home direcotry for our virtual host and index.html file

#mkdir /websites/auth

#vi /websites/auth/index.html

Step3 : Create and configure user names to whom we should give access to this site.  For this we have to create user name and passwords virtually.

Step3(a) : Create an empty file in /etc/httpd

#touch /etc/httpd/passwd

Step3(b) : Create usernames who require access to this site..

#htpasswd -mc /etc/httpd/passwd username

Then it will ask for passwd for this new user



Let me explain about two steps.

/etc/httpd/passwd file contains all the user names with encrypted passwords.

htpasswd is the command to create the new users

-m =>MD5 encryption

-c => Create a password file if it’s not there

Once the above users are create we have to create .htaccess file in /websites/auth folder.  So that our site will be accessed through authentication.

#vi /websites/auth/.htaccess

AuthName “Testing for Authentication”

AuthType Basic

AuthUserFile “/etc/httpd/passwd”

require valid-user

Save the file and exit

Step4 : Configure virtual host now in our httpd.conf file which is located at /etc/httpd/conf/

Step4(a) : Specify ServerName as in httpd.conf file


Step4(b) : Specify NameVirtualHost


Step4(c) : Create index.html in /websites/auth/ folder and edit something in that file

#vi /websites/auth/index.html

Save and exit the file

Step4(d) : Now create a virtual host entry.



<Directory /websites/auth>

AllowOverride Authconfig


DocumentRoot /websites/auth/

DirectoryIndex index.html


Let me explain new entries in this virtualhost

Directory /websites/auth => This indicate where should the authentication should start.  Its /websites/auth so when ever user tries to access this folder /websites/auth it should ask for username and password.

AllowOverride Authconfing => This specifies user require to enter username/password when entering this site.

Save and exit the file

Step5 : Check for the syntax errors in the httpd.conf file before restarting the apache service.

#httpd -t


#httpd -k graceful

Step6 : Now start the service and then add it to booting scripts so that it will start automatically at every boot of the system

#service httpd restart

#chkconfig httpd on

step7 : Now try to access you will be prompted to enter username and password.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.