Adding HTTP authentication to your site


HTTP authentication is quite useful to be added on your live site when you need only the authenticated people to have the access and who these “authenticated” people are? These are the people who have the username / password for the http authentication. Let’s see how it is done.

Step 1: You need to create the file that will keep the information of the usernames / passwords for authentication. (Yes you can create more than one accounts, you will see that soon). You can create this file in the folder of your project. In my case it is at the location /var/www/ my_dummy_project/ . How to create it?

htpasswd [ -c ] passwdfilename username <enter>

Then you will be asked to enter password twice. Do it. So, the account creation is done.
The below example should make it clear.

htpasswd –c /var/www/my_dummy_project/.dummyhtpasswd user1 <enter>

Here my username is user1.
The useful link is here..

Step 2: You are done with creation of account details, now you need to edit your .htaccess file to add the code for http authentication there and also to add the path of this newly created file that contains the credentials’ information. For that you have to reach out to your .htaccess file. In my case, I found it at /var/www/ my_dummy_project/ . You can also find it in the folder of your project.

Step 3: Edit the .htaccess file. How to do it?
The command is vi .htaccess <enter>. 
In general for opening any file, it is 
vi <filename> <enter>.

Now to go to edit mode, press “i”, that’s for “insert”. Take the cursor to the very bottom of the code and then add the following lines.

1 # /var/www/my_dummy_project/.dummyhtpasswd
2 #     AUTHENTICATION
3 ## BASIC PASSWORD AUTHENTICATION
4 AuthName “Prompt”
5 AuthUserFile /var/www/my_dummy_project/.dummyhtpasswd
6 AuthType basic
7 Require valid-user

That is it. Now we have to save it. Press “Esc” and then :w <enter>. This saves it. 
For saving and exiting press “Esc” and then press :wq! <enter>.

Now we have added the path of the newly created file. Please see that line 1 and 5 of the code would have the path of “your” created file, in above code I have given the path of “my” created file for reference.

*******ADDING MORE THAN ONE USERS*******

Simple! So we are done with adding the http authentication to your site with an account whose username / password you know. Now we have to add more than one such accounts, so the steps continue.. ;)

Step 4:  You have to reach out to the newly created file. (You created in Step1). After that the following command can be used to add a user to the already existing file.

htpasswd /var/www/ my_dummy_project/.dummyhtpasswd  newuser <enter>

Enter the password twice and you are done. The path in above command will be the path for that newly created password file (of Step1).

You can use the above command and as many users as you need. You may use reference here..
Done!! J

Comments