Skip to content

Instantly share code, notes, and snippets.

@cisoun
Created August 19, 2021 17:28
Show Gist options
  • Save cisoun/292735b17e3974ec3b189634ae11646d to your computer and use it in GitHub Desktop.
Save cisoun/292735b17e3974ec3b189634ae11646d to your computer and use it in GitHub Desktop.
File securization on Apache

File securization on Apache

Here, I explain how to provide securized access to a file (or folder) on an Apache server with a basic authentication. Let's say we are on folder /srv/http and we would like to restrain access to hello.txt to user alice only.

cd /srv/http
touch hello.txt

1. Generate password

htpasswd -c .htpasswd alice
New password: # Add password here.
Re-type new password: # Add password here again.
Adding password for user alice

2. Restrain access

Put this in a .htaccess file in the same folder to configure the access.

<Files "hello.txt">
	AuthName "This file needs a password"
	AuthType Basic
	AuthUserFile "/srv/http/.htpasswd"
	Require valid-user
</Files>

3. Final check

Done!

Open the file in your browser and check if a prompt is shown to ask a user and password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment