Blog

Create an .htpasswd File Without the htpasswd Command

Servers with only Nginx have no htpasswd command. Build .htpasswd lines and config snippets in the browser, tested on Nginx 1.24 on Ubuntu 24.04: choosing bcrypt or APR1, adding users and changing passwords, and telling a 401 from a 500 when login fails.

Result screen of the .htpasswd Generator & Checker. Two lines for editor and client are output as bcrypt hashes starting with $2y$10$. The table below shows bcrypt as Yes for Apache, Caddy and Traefik and Depends on OS crypt() for Nginx. The Nginx tab is selected, with a warning that bcrypt may not be readable depending on the OS crypt().
Two users created with bcrypt. Whether Nginx can read them depends on the OS, so the table and a warning point it out.
Contents

You want to show a client a site before it goes live, so you need a password on the staging environment only. Every guide says to run htpasswd -c /etc/nginx/.htpasswd user. On a server with only Nginx installed, though, that returns htpasswd: command not found. htpasswd is a utility that comes with Apache, packaged as apache2-utils on Debian and Ubuntu and httpd-tools on RHEL-based systems. So you end up either installing Apache packages just for Basic authentication or looking up how openssl passwd works all over again.

The .htpasswd Generator & Checker builds that one line in the browser. From a username and password, it produces a line in the same format as the htpasswd command, plus config snippets to paste into Apache, Nginx, Caddy and Traefik. It can also load an existing .htpasswd to add users, or check whether a password is the right one. Below, I used the tool, put the resulting lines on Nginx 1.24 on Ubuntu 24.04, and confirmed that logging in actually works.

Create an .htpasswd for two users and put it on Nginx

As an example, register two users on the staging environment: editor for the person building the site and client for the customer who will review it.

  1. Open the tool and type editor in “Username” under “Enter users and passwords”.
  2. Press “Add user” to add another pair of fields, and type client in the second “Username”.
  3. Press “Generate” for each. A hard-to-guess 20-character password is filled in and shown as is. Use “Copy” to keep the ones you need to share.
  4. Leave “Hash format” at “bcrypt (recommended, htpasswd -B)” and “bcrypt cost” at “10 (recommended)”.
  5. Press “Create .htpasswd”. When “Created 2 line(s).” appears below, you are done.
The tool screen. Under Enter users and passwords, two pairs with the usernames editor and client are filled in, and each password field shows a 20-character password made with Generate. Hash format is bcrypt (recommended, htpasswd -B) and bcrypt cost is 10 (recommended). Below Create .htpasswd, “Created 2 line(s).” is shown.
Passwords disappear when you close the page, so note down the generated ones here.

The .htpasswd content and config snippets appear under “Your .htpasswd and config examples”.

The tool screen. The .htpasswd content shows two lines for editor and client with hashes starting with $2y$10$. The table below shows bcrypt as Yes for Apache, Caddy and Traefik and Depends on OS crypt() for Nginx. The Nginx tab is selected, showing a yellow warning and a config example with auth_basic and auth_basic_user_file inside location /.
The Nginx tab warns that some OSes cannot read bcrypt.
editor:$2y$10$UdBqSn7pK3b8UrCblip2UusvfmJHs7.1/6B/QweXqrIP/olg4gke6
client:$2y$10$X9tt3KCfiZfT03DckaWTROO0r9gRaKUC1v4PF6PCDkUtU9Qjew9qC

Each line holds one user as username:hash, the same format the htpasswd command writes. The password itself is not stored in the file. $2y$ means bcrypt and the 10$ after it is the cost. Recreating a line with the same password gives a different value each time because a random salt goes in, and every one of them accepts the same password.

Then save the file with “Save .htpasswd” and put it at /etc/nginx/.htpasswd on the server. Add the content of the Nginx tab to the server block.

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

Check the syntax with nginx -t, then reload with nginx -s reload. With these two lines on my Ubuntu 24.04 (nginx 1.24.0), curl got a 200 with the right password and a 401 with a wrong one. Apache’s htpasswd -v also judged the lines from the tool “correct”.

Whether Nginx can read bcrypt depends on the OS

Only the Nginx column in the table says “Depends on OS crypt()”. Nginx itself only checks a few formats such as APR1-MD5 and {SHA}, and hands bcrypt and SHA-512 crypt to the OS crypt() function. Ubuntu 24.04 and recent Debian use libxcrypt for crypt(), which reads bcrypt’s $2y$, and it worked as is in this test. On older OSes, crypt() may not know bcrypt at all, and no password will let you in.

If you are dealing with an old server and are not sure, set “Hash format” to “APR1-MD5 (htpasswd default, widest support)”. Nginx checks APR1-MD5 by itself, so the OS does not matter, and the table shows “Yes” everywhere. The catch is that it only repeats MD5 1,000 times, which makes it weaker than bcrypt against brute force if the file leaks. Pick bcrypt where it is readable, and fall back to APR1-MD5 only when you find out it is not.

Each step up in bcrypt cost doubles the computation. htpasswd’s -B uses cost 5 when nothing is specified (it produced $2y$05$ in this environment too). The tool defaults to 10, a balance between resistance to brute force and the server load on every login. Going to 13 or more on a busy site makes the computation on each login a noticeable load.

Add users and change passwords

Once the staging site is in use, requests like “add one designer” or “change editor’s password” come up. Rebuilding .htpasswd from scratch means handing out everyone else’s passwords again. With the tool, you can load the current file and replace only the lines that change.

Suppose the server has the file below. admin was made with APR1-MD5, editor with bcrypt from the htpasswd command (cost 5), and legacy with the old 13-character DES crypt.

# staging users
admin:$apr1$LjeVd2Kj$pYeaR5BUNHFadwNTArMqZ/
editor:$2y$05$p4O4l.B/FAlBGLTGrfBmkOXobDlNrGlD8243MdncdJOFccfQwQtRa
legacy:77v6rgy6gozN.
  1. Open “Add to or edit an existing .htpasswd (optional)” and paste the file into “Current .htpasswd content” (or pick the file with “Open file”). “Loaded 3 user(s) (1 comment line(s)).” appears along with a table of users.
  2. In “Username” above, enter editor, whose password changes, and designer, who is new, and press “Generate” for each.
  3. The “Notes” for editor in the table says “Replaced by the line created above”.
  4. The “Notes” for legacy says “Legacy format that uses only the first 8 characters”. That user is no longer needed, so press “Remove”. The row is struck through and the button changes to “Undo”.
  5. Press “Create .htpasswd”.
The tool screen. Current .htpasswd content holds a comment line and three lines for admin, editor and legacy. In the table, admin is APR1-MD5, editor is bcrypt with the note Replaced by the line created above, and legacy is DES crypt, struck through, with its button changed to Undo. The resulting .htpasswd content has four lines: the comment, admin’s original line, a new $2y$10$ line for editor, and the added designer line.
admin’s line is kept with its hash, editor gets the new line, and designer is appended.

In the result, the comment and admin’s line stay as they were, editor is replaced by a new $2y$10$ line, and designer is added at the end. admin’s line is carried over without touching the hash, so whoever does this does not need to know admin’s password. With this file on Nginx, admin, the new editor and designer got 200, while editor’s old password and the removed legacy got 401.

legacy gets a warning because DES crypt only uses the first 8 characters of a password. A line made from oldpass1 also accepts passwords like oldpass1-anything that differ only from the 9th character on. The tool does not check these lines either, so recreate any that remain.

When login fails, first find out whether the password is the problem

When you have set everything up but the login prompt keeps coming back, the cause is usually one of four: the password is remembered wrong, the file is not what you think it is, the server cannot read the file, or the server cannot read the format. The tool’s checker separates out the first two.

Under “Check whether a password matches” at the bottom of the page, check an .htpasswd line against the password you remember. If you loaded a file above, picking a user in “Pick from the loaded file” fills in the line. Here I picked admin, entered river-lamp-0417-cobalt in “Password” and pressed “Check”.

The tool screen. Under Check whether a password matches, Line to check holds admin’s $apr1$ line, Pick from the loaded file is admin (APR1-MD5), and Password is river-lamp-0417-cobalt. Below Check, green text reads “Match. This is the correct password for “admin” (APR1-MD5).”
A match is shown in green as “Match.”

“Match.” means the line and password belong together. If even one character is off, such as Cobalt instead of cobalt at the end, you get “No match.” If they match and you still cannot log in, the cause is on the server, not the password.

On Nginx, server-side causes show up plainly in the error log (/var/log/nginx/error.log). I made it fail on purpose on my Nginx and compared the logs.

What the browser shows Error log Cause and fix
Login prompt keeps coming back (401) user "editor": password mismatch Wrong password or hash. Check it in the tool and recreate it if it does not match
Login prompt keeps coming back (401) user "editor" was not found in "/etc/nginx/.htpasswd" Username typo. If only the first line of the file fails, suspect a BOM at the start
“500 Internal Server Error” open() "…/.htpasswd" failed (13: Permission denied) The Nginx worker (www-data on Ubuntu) cannot read the file. Fix the owner and permissions

The BOM in the second row is easy to miss. If you save the file as “UTF-8 with BOM” in a Windows editor, three invisible bytes are added at the start. When I tried a file with a BOM, client on the second line got in, but only editor on the first line was “was not found”. Nginx reads the first three bytes as part of the username, so as far as it is concerned there is no user named editor. Loading the file into the tool’s “Current .htpasswd content” strips the BOM, so saving the rebuilt file fixes it. Windows CRLF line endings on their own, on the other hand, worked fine on this Nginx.

The third row often happens when you create .htpasswd in your home directory and copy it with chmod 600 still set. When the worker cannot read it, Nginx returns a 500 instead of an authentication failure. If you see a 500 rather than a 401, you do not need to suspect the password.

Caddy and Traefik put the hash somewhere else

With Caddy and Traefik, you normally write the hash directly into the config instead of placing an .htpasswd file. Switching tabs in the tool shows the right syntax for each. Two things need attention.

The first is how few formats Caddy reads. Caddy’s basic_auth accepts only bcrypt and argon2id (made with Caddy’s own command), not APR1-MD5. If you reuse lines made with APR1-MD5 for Nginx, the Caddy column in the tool’s table turns “No” and a warning tells you to recreate them.

The tool screen. Two lines made with Hash format set to APR1-MD5 are shown in the .htpasswd content. For APR1-MD5 the table shows Yes for Apache, Nginx and Traefik and No in red for Caddy. The Caddy tab is selected, with the warning “Caddy’s basic_auth only supports bcrypt (and argon2id). Switch the format to bcrypt and create it again.” and a config example listing editor and client with their $apr1$ hashes in a basic_auth block.
Trying to use APR1-MD5 lines with Caddy triggers the table and a warning.

The second is $ when you configure Traefik through Docker Compose labels. Compose reads $ as the start of an environment variable, so a hash like $2y$10$… breaks if left as is. The “Traefik (Docker labels)” tab outputs it with each $ doubled to $$.

labels:
  - "traefik.http.middlewares.basic-auth.basicauth.users=editor:$$2y$$10$$UdBqSn7pK3b8UrCblip2UusvfmJHs7.1/6B/QweXqrIP/olg4gke6,client:$$2y$$10$$X9tt3KCfiZfT03DckaWTROO0r9gRaKUC1v4PF6PCDkUtU9Qjew9qC"
  - "traefik.http.middlewares.basic-auth.basicauth.realm=Restricted"

Join multiple users with commas. In Traefik’s dynamic configuration file (YAML), $ is not doubled, so use the “Traefik (file)” tab instead.

What Basic authentication can and cannot do

With Basic authentication, the browser sends the username and password on every request, only Base64-encoded. Nothing is encrypted, so on a site without HTTPS they can be read in transit. Put a certificate on the staging environment before you use it.

Basic authentication also has no attempt limit and no logout. It is enough for putting a single “staff only” door on a pre-launch site or an internal admin page, but it does not suit pages that need per-user permissions or two-factor authentication. With a password of about 20 characters from “Generate”, there is practically no risk of it being cracked by brute force. How length relates to strength is worked out in How long should a password be?.

The tool handles both the passwords and the file content inside your browser and sends them nowhere. Instead of installing htpasswd on the server, you can build the lines in your browser, put the file in place, and Basic authentication is ready.

Frequently asked questions

How do I create an .htpasswd file on a server without the htpasswd command?

htpasswd ships with Apache in apache2-utils (httpd-tools on RHEL-based systems), so servers with only Nginx often lack it. Enter a username and password in a browser-based .htpasswd generator, save the file and upload it to the server. No package needs to be installed.

Should .htpasswd hashes use bcrypt or APR1-MD5?

Use bcrypt for Apache, Caddy, Traefik and Nginx on recent Debian or Ubuntu. Caddy cannot read APR1-MD5. If bcrypt does not work with Nginx on an older OS, use APR1-MD5 or SHA-512 crypt.

Why can't I log in with Basic authentication even though the password is right?

On Nginx, check the error log first. password mismatch means the password or hash is wrong, was not found means a wrong username or a BOM at the start of the file, and a 500 with Permission denied means the web server user cannot read the file.