Password-Protect a WordPress Staging Site With .htpasswd

Written by

in

2 views

To password-protect a WordPress staging site, create a .htpasswd file that holds a username and a hashed password, then point the site’s .htaccess at it. Browsers then show a login prompt before any page loads. This is HTTP Basic authentication. It works at the web server, so it covers every URL under the folder, including wp-login.php, the REST API and static files, and it needs no plugin. WP Aligned runs LiteSpeed, which reads these .htaccess directives the same way Apache does. The whole job takes about five minutes.

This guide covers doing it by hand. If you’d rather use cPanel’s Directory Privacy screen, that screen writes the same two files for you.

What you need

  • cPanel access to the account that hosts the staging site.
  • The site’s document root. In cPanel, go to Domains > Domains and read the Document Root column. The examples use /home/exampleuser/staging.example.com. Replace exampleuser and the folder with your own.
  • A username and a strong password. Use a password you generate for this site, not one you use anywhere else, because you’ll share it with clients and contractors.

Step 1: Generate the password hash

.htpasswd never stores the password itself. Each line is username:hash. Use bcrypt. Its hashes start with $2y$, and it’s deliberately slow to compute, which makes a stolen file much harder to crack than the older Apache MD5 (APR1) format. WP Aligned’s LiteSpeed servers accept bcrypt hashes in .htpasswd.

Option A: In the terminal (cPanel Terminal or SSH)

Open Advanced > Terminal in cPanel, or SSH in. This command asks for the password without showing it, then prints the finished username:hash line. Replace deploy with your username:

read -rsp 'Password: ' PW; echo
printf 'deploy:%s\n' "$(PW="$PW" /usr/local/bin/php -r 'echo password_hash(getenv("PW"), PASSWORD_BCRYPT);')"

Example output:

deploy:$2y$10$h9/8aiotS7odVihLBajXe.S2cg7pdKAiq68UcrRMh3sN5MYaEvlgK

The characters after $2y$10$ include a random salt, so you get a different hash every time, even for the same password. That’s normal, and any of them works.

Why this form: PHP’s password_hash() ships with every PHP 8.x on the server, so there’s nothing to install. Reading the password with read -s keeps it out of your shell history and off the screen. Don’t type the password inside the command itself.

If htpasswd is installed on your account, htpasswd -nB deploy does the same job. It isn’t on every account. Check with command -v htpasswd, and use the PHP command if it prints nothing.

Option B: In a web-based generator

If you’d rather not use a terminal, use the WP Aligned .htpasswd generator. Enter a username and password, choose bcrypt, click Generate, and copy the username:hash line it produces.

The generator runs entirely in your browser. The password is never sent anywhere, logged or stored. That’s the property to look for in any online generator, because you’re typing a password into it. If you use a different one, don’t enter a password you reuse anywhere else. If you’re unsure, use Option A.

Option C: In cPanel’s Directory Privacy

Files > Directory Privacy creates the hash and the .htpasswd file for you. See Alternative: cPanel Directory Privacy below.

If you can’t use bcrypt

Older tools and some other servers only understand APR1 (Apache MD5), whose hashes start with $apr1$. LiteSpeed accepts these too. Generate one with:

printf 'deploy:%s\n' "$(openssl passwd -apr1)"

Step 2: Create the .htpasswd file

Store the file outside the document root, in your home directory, so the web server never serves it as a page.

  1. In cPanel, go to Files > File Manager.
  2. Open your home directory (/home/exampleuser). Don’t open public_html or any site folder.
  3. Click + File, name it .htpasswd-staging, and click Create New File.
  4. Right-click the file, choose Edit, and paste the line from Step 1:

text
deploy:$2y$10$h9/8aiotS7odVihLBajXe.S2cg7pdKAiq68UcrRMh3sN5MYaEvlgK

  1. Click Save Changes.

cPanel File Manager showing the home directory with the .htpasswd-staging file selected, next to the public_html folder and not inside it

For more than one login, put one username:hash line per person. Each line needs its own hash.

If you used the terminal, you can create the file there instead:

read -rsp 'Password: ' PW; echo
printf 'deploy:%s\n' "$(PW="$PW" /usr/local/bin/php -r 'echo password_hash(getenv("PW"), PASSWORD_BCRYPT);')" > ~/.htpasswd-staging
chmod 644 ~/.htpasswd-staging

Use >> instead of > to add a second user without overwriting the first.

File permissions matter

Set the file to 644. On our LiteSpeed servers, a .htpasswd file set to 600 can’t be read by the web server, so the correct password gets rejected and you see the login prompt again and again. If you created the file in File Manager, check it: select the file, click Permissions, and confirm the file is 0644.

Step 3: Add the auth rules to .htaccess

  1. In File Manager, open the staging site’s document root and enable Settings > Show Hidden Files (dotfiles).
  2. Right-click .htaccess and choose Edit. If there’s no .htaccess, create one.
  3. Add these lines at the very top of the file, above # BEGIN WordPress:

apache
AuthType Basic
AuthName "Staging"
AuthUserFile /home/exampleuser/.htpasswd-staging
Require valid-user

  1. Click Save Changes.

What each line does:

  • AuthType Basic turns on browser login prompts.
  • AuthName is the text some browsers show in the prompt. It doesn’t have to match anything.
  • AuthUserFile is the full path to the .htpasswd file. A relative path is the most common mistake, and it fails silently.
  • Require valid-user allows any username in the file. To allow a single user, use Require user deploy.

Keep those four lines above the WordPress block. WordPress rewrites everything between # BEGIN WordPress and # END WordPress whenever permalinks are saved, and it would remove anything you put inside.

Step 4: Test it

  1. Open the staging URL in a private browser window. You should get a login prompt before the page loads.
  2. Enter a wrong password. The prompt should reappear.
  3. Enter the right one. The site should load.

You can also test from a terminal. A request with no login should return 401, and a request with the right login should return 200:

curl -I https://staging.example.com/
curl -I -u deploy https://staging.example.com/

Use your own staging URL and username. curl asks for the password when you give -u a username without one. A 401 response carries a www-authenticate: Basic realm="Staging" header. If you see that header, the rules are active and the server is working.

If the prompt doesn’t appear, or the password is rejected

What you see Likely cause Fix
The page loads with no prompt The rules are not active Confirm the four lines are in the .htaccess of the folder that serves this site, and that the file saved. Clear the LiteSpeed cache, then retest in a private window.
The prompt keeps coming back with the right password The web server can’t read .htpasswd, or the hash is malformed Set the file to 644. Check each line is username:hash with no spaces, and that the hash starts with $2y$ or $apr1$.
The prompt appears but no password works AuthUserFile points to a path that doesn’t exist Fix the path. Check the spelling and your cPanel username.
403 Forbidden A different rule is blocking the request, such as a firewall or CDN rule Check Cloudflare or other security rules for the hostname. An HTTP auth prompt is a 401, not a 403.
The login worked yesterday and doesn’t today The setting was changed but never saved Reopen .htaccess and .htpasswd, confirm the contents, and save again.

In File Manager, the editor doesn’t save until you click Save Changes. Closing the window with unsaved edits leaves the old version in place, and that’s an easy way to end up with a half-configured site.

If the page still loads without a prompt after a cache purge, see Clear the LiteSpeed Cache.

Things that break when the whole site is protected

Basic auth is checked on every request, including requests WordPress makes to itself:

  • Site Health loopback and REST checks can report errors, because the server’s own requests don’t carry your login.
  • Webhooks and external services that call the staging site, such as payment or form services, get a 401 unless they send the login.
  • Search engines are blocked, which is usually what you want on staging. Also leave Settings > Reading > Discourage search engines ticked.

If a service has to reach the staging site, expect to rework or pause its integration while the protection is on.

Remove or change the protection

  • To change a password: generate a new hash, then replace that user’s line in .htpasswd-staging.
  • To remove a user: delete their line.
  • To remove the protection: delete the four lines from .htaccess. You can leave the .htpasswd file, but deleting it is tidier.

Protection doesn’t move with a site. If you move staging to live, or copy the staging .htaccess onto a live site, remove the auth rules first. Otherwise visitors get a login prompt on the live site.

Alternative: cPanel Directory Privacy

If you only need to lock one folder:

  1. In cPanel, go to Files > Directory Privacy.
  2. Click the folder name, or Edit next to the document root.
  3. Tick Password protect this directory and enter a name for it.
  4. Click Save.
  5. Under Create User, enter a username and password and click Save.

cPanel stores the hash in a password file in your home directory and adds the matching lines to the folder’s .htaccess. Every change on that screen needs its own Save. It’s the same kind of setting that’s easy to configure but never save, so reload the screen to confirm the change took.

FAQ

Is HTTP auth secure enough for a staging site?

It keeps out visitors, bots and search engines, which is the job on a staging site. Always use it over HTTPS. Basic auth sends the username and password with every request, and only HTTPS keeps them from being read in transit. It’s not a substitute for WordPress user security on the live site.

Why bcrypt and not APR1?

Both work on our servers. bcrypt is slower to brute-force if the file ever leaks, so it’s the better default. Use APR1 only when a tool or server in your workflow can’t read bcrypt.

Need a hand?

If the prompt still doesn’t behave after these checks, open a ticket with the site URL and what you see. WP Aligned support can check the rules from the server side.