December 14, 2014

December 14, 2014
In this article, we are going o discuss about how to add HTTp authentication to the Drupal website. HTTP authentication is quite useful to be added on your live site when you need only the authenticated people to have the access.

Step 1 :

Create the file that will keep the information of the usernames / passwords for authentication. (You can create more than one accounts). You can create this file in the project root folder. Like, the location /var/www/drupal_site/. Use the following command.

htpasswd [ -c ] passwdfilename username 

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.

For example, in the following command, my file's name is ".dummyhtpasswd" and the username of the account is "user1".

htpasswd –c /var/www/drupal_site/.dummyhtpasswd user1

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/drupal_site/ . You can also find it in the folder of your project.

Step 3:

Now, edit the .htaccess file. By using the below command.

vi .htaccess

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.

# /var/www/drupal_site/.dummyhtpasswd
# AUTHENTICATION
## BASIC PASSWORD AUTHENTICATION
AuthName "Prompt"
AuthUserFile /var/www/drupal_site/.dummyhtpasswd
AuthType basic
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.

Step 4:  

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

htpasswd /var/www/ drupal_site/.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 Step 1).

Done!!

0 comments:

Post a Comment