What is an .htpasswd file?
An .htpasswd file stores usernames and hashed passwords for HTTP Basic Authentication — the browser pop-up that asks for a username and password before showing a page. Each line has the form username:hash. It is the quickest way to protect a staging site, an admin folder or an internal tool.
Which algorithm to choose
- bcrypt (
$2y$) — the strongest option. Supported by Apache 2.4+ and by Nginx on systems whosecrypt()supports it (most modern Linux distributions). - APR1-MD5 (
$apr1$) — Apache’s historical default and universally supported by Nginx. Salted, but much faster to crack than bcrypt. - SHA-1 (
{SHA}) — unsalted and fast; use only for compatibility with very old software.
Apache example
Upload the file outside your web root, for example /etc/apache2/.htpasswd, then add this to the site configuration or a .htaccess file in the folder you want to protect:
AuthType Basic
AuthName "Restricted area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
The command-line equivalent of this tool is htpasswd -B -c /etc/apache2/.htpasswd admin (-B selects bcrypt).
Nginx example
location /admin/ {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Reload with sudo nginx -s reload. If bcrypt lines are rejected on your system, switch to APR1.
Security notes
- Basic Auth sends the password with every request — always use HTTPS.
- Store the file outside the public document root and block access to dot-files.
- Use a long random password; the Generate strong button creates a 20-character one without characters that cause trouble in shells.
- Cloudflare users can protect a whole site without a server file using Cloudflare Access.
Last updated: · Reviewed by the Password Generator team