Protect your apache directory

Try Fast Read

We can use htpasswd to keep unauthorized user from visiting a directory in your server. Most of the cpanel allows this feature, but if you do not have a cpanel, but you have a ssh login, then you can use it to password protect your directory. This will make the web directory prompt for password when visited via url. If you want to protect your apache directory, you need to create .htaccess file and .htpasswd file

Steps to protect your apache directory

Requirements: SSH login with a user that has access to create a file on the server or you can also use this step to protect your apache directory on the local computer.

  • Go to the folder which you want to protect
  • create/modify the .htaccess file on your favorite text editor, vim, nano
    vim .htaccess
  • Add the following
    AuthUserFile "path_to_.htpasswd"
    AuthType Basic
    AuthName "user_name"
    require valid-user
  • Quite vim by pressing .
  • Now run the following to generate .htpasswd file
     htpasswd -c path_to_create_.htpasswd username

Now after you do this, try and go to the directory via url for example http://yourservername/protected directory. It will promopt you for username and password.

Post navigation