Failed: Configure protected directories in Apache

I used following command and set .htpasswd user and password. but i got task failed.

sudo htpasswd -c /etc/httpd/.htpasswd username

Can you tell me what mistake i made.

@harikrishnan can you please share the error which you are getting. meanwhile there are no issue with command.

Hi @harikrishnan I guess you created the file correctly but , you did not set proper permissions for the file , the file owner should be apache user and it should be able to read the file. Also did you start the httpd service before finishing the task? Try this when the task is assigned again.

Sure. Thanks for update

1 Like

You need to validate that under Directory section for β€œvar/www/html/” and AllowOverride to All
#sudo vi /etc/httpd/conf/httpd.conf

Also you create .htaccess on new directory

you can test your configuration is correct by

curl -u rose

You will be prompt for the password

1 Like

In jumphost:
thor@jump_host /$ scp /tmp/index.html tony@stapp01:/tmp
scp /tmp/index.html steve@stapp02:~

cd /var/www/html/
mkdir finance
cd finance
cp /tmp/index.html .

sudo htpasswd -c /var/www/html/finance/.htpasswd rose
chown apache:apache /var/www/html/finance/.htpasswd
chmod 640  /var/www/html/finance/.htpasswd

Also created .htaccess under  /var/www/html/finance/ :

AuthType Basic
AuthName "Restricted Access"
AuthUserFile /var/www/html/finance/.htpasswd
Require user rose

systemctl restart httpd
systemctl status httpd

To Validate :

curl -u rose
curl -u rose

refer :

this is worked for me.

1 Like