Home » Server » How to create a password-protected folder on your Linux server hosting

How to create a password-protected folder on your Linux server hosting



Sometimes you want to keep your files on your Linux hosting server but don’t want anybody else to access them. You want to create a password protected folder on your remote server. You can achieve that using .htaccess and .htpasswd files.

First of all create the folder you want password protected on your remote server. After this, on your local PC, using some basic text editor (not something like MS Word!), create a blank file called .htpasswd. Sometimes your PC doesn’t allow you to create files starting with a dot. Worry not. Name the files as mypass.txt and htaccess.txt and save them.

Now you’ll need to store the user name and password in this file, but the password must be encrypted. Use this password encryption tool to generate the needed string. For instance, you may get something like:

admin:$1$vUQSCn4S$tbFUx85XKJ1x3GPmpsbCF1

where admin is the login and the cryptic string is the password. Save the whole thing in your mypass.txt file (let’s assume you couldn’t create .htpasswd for everybody’s benefit). Upload this file to your root folder using your preferred FTP program (where your main index.html or index.php file resides).

Then create a blank htaccess.txt and put the following information in that:

AuthUserFile /home/<user_name>/html/.htpasswd
AuthGroupFile /dev/null
AuthName "Secure Area"
AuthType Basic
require valid-user

The first line needs the path to your root/home directory. If somehow you are unable to find the correct path, create a PHP file, something like showpath.php and put in it the following code:

<?
echo __FILE__;
?>

Upload this file and run it. This should show you the correct path of your root folder.

Save your htaccess.txt file and upload it into the folder you want password-protected.

On the remote server, rename htpasswd.txt to .htpasswd and htaccess.txt to .htaccess.

If the password you created was personal, if you type

http://yourdomain.com/personal

a window will prompt you to enter your login and password.