How to Password Protect your Website using .htaccess

Share It!

Apache authentication can be configured to require site users to login with a user id and secret key. Secret key securing a directory on your site is very simple. Website admins normally need to secure a directory in the event that they have data that they need to make accessible just to a chose number of people.


See Also:

  • Types of Virtual Web Hosting in Apache
  • How To Set Up Apache Virtual Hosts on CentOS 5/6/7
  • How to create Multiple Virtual Host in CentOS/RHEL 5/6/7
  • How to create Apache Virtual Host on Ubuntu
  • How to Install LAMP on CentOS/RHEL 7
  • Install LAMP on CentOS/RHEL 5/6
  • Apache configuration files:

    RedHat / Fedora / CentOS: "/etc/httpd/conf/httpd.conf"
    SuSE: "/etc/apache2/httpd.conf"
    Ubuntu / Debian: "/etc/apache2/apache2.conf"

    Follow below steps to protect your website with a Password Using .htaccess:

    Step #1: Create a .htaccess file

    First access the directory you want to protect with password. Create .htaccess file and add below entries:

    # cd /home/techoism/public_html/Admin/
    # vim .htaccess
    AuthName "Private"
    AuthType Basic
    AuthUserFile /home/techoism/public_html/.htpasswd
    require valid-user


  • AuthName: “AuthName” will be displayed when the browser prompts for a password.
  • AuthType: “AuthType” tells the server what sort of processing is in use, and “Basic” is the most common and perfectly adequate for almost any purpose.
  • AuthUserFile: “AuthUserFile” line tells the Apache web server where it can locate this password file.
  • require: If we want to grant access to everyone in the .htpasswd file, we can add this line (“valid-user” is like a keyword, telling apache any user will do)
  • Step #2: Create the Password File

    If you don’t current have an .htpasswd, use the “-c” option to create the file with the first user. It will prompt you for a password and encrypt it for you.

    # htpasswd -c /home/techoism/public_html/Admin/.htpasswd dennis

    If you already have the .htpasswd file and would like to append a new user run the following command:

    # htpasswd -m /home/techoism/public_html/Admin/.htpasswd steve

    Step #3: Verify the Configuration

    Now you can verify the configuration on your browser by running below domain:



    Enjoy it!

    Leave a Reply

    Your email address will not be published.