How to use the htpasswd generator
- Enter a username and password, or click Random Password.
- Click Add User. Repeat for each person who needs access.
- Copy the result into a file named
.htpasswdon your server and add the config shown below it.
Which hash type should I choose?
| Type | Looks like | Security | Works with |
|---|---|---|---|
| bcrypt | $2y$10$… | Strong. Slow on purpose, so guessing is expensive | Apache 2.4 and later. Nginx on most Linux systems |
| APR1-MD5 | $apr1$… | Acceptable | Every Apache and Nginx version |
| SHA-1 | {SHA}… | Weak. No salt, cracked quickly | Old 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.