Google Plus

How to Protect apache directories with a password in Ubuntu

Written by Mel Kham on . Posted in Frequently Asked Questions

Question : How to Protect apache directories with a password in Ubuntu?apache-logo

 

Answer:

  To  enable  this  option we  have  to use  2 terms  .htaccess  and  htpasswd

{xtypo_warning}Warning: On at least some versions of Ubuntu, .htaccess files will not work by default. See EnablingUseOfApacheHtaccessFiles for help on enabling them.{/xtypo_warning}

 

  • Create a file called .htaccess in the directory you want to password-protect with the follwing content:
AuthUserFile /your/path/.htpasswd
AuthName "Authorization Required"
AuthType Basic
require valid-user

Instead of valid-user, you can also add the users you want directly

  • If you want to password protect just a single file in a folder add the following lines to the .htaccess file:
<Files "mypage.html">
Require valid-user
</Files>

Then create the file /your/path/.htpasswd which contains the users that are allowed to login and their passwords. We do that with the htpasswd command:

htpasswd -c /path/to/your/.htpasswd user1

Example:

pirat9@unixmen-laptop:/var/www$ sudo htpasswd  -c /var/www/web/.htpasswd  pirat9
Output
New password: 
Re-type new password:
Adding password for user pirat9
pirat9@unixmen-laptop:/var/www$

You can see your  crypted  password  with

pirat9@unixmen-laptop:/var/www$ sudo more  /var/www/web/.htpasswd  
pirat9:Amt1ZMf.BqDjC
  • Finally we need to add the following lines to /etc/apache2/apache2.conf:

<Directory /your/path>
AllowOverride All
</Directory>

You have to adjust /your/path/.htpasswd

Restart your webserver:

sudo /etc/init.d/apache2 restart

Now  restart  apache  and  check

http://ip/path

Useful links: Ubuntu help

For questions please refer to our Q/A forum at : http://ask.unixmen.com

Mel Kham

Founder of Unixmen, Living in Amsterdam. Am working in my free time to help people to understand the Opensource and to explain them in easy way how to make the fist steps to the the light. Working day and night with my Co-founder Zinovsky to keep this website live even with less resources.
  • http://www.freetstar.com freetstar

    thanks for the blog!it helps me in apache2

    • Zinovsky

      You are welcome

  • rudrakshya

    AuthUserFile command not found in ubuntu 12.04

Like us on Facebook

This week Top Posts

Write for us

Recent Comments

Ladi Oyekanmi

|

Could you please assist in step by step on how to install nagios on solaris

Adhraa

|

Greate tutorial :)

pigmej

|

Just one thing:

What about pep8 in your python code ? How can you give ‘tutorials’ on quite popular website, of such a bad quality ?

Amit Rai

|

I just renamed shared.xml and it logged in and created a new shared.xml.

DB Griffin

|

Larry Page is not being completely honest! The manner in which the PRISM program/project works does not need access from company administrators or owners, so called “direct access”; the access to the information is already there. These tech company CEOs take for granted the actual intelligence of most end users of their products. All it takes is a little digging and reading to go from ignorant to informed on these things especially on exactly how the internet works/functions in the U.S.A. I find Larry Page’s remarks just as laughable as Al Gore’s claim to “inventing” the internet/world wide web!

If you, as an end user, are reading this post; I challenge you to research these matters yourself. It really is quite simple with all the “information sites” that exist on the web today ie Wikipedia, & other online encyclopedias that actually list source material, as well as highly respected tech sites and blogs that also list their source material. Be warned: this is only the tip of the iceberg and these tech CEOs know and understand this; they are scrambling in attempt to perform DAMAGE CONTROL to save the company and what little trust thay have left from their products end users/consumers.

Am I a skeptic? I believe someone has to be or needs to be at this point in time! If your not just a little skeptical of the government, tech companies, and the people that are in charge of these agencies and companies; you need to be, even if just a little skeptic. For your own personal protection and security! I know I was a part of this community for over 14 years!

 
IDG Tech Network
Copyright © 2008-2013 Unixmen.com .
Maintained by Anblik .