{"id":1490,"date":"2018-11-23T13:37:32","date_gmt":"2018-11-23T10:37:32","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1490"},"modified":"2024-03-11T21:31:02","modified_gmt":"2024-03-11T18:31:02","slug":"how-to-protect-apache-web-directories-with-password-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-protect-apache-web-directories-with-password-on-ubuntu-18-04\/","title":{"rendered":"How to Protect Apache Web Directories with Password on Ubuntu 18.04"},"content":{"rendered":"\n<p>Hello there. Welcome to our guide on how to protect Apache web directories with password on Ubuntu 18.04. This will enable you to restrict access to various sections of your web site. In this regard, Apache supports two contexts in which Authentication directives can be applied and these are; <strong><code>Directory<\/code> <\/strong>and <code><strong>htaccess<\/strong><\/code>.<\/p>\n\n\n\n<p>While the authentication directives can be used within <strong><code>&lt;Directory&gt;<\/code><\/strong>, <strong><code>&lt;Location&gt;<\/code><\/strong>, <strong><code>&lt;Files&gt;<\/code><\/strong> and even <strong><code>&lt;Proxy&gt;<\/code> <\/strong>blocks in the Apache configuration file, the <code><strong>htaccess<\/strong><\/code> context authentication directives can be used within <code><strong>.htaccess<\/strong><\/code> files.<\/p>\n\n\n\n<p>In this guide, we are going to learn how setup password Apache protected directories in both contexts.<\/p>\n\n\n\n<p>Well, before you can proceed, ensure that you have Apache HTTP server utility program installed in your server. This utility provides the <code>htpasswd<\/code> command. To verify that this packages is already installed,run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo dpkg -s apache2-utils | grep -i status\nStatus: install ok installed<\/pre>\n\n\n\n<p>If for some reasons it is not installed, you can just install it by running the following command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install apache2-utils<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up Password Protected Directory using Directory Context<\/h3>\n\n\n\n<p>So to kick off with, let us assume that you want to password protect the web site root directory located at <code>\/var\/www\/html\/example<\/code>.<\/p>\n\n\n\n<p>To proceed, you have to create a flat-file that is used to store usernames and password for basic authentication of HTTP users. The password file is generated using the <code>htpasswd<\/code> utility and can be stored just about anywhere in your server. <code>htpasswd<\/code> encrypts passwords using either bcrypt, a version of MD5 modified for Apache, SHA1, or the system&#8217;s crypt() routine.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a Password File<\/h4>\n\n\n\n<p>To create a password file, run the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo htpasswd -c \/etc\/apache2\/.webroot amos<\/pre>\n\n\n\n<p>This will create a hidden flat-file called <code>webroot<\/code> under <code>\/etc\/apache2\/<\/code>. The <code><strong>-c<\/strong><\/code> tells htpasswd to create a password file. If the file already exists, it will be rewritten and truncated. Therefore, to add another user to the same file, run the same command without option <strong>-c<\/strong>. For example, to add user mibey,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo htpasswd \/etc\/apache2\/.webroot mibey<\/pre>\n\n\n\n<p>You now have two users who can authenticate to the specific web root directories and whose passwords are hashed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo less \/etc\/apache2\/.webroot\n<strong>amos:$apr1$FJuti2Ok$4apPG6wrlrhV0lexnRLoA1<\/strong>\n<strong>mibey:$apr1$Hqc217.G$d2rzs8d9SbzE1ap\/v7jbP\/<\/strong><\/pre>\n\n\n\n<p>Note that if htpasswd utility is not located in your PATH, you can find its location with <code>which<\/code> command and specify the full path when running i.e <strong>\/full\/path\/to\/htpasswd<\/strong>.<\/p>\n\n\n\n<p>Ensure that Apache has access to the password file. Thus you can set ownership and permissions as follows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown www-data.www-data \/etc\/apache2\/.webroot\nsudo chmod 644 \/etc\/apache2\/.webroot<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Protect Apache Directory<\/h4>\n\n\n\n<p>Now that we have generated the authentication details, you can now set the directory authentication directives within the main Apache config file or your virtual host config file as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vim \/etc\/apache2\/sites-available\/example.conf<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost *:80&gt;\n  # Admin email, Server Name (domain name) and any aliases\n  ServerAdmin webmaster@test.com\n  ServerName  example.com\n  ServerAlias www.example.com\n  \n  DirectoryIndex index.html\n  DocumentRoot \/var\/www\/html\n  # Custom log file locations\n  LogLevel warn\n  ErrorLog \/var\/log\/apache2\/error-example.com.log\n  CustomLog \/var\/log\/apache2\/access-example.com.log combined\n\n<strong>  &lt;Directory \"\/var\/www\/html\/example\"&gt;<\/strong>\n<strong>    AuthType Basic<\/strong>\n<strong>    AuthName \"Authentication Required\"<\/strong>\n<strong>    AuthUserFile \"\/etc\/apache2\/.webroot\"<\/strong>\n<strong>    Require valid-user<\/strong>\n<strong>  &lt;\/Directory&gt;<\/strong>\n&lt;\/VirtualHost&gt;<\/pre>\n\n\n\n<p>The directives used above are;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>AuthType<\/strong><\/code> &#8211; Defines the type of authentication, basic in this example<\/li>\n\n\n\n<li><strong><code>AuthName<\/code> <\/strong>&#8211; Defines the message displayed on the password prompt from the browser.<\/li>\n\n\n\n<li><code><strong>AuthUserFile<\/strong><\/code>&nbsp;&#8211; Defines the location of the password file.<\/li>\n\n\n\n<li><code><strong>Require<\/strong><\/code>&nbsp;&#8211; Specifies that only authenticates users are granted access.<\/li>\n<\/ul>\n\n\n\n<p>Once you are done with the configuration, save the file and restart Apache.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apachectl -t\nSyntax OK<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart apache2<\/pre>\n\n\n\n<p>When you try to access your page on your browser, you should get a password prompt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/192.168.43.99\/example<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1493\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/directory-htaccess.png\" alt=\"directory-htaccess\" width=\"842\" height=\"406\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/directory-htaccess.png 842w, https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/directory-htaccess-768x370.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/h3>\n\n\n\n<p>When you enter your Password, you can be able to see your site contents.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"147\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/directory-access.png\" alt=\"directory-access\" class=\"wp-image-1494\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up Password Protected Directory using .htaccess File<\/h3>\n\n\n\n<p>The <code>.htaccess<\/code> files also known as distributed configuration files, provide a way to make configuration changes on a per-directory basis. If it is possible, do not use this file at all as it will slow down your Apache HTTP server.<\/p>\n\n\n\n<p>To use <strong>htaccess<\/strong>, create <strong>.htaccess<\/strong> file in the directory whose access is to be protected with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vim \/var\/www\/html\/example\/.htaccess\nsudo chown www-data.www-data \/var\/www\/html\/example\/.htaccess<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong> AuthType Basic<\/strong>\n<strong> AuthName \"Restricted Content\"<\/strong>\n<strong> AuthUserFile \"\/etc\/apache2\/.webroot\"<\/strong>\n<strong> Require valid-user<\/strong><\/pre>\n\n\n\n<p>After that, edit the main Apache config file or your virtual host config file and create a <strong>&lt;Directory&gt; <\/strong>block with the following content;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vim \/etc\/apache2\/sites-available\/example.conf<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>...<\/strong>\n  ErrorLog \/var\/log\/apache2\/error-test.com.log\n  CustomLog \/var\/log\/apache2\/access-test.com.log combined\n<strong>  &lt;Directory \"\/var\/www\/html\/example\"&gt;<\/strong>\n<strong>    Options Indexes FollowSymLinks<\/strong>\n<strong>    AllowOverride All<\/strong>\n<strong>    Require all granted<\/strong>\n<strong>  &lt;\/Directory&gt;<\/strong>\n&lt;\/VirtualHost&gt;<\/pre>\n\n\n\n<p>Save and close the file when you are done making changes. After that, you need to restart you web server in order to effect the changes. When you navigate to the browser and try to access your site content, you should be prompted to authenticate.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"211\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/htaccess.png\" alt=\"Apache htaccess\" class=\"wp-image-1495\" title=\"\"><\/figure>\n\n\n\n<p>Well, that is all about how to password protect an Apache directory using basic authentication. We hope this was informative.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello there. Welcome to our guide on how to protect Apache web directories with password on Ubuntu 18.04. This will enable you to restrict access<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,254,253],"tags":[255,257,256],"class_list":["post-1490","post","type-post","status-publish","format-standard","hentry","category-howtos","category-apache","category-web-servers","tag-apache2","tag-htaccess","tag-htpasswd","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1490"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=1490"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1490\/revisions"}],"predecessor-version":[{"id":21054,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1490\/revisions\/21054"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=1490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}