InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Apache tutorials

  • AWStats on Ubuntu
  • ApacheBench - load testing
  • Auth to a location or directory
  • Block directory access using htaccess 404
  • Different expire Headers for multiple images
  • Disable directory listing
  • List loaded modules
  • Log Content-Type in access log
  • Log latency and host in apache log
  • Monitoring using mod_status
  • Python to analyze bots in logs
  • Remove php extension from url
  • egrep and access log
  • log custom data in apache access log
  • mod_rewrite
  • top IP list from access log
 
  • Home
  • > Tutorials
  • > Web Development
  • > Apache

Apache – add basic auth to a location or directory

By admin on Nov 14, 2015

In case you want to protect a directory from public access, you can add Apache basic auth to it. This may be needed if you have a wordpress or some other software and want to keep some internal scripts behind auth. Here are quick steps to do this using Apache or .htaccess configuration.

Apache config for basic auth

Use the following code if you want to add basic auth to a location /internal which is served by directory internal inside Apache document root.

<Location /internal>
  AuthType Basic
  AuthName "Restricted Resource"
  AuthBasicProvider file
  AuthUserFile /etc/auth-file
  Require valid-user
</Location>

htaccess config for basic auth

Use the following code in .htaccess in directory internal.

AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/auth-file
Require valid-user

Using htpasswd to create auth user

To add/edit auth users to /etc/auth-file you can use the command line utility htpasswd. Here is the command you can use in case auth file /etc/auth-file is being created first time:

sudo htpasswd -c /etc/auth-file awstats

Incase file already exists:

sudo htpasswd /etc/auth-file awstats

View an auth protected url

When the above location is visited in browser, here is how the basic auth is asked by browser.
apache-authentication-required-example-in-chromeAny user and password mentioned in /etc/auth-file can be used for authentication.

Video version of the tutorial

Here is the video tutorial on same topic (courtesy Webucator – Online and onsite instructor led training classes). You may also want to checkout their Apache courses.

Suggested posts:

  1. How to setup svn repository on AWS Ubuntu Linux with apache simple auth
  2. How to install AWStats on Ubuntu Linux
  3. Monitor apache using mod_status on Ubuntu
  4. Disable directory listing in apache
  5. Block directory access using htaccess 404
  6. Command line – top IP list from apache access log
  7. Chrome – how to find a plugin directory location on Mac
  8. node – how to find source file location of a module
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Apache, Linux, Tutorials, Ubuntu Linux, Web Development

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2022 InfoHeap.

Powered by WordPress