Htpasswd generator

Create the lines for a .htpasswd file to password-protect a site or folder with Apache or Nginx basic authentication. Passwords are hashed in your browser and never sent anywhere.

.htpasswd

Server config to go with it


How to use the htpasswd generator

  1. Enter a username and password, or click Random Password.
  2. Click Add User. Repeat for each person who needs access.
  3. Copy the result into a file named .htpasswd on your server and add the config shown below it.

Which hash type should I choose?

TypeLooks likeSecurityWorks with
bcrypt$2y$10$…Strong. Slow on purpose, so guessing is expensiveApache 2.4 and later. Nginx on most Linux systems
APR1-MD5$apr1$…AcceptableEvery Apache and Nginx version
SHA-1{SHA}…Weak. No salt, cracked quicklyOld systems only

Start with bcrypt. If Nginx rejects the login (some builds on Alpine or BSD lack bcrypt support), switch to APR1-MD5.

Where to put the file

Store .htpasswd outside the folder your website is served from, for example /etc/nginx/.htpasswd or /etc/apache2/.htpasswd, so nobody can download it. Make it readable by the web server only:

sudo chown root:www-data /etc/nginx/.htpasswd
sudo chmod 640 /etc/nginx/.htpasswd

With SWAG, save it as /config/nginx/.htpasswd and uncomment the two auth_basic lines in the proxy conf.

Basic auth needs HTTPS

Basic authentication sends the username and password with every request, only lightly encoded. Over plain HTTP anyone on the network can read them. Always use it together with HTTPS. The Nginx config generator can write the HTTPS server block for you.

The command-line way

htpasswd -B -c /etc/nginx/.htpasswd admin   # create the file with a bcrypt entry
htpasswd -B /etc/nginx/.htpasswd anna       # add another user

The htpasswd command is in the apache2-utils package on Debian and Ubuntu, and httpd-tools on RHEL and Fedora.

Frequently asked questions

What is an .htpasswd file?

A text file with one line per user in the form username:hashed-password. Apache and Nginx read it to check logins for basic authentication.

Is my password sent to your server?

No. The hash is calculated in your browser. Nothing is uploaded or stored.

Why is the hash different every time?

bcrypt and APR1 add a random salt, so the same password gives a different line each time. Every one of them is valid.

Does Nginx support bcrypt in htpasswd?

Nginx relies on the operating system’s crypt function. On most Linux distributions with glibc, bcrypt works. If it does not, use APR1-MD5.