Bobcares

Set up Apache htpasswd Authentication: Configuration

by | Oct 30, 2022

Let us take a closer look at how to set up Apache htpasswd Authentication with the support of our Server Management Support services at Bobcares.

Install Apache2 utils on Ubuntu to set up Apache htpasswd Authentication

set up Apache htpasswd Authentication

If apache2-utils is not already installed on the server, use the following instructions to install it. To see if it is already installed, use the command below.

$ sudo apt-get update
$ sudo apt-get install apache2-utils

The installation process is now complete. To save the user information, we must now construct the htpasswd file.

Create Apache htpasswd file to setup htpasswd Authentication

 

The htpasswd file is really named .htpasswd. We can alter the default name to anything we choose. The initial character of the filename, “.”, indicates that the file should be hidden in the list. To generate a blank .htpasswd file in Apache’s configuration directory, use the following command. We can keep it anywhere we choose, according to our needs.

$ sudo touch /etc/apache2/.htpasswd

We may now add new users to the.htpasswd file. The htpasswd command will be used for this operation.

It’s worth noting that we’re not restricted to just one htpasswd file. For various apps with different users, we may generate numerous htpasswd files in separate places.

htpasswd Add User

We only need to run a command, and the utility will take care of the rest. To add a new user to the htpasswd file, use the following command in the terminal.

$ sudo htpasswd /etc/apache2/.htpasswd USERNAME

We may change the text color of the command to orange. Simply provide the right htpasswd file location and the user we want to add to that htpasswd file.

It will prompt us for a new password twice. Enter a secure password. To add more users, we may use the same command.

Apache Htpasswd protection using VirtualHost

A virtual host is a file that instructs the web browser (Apache) to redirect a request based on the domain name to a certain document root. By inserting a few lines of code into our virtual host file, we may secure a single directory or an entire program. Execute the following command to open the virtual host file in edit mode.

$ sudo nano /etc/apache2/sites-available/000-default.conf

Here we’re modifying a standard Apache virtual host here. However, by altering the file name, we may open the one we wish to modify. Once in edit mode, add the colored code below to the file.


.
.
.
Directory /var/www/html/protected
AuthType Basic
AuthName "Protected"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
Directory
/VirtualHost

Replace the absolute path to the directory to be protected as well as the location of the htpasswd file. Once finished, use CTRL+X, then Y, then Enter to save the configuration file. To restart the Apache web server, use the following command.

$ sudo service apache2 restart

To apply the modifications, we must restart the Apache server. After then, try to visit the directory or website to secure it. An alert will appear, requesting the username and password. We may access the website/directory by entering the right login and password.

Apache Htpasswd protection using htaccess

We can also protect the contents by editing the.htaccess file on the server. It is a simple procedure, and we can also create .htaccess files within subdirectories. To secure a directory with simple authentication, we can build a new.htaccess file within that directory.

To open the.htaccess file in edit mode, use the following command. The command will create a new file if it does not already exist.

$ sudo nano /var/www/html/protected/.htaccess

Remember to change the path to the directory we wish to protect. Once in edit mode, paste the following code at the start of the file.

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

Once finished, use CTRL+X, then Y, then Enter to save the file. After that, open a browser and try to visit the protected directory or a website. Before the contents are shown in the browser, it will prompt us for our username and password.

[Need assistance with similar queries? We are here to help]

Conclusion

To sum up we have now learned how to set up Apache htpasswd Authentication. With the support of our Server management support services, we have gone through all of the steps to set up the extra security.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF