HTTP authentication is quite useful to be added on your live site when you need only the authenticated people to have the access. Well, who are these “authenticated” people ? 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? The following is the command.
htpasswd [ -c ] passwdfilename username <enter>
So, here you have given the username that would be authenticated for the site. You will be asked twice to enter password for this username; do it.
For example, in the following command, my file's name is ".dummyhtpasswd" and the username of the account is "user1".
htpasswd –c /var/www/my_dummy_project/.dummyhtpasswd user1 <enter>
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 so that this newly created file's presence and the credentials are verified at every http access. For that you have to reach out to your .htaccess file. In my case, I found it in my project folder at /var/www/ my_dummy_project/ . You can also find it in the folder of your project.
Step 3: Now, 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).
References / Useful Links: