Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Setup Basic Authentication in Apache using .htaccess

    How to Setup Basic Authentication in Apache using .htaccess

    RahulBy RahulMarch 24, 20132 Mins Read

    .htaccess stands for “hypertext access”. This is the default Apache directory level configuration file. .htaccess can be used to secure a particular directories in web server. One of the most common uses is to require user authentication in order to serve certain web pages.

    Create .htaccess File.

    First create a .htaccess file in your website document root to protect entire site or in specific directory and add following content.

      AuthType Basic
      AuthName "Secure Content"
      AuthUserFile /home/myuser/public_html/.htpasswd
      require valid-user
    
  • AuthType: defines the type of authentication. Basic means there is no encryption and the password hash is sent as clear text.
  • AuthName: is content which displayed on web page when prompts for user name and password.
  • AuthUserFile: is file which stored user credentials.
  • require valid-user: indicates that only successful authenticated requests may load of the page.
  • Create Users in .htpasswd

    Now start with creating users in .htpasswd defined in .htaccess file. You can add user and password either in plain text or md5 encrypted.

    Adding password in plain text format:

    # htpasswd -c /home/myuser/public_html/.htpasswd  myuser
    

    Adding password with md5 crypt format

    # htpasswd -cm /home/myuser/public_html/.htpasswd  myuser
    
  • -c : is used only for first time when you create .htpasswd file. If you use it second time, it will remove existing file and recreate new one.
  • -m : is used to save password in md5 format.
  • Configure Apache to allow .htaccess Authentication

    By default Apache doesn’t allow to use of .htaccess, So you also need to update below setting in your httpd.conf to allow .htaccess based authentication. We use Allowoverride variable to define if .htaccess will read by apache or not.

    From:
    AllowOverride none
    
    To:
    AllowOverride AuthConfig
    

    To set AuthConfig will allow only authentication in .htaccess, rest of setting (if any) will be ignored. To allow all setting defined in .htaccess file use “All” in place of AuthConfig”.

    Restart Apache and Test Setup.

    After making any changes in apache configuration file (httpd.conf or apache2.conf), you need to restart Apache web service.

    For CentOS/RHEL 6/5 Users:

    # service httpd restart
    

    For CentOS/RHEL 7 Users:

    # systemctl enable httpd.service
    

    For Ubuntu/Debian Users:

    # service apache2 restart
    

    Thanks for reading this article, I hope it will help you to understand to set up basic authentication in Apache using .htaccess.

    Apache authentication htaccess htpasswd
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleScheduling Jobs Using at Command in Linux
    Next Article Chicken Egg Problem in Linux and Initrd

    Related Posts

    What is the /etc/aliases file

    2 Mins Read

    What is the /etc/nsswitch.conf file in Linux

    2 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How to Delete a Let’s Encrypt Certificate using Certbot

    Updated:June 3, 20222 Mins Read

    How to Install Latest Git on Ubuntu 22.04

    Updated:May 31, 20222 Mins Read

    How To Install Apache Solr 9.0 on Fedora 36/35

    Updated:May 26, 20223 Mins Read

    Comments are closed.

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.