How to Password-Protect a Folder With .htpasswd on Apache or Nginx
Create an .htpasswd file with bcrypt or APR1 hashes, choose the right format for Apache or Nginx, and add the basic auth config safely over HTTPS.

HTTP basic authentication is the quickest way to keep strangers and search engines out of a staging site, an admin folder or an internal tool. The web server asks for a name and password, checks them against a small text file and either lets the request through or answers with 401 Unauthorized. The file is traditionally called .htpasswd and holds one line per user in the form user:hash.
What goes in the file
Each line has a user name, a colon and a password hash. The hash format is recognised by its prefix:
- $2y$ is bcrypt, a slow, salted hash with a cost factor. Apache recommends it.
- $apr1$ is Apache's MD5-based format, salted and run 1,000 times. Every Apache and Nginx version reads it.
- {SHA} is an unsalted SHA-1 hash, kept for old setups.
- {SSHA} is a salted SHA-1 hash that Nginx accepts and Apache does not.
- {PLAIN} stores the password as it is, which Nginx allows for testing.
Never store passwords in plain text on a real server. If the file leaks, every account is exposed.
Which format for which server
Apache 2.4 reads bcrypt, APR1 and SHA. Use bcrypt with a cost of 10 to 12.
Nginx hands the hash to the operating system's crypt function. APR1 and SHA always work. bcrypt works only where the system uses libxcrypt, which includes Alpine Linux and recent Debian, Ubuntu, Fedora and Arch releases, but not older glibc-based systems. If logins fail with a correct password on Nginx, switch the line to APR1.
Creating the file
The usual tool is the htpasswd command from the Apache utilities, which many laptops and Nginx-only servers do not have. The htpasswd Generator makes the same lines in your browser, without sending the password anywhere.
- Pick the format. Choose bcrypt and a cost, or APR1 for older Nginx systems.
- Add users. Type a user name and a password, or generate a strong random one. A strength estimate shows roughly how many bits of entropy the password has.
- Add many at once. Paste lines such as alice:password, or just names to get a generated password each. Copy the generated passwords straight away; the tool does not keep them.
- Edit an existing file. Open your current .htpasswd to add, replace or remove users while keeping everyone else's line as it is.
- Copy the server config. Set the path and the realm text and copy the block for Apache, .htaccess or Nginx.
- Download the file. Upload it to the server outside the public web folder.
The tool's APR1 output matches the lines produced by openssl passwd -apr1, and a checker lets you test whether a password matches any bcrypt, APR1, SHA or SSHA line.
Apache configuration
In the main configuration or a virtual host:
<Directory "/var/www/html/private">
AuthType Basic
AuthName "Restricted area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
If you cannot edit the main configuration, the same four lines from AuthType down can go in an .htaccess file inside the folder, provided the host allows AuthConfig overrides.
Nginx configuration
Inside a server block:
location /private/ {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Reload Nginx after saving. Test with a private browser window so cached credentials do not hide a mistake.
Keep it safe
- Use HTTPS. Basic authentication sends the user name and password, only base64-encoded, with every request. Without TLS, anyone on the network can read them.
- Keep the file out of the web root. A file inside the public folder may be downloadable if the server is misconfigured.
- Restrict permissions. The web server user should be able to read the file and nobody else, for example mode 640 with owner root and group www-data.
- Choose a sensible cost. Basic auth checks the password on every request. A bcrypt cost of 10 takes a few tens of milliseconds; going above 12 can slow pages noticeably.
- Remember the 72-byte limit. bcrypt ignores anything past the first 72 bytes of a password.
When basic auth is not enough
Basic authentication has no lockout after failed attempts, no two-factor login and no proper logout. It is ideal for keeping a staging site private or adding a second lock in front of an admin panel. For customer accounts or anything holding personal data, use the application's own login with rate limiting and two-factor authentication, and treat basic auth as an extra layer rather than the only one.



