User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

password_protect [2022/02/05 14:13] – created normanpassword_protect [2022/02/05 14:23] (current) norman
Line 10: Line 10:
 Run pwd to confirm the full file path to this directory. You'll need this full path in the next step. Run pwd to confirm the full file path to this directory. You'll need this full path in the next step.
  
-<code>[server]$ pwd +<cli>[server]$ pwd 
-/home/username/example.com</code>+/home/username/example.com</cli>
  
 Create an .htpasswd file by running the following command in the directory you wish to password protect. This uses the htpasswd utility and the full path to the directory. For example, if the username you're creating to log in is user1, run the following: Create an .htpasswd file by running the following command in the directory you wish to password protect. This uses the htpasswd utility and the full path to the directory. For example, if the username you're creating to log in is user1, run the following:
  
-<code>[server]$ htpasswd -c /home/username/example.com/.htpasswd user1</code>+<cli>[server]$ htpasswd -c /home/username/example.com/.htpasswd user1</cli>
  
-Enter a password for the new user named user1. The code in your .htpasswd file then displays the encrypted password like this:+Enter a password for the new user named user1. The cli in your .htpasswd file then displays the encrypted password like this:
  
-<code>nano ./htpasswd +<cli>nano ./htpasswd 
-user1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2</code>+user1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2</cli>
  
        Run it again (without the -c option) for any other users you wish to allow access to your directory.        Run it again (without the -c option) for any other users you wish to allow access to your directory.
Line 26: Line 26:
 Confirm the permissions are set to 644 by running the following command. Confirm the permissions are set to 644 by running the following command.
  
-<code>[server]$ chmod 644 .htpasswd</code>+<cli>[server]$ chmod 644 .htpasswd</cli>
  
 ===== Step 2 — Create the .htaccess file ===== ===== Step 2 — Create the .htaccess file =====
Line 34: Line 34:
  
  
-<code>[server]$ nano .htaccess</code>+<cli>[server]$ nano .htaccess</cli>
  
 ======  ====== 
-Code examples to add to the .htaccess file+cli examples to add to the .htaccess file
 Protect an entire website Protect an entire website
 This example password protects an entire website by placing the files in the site's main web directory. ====== This example password protects an entire website by placing the files in the site's main web directory. ======
Line 43: Line 43:
         Make sure to change the highlighted lines to your username, domain name, and specific file names.         Make sure to change the highlighted lines to your username, domain name, and specific file names.
                  
- <code>#Protect Directory+ <cli>#Protect Directory
 AuthName "Dialog prompt" AuthName "Dialog prompt"
 AuthType Basic AuthType Basic
 AuthUserFile /home/username/example.com/.htpasswd AuthUserFile /home/username/example.com/.htpasswd
-Require valid-user</code>+Require valid-user</cli>
  
 Protect a single file Protect a single file
 This example password protects a single file named admin.php: This example password protects a single file named admin.php:
  
-<code>#Protect single file+<cli>#Protect single file
 <Files admin.php> <Files admin.php>
 AuthName "Dialog prompt" AuthName "Dialog prompt"
Line 58: Line 58:
 AuthUserFile /home/username/example.com/.htpasswd AuthUserFile /home/username/example.com/.htpasswd
 Require valid-user Require valid-user
-</Files><code>+</Files><cli>
  
 Protect multiple files Protect multiple files
 This example protects multiple files such as admin.php and staff.php. This example protects multiple files such as admin.php and staff.php.
  
-<code>#Protect multiple files+<cli>#Protect multiple files
 <FilesMatch "^(admin|staff).php$"> <FilesMatch "^(admin|staff).php$">
 AuthName "Dialog prompt" AuthName "Dialog prompt"
Line 69: Line 69:
 AuthUserFile /home/username/example.com/.htpasswd AuthUserFile /home/username/example.com/.htpasswd
 Require valid-user Require valid-user
-</FilesMatch></code>+</FilesMatch></cli>
  
 Rest can read on https://help.dreamhost.com/hc/en-us/articles/216363187-Password-protecting-your-site-with-an-htaccess-file Rest can read on https://help.dreamhost.com/hc/en-us/articles/216363187-Password-protecting-your-site-with-an-htaccess-file