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.
/home/exampleuser/staging.example.com. Replace exampleuser and the folder with your own..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.
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
htpasswdis installed on your account,htpasswd -nB deploydoes the same job. It isn't on every account. Check withcommand -v htpasswd, and use the PHP command if it prints nothing.
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.
Files > Directory Privacy creates the hash and the .htpasswd file for you. See Alternative: cPanel Directory Privacy below.
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)"
Store the file outside the document root, in your home directory, so the web server never serves it as a page.
/home/exampleuser). Don't open public_html or any site folder..htpasswd-staging, and click Create New File.text
deploy:$2y$10$h9/8aiotS7odVihLBajXe.S2cg7pdKAiq68UcrRMh3sN5MYaEvlgK
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.
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.
.htaccess and choose Edit. If there's no .htaccess, create one.# BEGIN WordPress:apache
AuthType Basic
AuthName "Staging"
AuthUserFile /home/exampleuser/.htpasswd-staging
Require valid-user
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.
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.
| 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.
Basic auth is checked on every request, including requests WordPress makes to itself:
If a service has to reach the staging site, expect to rework or pause its integration while the protection is on.
.htpasswd-staging..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.
If you only need to lock one folder:
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.
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.
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.
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.