How to Set Up Password Authentication with Apache in Ubuntu?

By Hardik Savani December 30, 2021 Category : Ubuntu Server

Here, i will show you how to works how to set up password authentication with apache on ubuntu 20.04. you can understand a concept of ubuntu apache password authentication. In this article, we will implement a configure apache password authentication. Here you will learn ubuntu apache authentication.

In this post i will give you list of commands how to set username password authentication with apache in ubuntu 22.10, ubuntu 22.04, ubuntu 21.10, ubuntu 21.04, ubuntu 20.04, ubuntu 18.04 and ubuntu 16.04 server. so let's see bellow commands to add authentication.

Install apache2-utils

Here, we need to install apache2-utils package.

sudo apt-get install apache2-utils

Set Username and Password

Now, set username and password that you want to keep.

Syntax:

sudo htpasswd -c /etc/apache2/.htpasswd <username>

Example:

sudo htpasswd -c /etc/apache2/.htpasswd hardik

you can see bellow layout:

Add Apache Authentication:

Here, we will add Directory tag for basic authentication.

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

<VirtualHost *:80>

# The ServerName directive sets the request scheme, hostname and port that

# the server uses to identify itself. This is used when creating

# redirection URLs. In the context of virtual hosts, the ServerName

# specifies what hostname must appear in the request's Host: header to

# match this virtual host. For the default virtual host (this file) this

# value is not decisive as it is used as a last resort host regardless.

# However, you must set it for any further virtual host explicitly.

#ServerName www.example.com

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

# error, crit, alert, emerg.

# It is also possible to configure the loglevel for particular

# modules, e.g.

#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are

# enabled or disabled at a global level, it is possible to

# include a line for only one particular virtual host. For example the

# following line enables the CGI configuration for this host only

# after it has been globally disabled with "a2disconf".

#Include conf-available/serve-cgi-bin.conf

<Directory "/var/www/html">

AuthType Basic

AuthName "Restricted Content"

AuthUserFile /etc/apache2/.htpasswd

Require valid-user

</Directory>

</VirtualHost>

Now, let's restart apache2.

sudo service apache2 reload

After install successfully, you can go to browser and check your IP as like bellow and layout:

http://your_server_ip

OR

http://localhost

i hope it can help you...

Tags :
Shares