<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.r512.com/index.php?action=history&amp;feed=atom&amp;title=Setup_Password_Authentication_with_Apache</id>
	<title>Setup Password Authentication with Apache - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.r512.com/index.php?action=history&amp;feed=atom&amp;title=Setup_Password_Authentication_with_Apache"/>
	<link rel="alternate" type="text/html" href="https://www.r512.com/index.php?title=Setup_Password_Authentication_with_Apache&amp;action=history"/>
	<updated>2026-04-30T17:49:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://www.r512.com/index.php?title=Setup_Password_Authentication_with_Apache&amp;diff=332&amp;oldid=prev</id>
		<title>Bacchas: Created page with &quot;== Introduction== When setting up a web server, there are often sections of the site that you wish to restrict access to. Web applications often provide their own authenticati...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.r512.com/index.php?title=Setup_Password_Authentication_with_Apache&amp;diff=332&amp;oldid=prev"/>
		<updated>2016-08-17T15:11:13Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Introduction== When setting up a web server, there are often sections of the site that you wish to restrict access to. Web applications often provide their own authenticati...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Introduction==&lt;br /&gt;
When setting up a web server, there are often sections of the site that you wish to restrict access to. Web applications often provide their own authentication and authorization methods, but the web server itself can be used to restrict access if these are inadequate or unavailable.&lt;br /&gt;
&lt;br /&gt;
==Install the Apache Utilities Package==&lt;br /&gt;
In order to create the file that will store the passwords needed to access our restricted content, we will use a utility called htpasswd. This is found in the apache2-utils package within the Ubuntu repositories.&lt;br /&gt;
&lt;br /&gt;
Update the local package cache and install the package by typing this command. We will take this opportunity to also grab the Apache2 server in case it is not yet installed on the server:&lt;br /&gt;
&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt-get install apache2 apache2-utils&lt;br /&gt;
&lt;br /&gt;
==Create the Password File==&lt;br /&gt;
We now have access to the htpasswd command. We can use this to create a password file that Apache can use to authenticate users. We will create a hidden file for this purpose called .htpasswd within our /etc/apache2 configuration directory.&lt;br /&gt;
&lt;br /&gt;
The first time we use this utility, we need to add the -c option to create the specified file. We specify a username (sammy in this example) at the end of the command to create a new entry within the file:&lt;br /&gt;
&lt;br /&gt;
 sudo htpasswd -c /etc/apache2/.htpasswd sammy&lt;br /&gt;
&lt;br /&gt;
You will be asked to supply and confirm a password for the user.&lt;br /&gt;
&lt;br /&gt;
Leave out the -c argument for any additional users you wish to add:&lt;br /&gt;
&lt;br /&gt;
 sudo htpasswd /etc/apache2/.htpasswd another_user&lt;br /&gt;
If we view the contents of the file, we can see the username and the encrypted password for each record:&lt;br /&gt;
&lt;br /&gt;
 cat /etc/apache2/.htpasswd&lt;br /&gt;
Output&lt;br /&gt;
 sammy:$apr1$lzxsIfXG$tmCvCfb49vpPFwKGVsuYz.&lt;br /&gt;
 another_user:$apr1$p1E9MeAf$kiAhneUwr.MhAE2kKGYHK.&lt;br /&gt;
&lt;br /&gt;
==Configure Apache Password Authentication==&lt;br /&gt;
Now that we have a file with our users and passwords in a format that Apache can read, we need to configure Apache to check this file before serving our protected content. We can do this in two different ways.&lt;br /&gt;
&lt;br /&gt;
The first option is to edit the Apache configuration and add our password protection to the virtual host file. This will generally give better performance because it avoids the expense of reading distributed configuration files. If you have this option, this method is recommended.&lt;br /&gt;
&lt;br /&gt;
If you do not have the ability to modify the virtual host file (or if you are already using .htaccess files for other purposes), you can restrict access using an.htaccessfile. Apache uses.htaccess` files in order to allow certain configuration items to be set within a file in a content directory. The disadvantage is that Apache has to re-read these files on every request that involves the directory, which can impact performance.&lt;br /&gt;
&lt;br /&gt;
==Configuring Access Control within the Virtual Host Definition==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;VirtualHost *:80&amp;gt;&lt;br /&gt;
    ServerAdmin webmaster@localhost&lt;br /&gt;
    DocumentRoot /var/www/html&lt;br /&gt;
    ErrorLog ${APACHE_LOG_DIR}/error.log&lt;br /&gt;
    CustomLog ${APACHE_LOG_DIR}/access.log combined&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/html&amp;quot;&amp;gt;&lt;br /&gt;
        AuthType Basic&lt;br /&gt;
        AuthName &amp;quot;Restricted Content&amp;quot;&lt;br /&gt;
        AuthUserFile /etc/apache2/.htpasswd&lt;br /&gt;
        Require valid-user&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&amp;lt;/VirtualHost&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 sudo service apache2 restart&lt;br /&gt;
----&lt;br /&gt;
==[[#Introduction|Back To Top]]-[[Main_Page| Home]] - [[Ubuntu_Tips|Category]]==&lt;/div&gt;</summary>
		<author><name>Bacchas</name></author>
	</entry>
</feed>