{"id":18260,"date":"2015-03-19T11:59:10","date_gmt":"2015-03-19T06:29:10","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18260"},"modified":"2015-03-23T23:50:48","modified_gmt":"2015-03-23T18:20:48","slug":"http-authentication-using-apache-web-server","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/http-authentication-using-apache-web-server\/","title":{"rendered":"HTTP Authentication using Apache Web Server"},"content":{"rendered":"<p><strong>HTTP Authentication using Apache Web Server\u00a0<\/strong>is used to give password protected access to the files or folders present in your web server. Using it, you can also decide to whom you want to give the access.\u00a0If you have information on your web site that is sensitive or intended for only a small group of people, the techniques in this blog\u00a0will help you make sure that the people that see those pages are the people that you wanted to see them.<\/br><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Prerequisites<\/h3>\n<p><\/br><br \/>\nYou can put HTTP authentication in two ways:<\/p>\n<ol>\n<li>Using Directives directly in the apache2.conf file.<\/li>\n<li>Using Directives in .htaccess file. This file is stored in the folder to which you want to give restricted access.<\/li>\n<\/ol>\n<p><\/br><br \/>\nIf you plan to use .htaccess files, you will need to have a server configuration that permits putting authentication directives in these files.Since we&#8217;re talking here about authentication, you will need an <em>AllowOverride<\/em> directive like the following:<\/p>\n<div class=\"example\">\n<p>[js]AllowOverride AuthConfig[\/js]<\/p>\n<\/div>\n<p><\/br><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Creating Password File<\/h3>\n<p><\/br><br \/>\nYou need to create a password file which will store the information about user and password. Whenever someone needs access to a file or folder which is restricted, the user and password is searched in this file for granting the access. The password file should be restricted from the user . For instance, if the default root is \/var\/www\/html then the password should be present in \/var\/www\/password.<\/p>\n<p>To create the password we will use the command given below. The command will generate a password for a particular user and save it in the password file<\/p>\n<p>[js]htpasswd -c \/var\/www\/password\/passwords\u00a0mohit[\/js]<\/p>\n<p>This command is creating a file passwords for user\u00a0<strong>mohit\u00a0<\/strong>. &#8216;<strong>-c&#8217;\u00a0<\/strong>is used to create a file. This file will store the password information for user mohit. After typing the above command, it will ask to enter the password twice as shown below:<\/p>\n<p>[js]htpasswd -c \/var\/www\/password\/passwords\u00a0mohit<br \/>\nNew password: mypassword<br \/>\nRe-type new password: mypassword<br \/>\nAdding password for user mohit[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Protecting a Folder<\/h3>\n<p><\/br><br \/>\nConsider protecting a folder <strong>var\/www\/secret .\u00a0<\/strong>You can protect it either using .htaccess file or editing the main configuration file of apache. \u00a0Add the following lines in the configuration file or create .htaccess file in the secret folder:<\/p>\n<p>[js]AuthType Basic<br \/>\nAuthName &quot;Restricted Files&quot;<br \/>\n# (Following line optional)<br \/>\nAuthBasicProvider file<br \/>\nAuthUserFile \/var\/www\/password\/passwords<br \/>\nRequire user mohit[\/js]<\/br><\/p>\n<p>Here\u00a0<strong>Basic\u00a0<\/strong>is the Authentication type.\u00a0<strong>Restricted files\u00a0<\/strong>is the name given to the prompt.\u00a0<strong>file\u00a0<\/strong>is the authentication medium.\u00a0<strong>AuthUserFile\u00a0<\/strong>is the file in which credentials are stored.\u00a0<strong><strong>Require user\u00a0<\/strong><\/strong>is the user who needs to be authenticated.<\/p>\n<p>After adding the above lines, restart the apache server and you are done. Now whenever you will try to access the directory var\/www\/secret , it will prompt for user and password.<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Allowing access for a single IP and restricting access from others<\/h3>\n<p><\/br><br \/>\n[js]Directory \/var\/www\/secret&amp;gt;<br \/>\nAuthType Basic<br \/>\nAuthName &quot;Restricted Files&quot;<br \/>\n# (Following line optional)<br \/>\nAuthBasicProvider file<br \/>\nAuthUserFile \/var\/www\/password\/passwords<br \/>\nRequire user mohit<br \/>\nOrder deny,allow<br \/>\nDeny from all<br \/>\nAllow from\u00a010.1.1.118<br \/>\nSatisfy any<br \/>\nRequire ip 10.1.1.118<br \/>\n[\/js]<br \/>\n&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Giving access to more than one user<\/h3>\n<p><\/br><br \/>\nCreate a group for the users whom you want to give the access. Suppose we want to create a group named GroupName and want to give access to the users in GroupName. The password of these users must also be added to the password file.<\/p>\n<p>[js]GroupName: rbowen dpitts sungo rshersey<br \/>\nAuthType Basic<br \/>\nAuthName &quot;By Invitation Only&quot;<br \/>\n# Optional line:<br \/>\nAuthBasicProvider file<br \/>\nAuthUserFile \/var\/www\/password\/passwords<br \/>\nAuthGroupFile \/var\/www\/groups<br \/>\nRequire group GroupName[\/js]<\/br><\/p>\n<p>Instead of creating a group, you can also add users to the password file and add\u00a0<strong>Require valid-user\u00a0<\/strong>. It will give access to all the users who are listed in the password file.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTTP Authentication using Apache Web Server\u00a0is used to give password protected access to the files or folders present in your web server. Using it, you can also decide to whom you want to give the access.\u00a0If you have information on your web site that is sensitive or intended for only a small group of people, [&hellip;]<\/p>\n","protected":false},"author":163,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1174],"tags":[1716,1337,1717],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18260"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/163"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18260"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18260\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}