Password-Protect a Folder with .htpasswd (Apache & Nginx)

Password-Protect a Folder with .htpasswd (Apache & Nginx)

An htpasswd generator writes one line of username:hash that Apache or Nginx Basic Auth can read. It is the right tool for a staging directory, an internal report folder, or a lab VM. It is the wrong tool for a public login with password-reset email, OAuth, or anything that must survive a stolen password file. FlashKit’s htpasswd generator builds that line in this tab. It implements SHA-1 ({SHA}) and plain text only — not bcrypt. For bcrypt, use htpasswd -B on the server; copy-paste CLI is in htpasswd command with bcrypt.

When Basic Auth is the right tool

HTTP Basic Authentication (RFC 7617, Apache mod_auth_basic, Nginx ngx_http_auth_basic_module) pops a browser dialog, sends Authorization: Basic … on every request, and checks a password file. Use it when:

  • A small ops set needs a gate in front of /staging/ or /admin/ on a host you control.
  • You can put TLS in front. Without HTTPS the password crosses the network as Base64 of user:pass — readable, not hashed, on the wire.
  • You accept that there is no per-user session logout beyond closing the browser, and no “forgot password” flow.

Do not use it as the only protection for a customer account system, a PCI form, or a folder that must stay down if the password file leaks. Hash choice then matters, and bcrypt (or the platform’s own auth) is the next step, not a SHA-1 line from a web page.

Using an htpasswd generator vs htpasswd -B

FlashKit’s form has Username (default user), Password (default password123), and two algorithm cards: SHA-1 (“Standard Apache”) and Plain Text (“Windows / Insecure”). Output is one line with a Copy button. Hashing uses crypto.subtle.digest('SHA-1') then Base64. The SHA-1 line for those defaults, computed with Python hashlib.sha1 of password123, is:

user:{SHA}y/2sYAj5yrQIN4TL0YdPdmGNKpc=

Anatomy: username, colon, literal {SHA}, then Base64 of the raw SHA-1 digest. That is Apache’s {SHA} format, not $2y$ bcrypt and not APR1. The source file notes that MD5 (APR1) and bcrypt were omitted to keep the bundle on standard browser APIs. Treat password123 as the widget sample, not a password to deploy.

On a machine with Apache htpasswd (see htpasswd manual):

htpasswd -B -c /etc/apache2/.htpasswd alice

-B is bcrypt. -c creates the file (use it once). Without -c, a second user is appended. Pair the password itself with the password generator so you are not typing a reused phrase into the hash.

Build a {SHA} line in this tab

SHA-1 or plain text, local Web Crypto. Not bcrypt. Copy the line into a file you store outside the web root.

Open the htpasswd generator

Hash choice table

Hash Looks like FlashKit? Apache 2.4 Nginx auth_basic
bcrypt $2y$… No — use htpasswd -B Yes via htpasswd -B Yes when crypt(3) knows bcrypt (libxcrypt); not guaranteed on every libc
APR1-MD5 $apr1$… No Yes Often yes (APR1 is widely supported)
SHA-1 {SHA} {SHA}… Yes Yes Not the crypt(3) formats Nginx documents; do not assume it
crypt DES / SHA-512 crypt $6$… etc. No Yes (htpasswd -d / openssl passwd) Yes — this is what auth_basic_user_file is built on
Plain user:password Yes (labelled insecure) Avoid Avoid

Recommend bcrypt for anything that will sit on a network. Use FlashKit SHA-1 only when you are targeting Apache and you know the host still wants {SHA}. Nginx’s module “allows validation of usernames and passwords using HTTP Basic Authentication protocol” against a file “generated with the Apache htpasswd utility, or by openssl” — cite ngx_http_auth_basic_module. Prefer a crypt-style hash there.

OpenSSL can emit an APR1 line without Apache installed:

openssl passwd -apr1

That prompts for a password and prints $apr1$…. It is still MD5-crypt. Digest authentication (Apache mod_auth_digest) is a different protocol and a different file format; this page does not generate Digest hashes. IIS on Windows uses its own Basic Auth store, not .htpasswd. Match the file to the server you actually run.

Apache: AuthType Basic and AllowOverride

From Apache 2.4’s Authentication and Authorization howto, a directory (or .htaccess when allowed) looks like:

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

Require valid-user is the 2.4 way; do not copy 2.2 Allow from all + Satisfy Any samples. If you put this in .htaccess, the parent <Directory> must include AllowOverride AuthConfig (or All). Without that, the file is ignored and the folder stays open — or you get 500 if a directive is forbidden. The htaccess file generator can emit other rewrite rules (HTTPS, www, hotlink, caching) — walkthrough: 10 .htaccess rules for HTTPS, www, 301s and caching; it does not replace a readable AuthUserFile path.

Nginx: auth_basic and reload

location /admin/ {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Then:

sudo nginx -t
sudo systemctl reload nginx

nginx -t validates the config. A bad auth_basic_user_file path often still passes -t and fails at request time (401/500 depending on permissions). Reload, do not restart, if you only changed a location.

Where to store the file, and permissions

Keep .htpasswd outside the web root so a misconfigured Alias cannot serve it as a download. /etc/apache2/.htpasswd or /etc/nginx/.htpasswd are the usual places on a dedicated VM. Permissions: readable by the worker user (www-data, nginx) and not by the world — chmod 640 and a group the worker is in is a common pattern. Do not commit the file to a public git repo. On shared hosting, the panel’s “password protect directory” button usually writes both the hash file and the vhost snippet; prefer that over a hand-rolled .htaccess if you cannot set AllowOverride. Two users are two lines in the same file, not two files, unless you have a reason to split audiences.

Testing with curl, and common errors

curl -I https://www.example.com/admin/
# expect 401 and WWW-Authenticate: Basic

curl -I -u alice:the-password https://www.example.com/admin/
# expect 200 if the path is right and the hash is accepted
  • 401 every time — wrong password, wrong hash format for that server, or the prompt looping because the browser cached a bad credential (try a private window).
  • 500 — Apache cannot read AuthUserFile (missing file, wrong path, SELinux, or AllowOverride forbidding the directives). Read the error log; do not guess.
  • bcrypt unsupported — Nginx on a libc without bcrypt, or an old Apache without -B. Fall back to a crypt hash the module documents, or upgrade.
  • Works on HTTP, “fails” on HTTPS — usually a different vhost file. Put the same auth_basic / AuthUserFile on the TLS server block. A 401 with the right password after a hash change often means the browser cached the old Authorization header — a private window is faster than staring at the file.

A longer password-construction note is how to create strong passwords. Basic Auth does not replace that password; it only stores a hash of it. Rotate the file by writing a new hash line and reloading; there is no “expire this user next Tuesday” field in .htpasswd. If you outgrow a shared password file, that is the moment to move to the platform’s SSO, not to add a second SHA-1 user.

Generate the password first

Local random string, then hash it with htpasswd -B or (Apache lab only) the SHA-1 generator.

Open the password generator

Frequently asked questions

What does an htpasswd generator create?

One line, username:hash, to append to a .htpasswd file. FlashKit’s generator creates {SHA} (Base64 SHA-1) or a plain-text line. It does not create bcrypt, APR1-MD5, or crypt hashes.

Should I use bcrypt or SHA-1?

Use bcrypt ($2y$) via htpasswd -B when the server’s crypt/APR stack supports it. SHA-1 {SHA} is what many shared Apache hosts still accept; it is not a 2026 recommendation for anything sensitive. Plain text is only for a local experiment you will delete.

Why do I get HTTP 500 after enabling Basic Auth?

Usually AuthUserFile / auth_basic_user_file points at a missing or unreadable path, or Apache was not allowed AuthConfig in that directory. Check the error log; nginx -t catches many Nginx mistakes before reload.

Does Basic Auth work without HTTPS?

The protocol will send the Authorization header. Without TLS that header is trivially decoded (Base64, not a hash of the password on the wire). Do not use Basic Auth on cleartext HTTP for anything real.

Can I use .htpasswd on Netlify or Vercel?

Those hosts do not apply Apache .htaccess / .htpasswd. Use the platform’s dashboard auth or a function-level check. The htaccess generator will not change that.

Related Tools