How To Setup Password Authentication With Apache2 in LinuxMint 20.2

To Setup Password Authentication with Apache in Linux Mint 20.2

Requirements:

In order to setup password protection for your web directories, you will need to have A working Apache web server The AllowOverride AuthConfig directive must be enabled in Apache configuration file.

Installation Procedure:

Step 1: Check the OS Version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Linuxmint
Description:	Linux Mint 20.2
Release:	20.2
Codename:	uma

Step 2: Install apache2 package by using the below command

root@linuxhelp:~# apt install apache2 apache2-utils -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,715 kB of archives.
After this operation, 7,504 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libapr1 amd64 1.6.5-1ubuntu1 [91.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1 amd64 1.6.1-4ubuntu2 [84.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1-dbd-sqlite3 amd64 1.6.1-4ubuntu2 [10.5 kB]


Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1-ldap amd64 1.6.1-4ubuntu2 [8,736 B]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-bin amd64 2.4.41-4ubuntu3.8 [1,181 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-data all 2.4.41-4ubuntu3.8 [159 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-utils amd64 2.4.41-4ubuntu3.8 [84.5 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2 amd64 2.4.41-4ubuntu3.8 [95.5 kB]
Fetched 1,715 kB in 3s (635 kB/s)  
Selecting previously unselected package libapr1:amd64.
(Reading database ... 316242 files and directories currently installed.)
Preparing to unpack .../0-libapr1_1.6.5-1ubuntu1_amd64.deb ...
Unpacking libapr1:amd64 (1.6.5-1ubuntu1) ...
Selecting previously unselected package libaprutil1:amd64.
Preparing to unpack .../1-libaprutil1_1.6.1-4ubuntu2_amd64.deb ...
Unpacking libaprutil1:amd64 (1.6.1-4ubuntu2) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:amd64.
Preparing to unpack .../2-libaprutil1-dbd-sqlite3_1.6.1-4ubuntu2_amd64.deb ...
Unpacking libaprutil1-dbd-sqlite3:amd64 (1.6.1-4ubuntu2) ...
Selecting previously unselected package libaprutil1-ldap:amd64.
Preparing to unpack .../3-libaprutil1-ldap_1.6.1-4ubuntu2_amd64.deb ...
Unpacking libaprutil1-ldap:amd64 (1.6.1-4ubuntu2) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../4-apache2-bin_2.4.41-4ubuntu3.8_amd64.deb ...
Unpacking apache2-bin (2.4.41-4ubuntu3.8) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../5-apache2-data_2.4.41-4ubuntu3.8_all.deb ...
Unpacking apache2-data (2.4.41-4ubuntu3.8) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../6-apache2-utils_2.4.41-4ubuntu3.8_amd64.deb ...
Unpacking apache2-utils (2.4.41-4ubuntu3.8) ...
Selecting previously unselected package apache2.
Preparing to unpack .../7-apache2_2.4.41-4ubuntu3.8_amd64.deb ...
Unpacking apache2 (2.4.41-4ubuntu3.8) ...
Setting up libapr1:amd64 (1.6.5-1ubuntu1) ...
Setting up apache2-data (2.4.41-4ubuntu3.8) ...
Setting up libaprutil1:amd64 (1.6.1-4ubuntu2) ...
Setting up libaprutil1-ldap:amd64 (1.6.1-4ubuntu2) ...
Setting up libaprutil1-dbd-sqlite3:amd64 (1.6.1-4ubuntu2) ...
Setting up apache2-utils (2.4.41-4ubuntu3.8) ...
Setting up apache2-bin (2.4.41-4ubuntu3.8) ...
Setting up apache2 (2.4.41-4ubuntu3.8) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for ufw (0.36-6ubuntu1) ...
Processing triggers for systemd (245.4-4ubuntu3.13) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) 
...

Step 3: Create the user and password by using the below command

root@linuxhelp:~# htpasswd -c /etc/apache2/.htpasswd user1
New password: 
Re-type new password: 
Adding password for user user1
root@linuxhelp:~# htpasswd -c /etc/apache2/.htpasswd user2
New password: 
Re-type new password: 
Adding password for user user2

Step 4: Check the password authentication error by using the below command

root@linuxhelp:~# cat /etc/apache2/.htpasswd
user2:$apr1$.D1yJZys$uE93fA7sTbCbjYtbfJJxM1 

Step 5: Edit the default configuration file by using the below command

root@linuxhelp:~# nano /etc/apache2/sites-enabled/000-default.conf

Step 6: Check the error of the above configuration file by using the below command

root@linuxhelp:~# apachectl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

Step 7: Restart apache2 service by using the below command

root@linuxhelp:~# systemctl restart apache2

Step 8: Check the status of apache2 service by using the below command

root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

     Active: active (running) since Mon 2021-12-06 12:54:17 IST; 7s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 5391 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 5410 (apache2)
      Tasks: 55 (limit: 4579)
     Memory: 5.7M
     CGroup: /system.slice/apache2.service
             ├─5410 /usr/sbin/apache2 -k start
             ├─5411 /usr/sbin/apache2 -k start
             └─5412 /usr/sbin/apache2 -k start

Dec 06 12:54:17 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Dec 06 12:54:17 linuxhelp apachectl[5400]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Dec 06 12:54:17 linuxhelp systemd[1]: Started The Apache HTTP Server.

Step 9: Edit apache2 configuration file by using the below command

root@linuxhelp:~# nano /etc/apache2/apache2.conf

Step 10: Edit .htaccess configuration file by using the below command

root@linuxhelp:~# nano /var/www/html/.htacces
[1]+  Stopped                 nano /var/www/html/.htacces

Step 11: Restart apache2 service by using the below command

root@linuxhelp:~# systemctl restart apache2

Step 12: Check status apache2 service by using the below command

root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-12-06 13:01:33 IST; 3s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 5483 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 5505 (apache2)
      Tasks: 55 (limit: 4579)
     Memory: 5.8M
     CGroup: /system.slice/apache2.service
             ├─5505 /usr/sbin/apache2 -k start
             ├─5507 /usr/sbin/apache2 -k start
             └─5508 /usr/sbin/apache2 -k start

Dec 06 13:01:33 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Dec 06 13:01:33 linuxhelp apachectl[5494]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Dec 06 13:01:33 linuxhelp systemd[1]: Started The Apache HTTP Server.

Step 13: Once the above process is completed, let’s try to access the restricted content in a web browser as shown in the below image.

Step 14: Enter the following credentials, here you can see the Apache default page appears as shown in the below image

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to Setup Password Authentication with Apache in Apache in Linux Mint 20.2. Your feedback is much welcome.

FAQ
Q
What is the path of the .htaccess file?
A
/var/www/html/.htaccess
Q
What is the apache2 configuration file?
A
/etc/apache2/apache2.conf
Q
How do you check the syntax of the configuration?
A
apachectl configtest
Q
What configuration file for the 000-default.con?
A
/etc/apache2/sites-enabled/000-default.conf
Q
What is the apache2 package for password authentication?
A
apt-get install apache2 apache2-utils -y