{"id":13816,"date":"2019-04-06T09:40:47","date_gmt":"2019-04-06T09:40:47","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13816"},"modified":"2019-04-06T09:40:47","modified_gmt":"2019-04-06T09:40:47","slug":"how-to-password-protect-web-directories-in-apache-using-htaccess-file","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-password-protect-web-directories-in-apache-using-htaccess-file\/","title":{"rendered":"How to Password Protect Web Directories in Apache Using .htaccess File"},"content":{"rendered":"<p>When you manage online projects, you often need to limit access to that project in order to protect it against the outside world. There are might be different reasons for that \u2013 for example you want to prevent search engine crawlers from accessing your site while it is still in development phase.<\/p>\n<div id=\"attachment_17084\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Password-Protect-Apache-Directories.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17084\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Password-Protect-Apache-Directories.png\" alt=\"Password Protect Apache Web Directories\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-17084\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17084\" class=\"wp-caption-text\">Password Protect Apache Web Directories<\/p>\n<\/div>\n<p>In this tutorial,\u00a0I am going to show you how to password protected different web sites directories in\u00a0<strong>Apache<\/strong>\u00a0web server. There are many\u00a0ways you can achieve this, but we will review two of them which are most commonly used.<\/p>\n<p>The first method configures\u00a0password protection directly in Apache\u2019s configuration file, while the second one\u00a0uses\u00a0<strong>.htaccess<\/strong>\u00a0file.<\/p>\n<h3>Requirements<\/h3>\n<p>In order to setup password protection for your web directories, you will need to have:<\/p>\n<ul>\n<li>A working Apache web server<\/li>\n<li>The\u00a0<strong>AllowOverride AuthConfig<\/strong>\u00a0directive must be enabled in Apache configuration file.<\/li>\n<\/ul>\n<h3>Setup Apache Password Protected Directory<\/h3>\n<p><strong>1.<\/strong>\u00a0For this tutorial, we will be protecting the main web root directory\u00a0<code>\/var\/www\/html<\/code>. \u00a0To protect that directory, open your Apache\u2019s configuration:<\/p>\n<pre>---------------- On <strong>RedHat\/CentOS<\/strong> based systems ----------------\r\n# vi \/etc\/httpd\/conf\/httpd.conf\r\n\r\n---------------- On <strong>Debian\/Ubuntu<\/strong> based systems ----------------\r\n# nano \/etc\/apache2\/sites-available\/000-default.conf\r\n<\/pre>\n<p><strong>2.<\/strong>\u00a0Find the Apache Document directory root for\u00a0<strong>\/var\/www\/html<\/strong>\u00a0and add the following things as suggested:<\/p>\n<h4>On Apache 2.2 Version<\/h4>\n<pre>&lt;Directory \/var\/www\/html&gt; \r\nOptions Indexes Includes FollowSymLinks MultiViews \r\nAllowOverride All\r\nOrder allow,deny\r\nAllow from all \r\n&lt;\/Directory&gt;\r\n<\/pre>\n<h4>On Apache 2.4 Version<\/h4>\n<pre>&lt;Directory \/var\/www\/html&gt; \r\nOptions Indexes Includes FollowSymLinks MultiViews \r\nAllowOverride All \r\nRequire all granted \r\n&lt;\/Directory&gt;\r\n<\/pre>\n<div id=\"attachment_17075\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Enable-AllowOverride-in-Apache-2.4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17075\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Enable-AllowOverride-in-Apache-2.4.png\" alt=\"Apache 2.4: Enable AllowOverride All\" width=\"929\" height=\"572\" aria-describedby=\"caption-attachment-17075\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17075\" class=\"wp-caption-text\">Apache 2.4: Enable AllowOverride All<\/p>\n<\/div>\n<p><strong>3.<\/strong>\u00a0Save the file and restart Apache by using the following command:<\/p>\n<pre>--------------- On <strong>Systemd<\/strong> -------------------\r\n# systemctl restart httpd         [On <strong>RedHat<\/strong> based systems]\r\n# systemctl restart apache2       [On <strong>Debian<\/strong> based systems]\r\n\r\n\r\n--------------- On <strong>SysV init<\/strong> -----------------\r\n# service httpd restart           [On <strong>RedHat<\/strong> based systems]\r\n# service apache2 restart         [On <strong>Debian<\/strong> based systems]\r\n<\/pre>\n<p><strong>4.<\/strong>\u00a0Now we will use the\u00a0<strong>htpasswd<\/strong>\u00a0command to generate username and password for our protected directory. This command is used to manage user files for basic authentication.<\/p>\n<p>The general syntax of the command is:<\/p>\n<pre># htpasswd -c filename username\r\n<\/pre>\n<p>The\u00a0<code>-c<\/code>\u00a0option specifies the file that will keep the encrypted password and\u00a0<b>username<\/b>\u00a0specifies the user for the authentication.<\/p>\n<p><strong>5.<\/strong>\u00a0Our password file needs to be located out of the Apache\u2019s web accessible directory so that it is well protected. For that purpose, we will create new directory:<\/p>\n<pre># mkdir \/home\/tecmint\r\n<\/pre>\n<p><strong>6.<\/strong>\u00a0After that we will generate our username and password that will be stored in that directory:<\/p>\n<pre># htpasswd -c \/home\/tecmint\/webpass tecmint\r\n<\/pre>\n<p>Once you execute this command you will have to enter a password for our new user\u00a0<code>\"tecmint\"<\/code>\u00a0twice:<\/p>\n<div id=\"attachment_17077\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Apache-User.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17077\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Apache-User.png\" alt=\"Create Apache User Password\" width=\"720\" height=\"400\" aria-describedby=\"caption-attachment-17077\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17077\" class=\"wp-caption-text\">Create Apache User Password<\/p>\n<\/div>\n<p>After that we will need to make sure that Apache is able to read the \u201c<strong>webpass<\/strong>\u201d file. For that purpose, you will need to change the ownership of that file with the following command:<\/p>\n<pre>---------------- On <strong>RedHat\/CentOS<\/strong> based systems ----------------\r\n# chown apache: \/home\/tecmint\/webpass\r\n# chmod 640 \/home\/tecmint\/webpass\r\n<\/pre>\n<pre>---------------- On <strong>Debian\/Ubuntu<\/strong> based systems ----------------\r\n# chown www-data \/home\/tecmint\/webpass\r\n# chmod 640 \/home\/tecmint\/webpass\r\n<\/pre>\n<p><strong>7.<\/strong>\u00a0At this point our new user and password are ready. Now we need to tell Apache to request password when accessing our targeted directory. For that purpose, create file called\u00a0<strong>.htaccess<\/strong>\u00a0in\u00a0<strong>\/var\/www\/html<\/strong>:<\/p>\n<pre># vi \/var\/www\/html\/.htaccess\r\n<\/pre>\n<p>Add the following code in it:<\/p>\n<pre>AuthType Basic\r\nAuthName \"Restricted Access\"\r\nAuthUserFile \/home\/tecmint\/webpass\r\nRequire user tecmint\r\n<\/pre>\n<div id=\"attachment_17078\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Apache-Restricted-Access.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17078\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Apache-Restricted-Access.png\" alt=\"Create Apache Restricted Access\" width=\"720\" height=\"400\" aria-describedby=\"caption-attachment-17078\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17078\" class=\"wp-caption-text\">Create Apache Restricted Access<\/p>\n<\/div>\n<p>8. Now save the file and put your setup to the test. Open your browser and enter your IP address or domain name in the web browser, for example:<\/p>\n<pre>http:\/\/ip-address\r\n<\/pre>\n<p>You should be prompted for username and password:<\/p>\n<div id=\"attachment_17079\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Apache-Password-Protected-Directory-Authentication.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17079\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Apache-Password-Protected-Directory-Authentication.png\" alt=\"Apache Password Protected Directory Authentication\" width=\"981\" height=\"675\" aria-describedby=\"caption-attachment-17079\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17079\" class=\"wp-caption-text\">Apache Password Protected Directory Authentication<\/p>\n<\/div>\n<p>Enter the username and password that you set to proceed to your page.<\/p>\n<h3>Additional Notes<\/h3>\n<p>If you are using shared hosting, you will most probably not have access to the Apache configuration file. However most hosting companies\u00a0have enabled the \u201c<strong>AllowOverride All\u201d<\/strong>\u00a0option by default. This means that you will only need to generate the username and password and then select directory that you wish to protect. This significantly eases your task.<\/p>\n<h3>Conclusion<\/h3>\n<p>I hope that you found this tutorial useful and help you achieve your goal. If you have any questions or comments, please do not hesitate to post them in the section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/password-protect-apache-web-directories-using-htaccess\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you manage online projects, you often need to limit access to that project in order to protect it against the outside world. There are might be different reasons for that \u2013 for example you want to prevent search engine crawlers from accessing your site while it is still in development phase. Password Protect Apache &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-password-protect-web-directories-in-apache-using-htaccess-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Password Protect Web Directories in Apache Using .htaccess File&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13816","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13816"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13816\/revisions"}],"predecessor-version":[{"id":13817,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13816\/revisions\/13817"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}