Add password to website using apache web server

Adding password to a website or web directory using Apache web server is easy, for CentOS and other RedHat-based distro, edit your /etc/httpd/conf/httpd.conf and find the line AllowOverride None under <Directory "/var/www/html"> and replace with AllowOverride All.

Then after this line Allow from all add these following lines:

AuthName "Login Message Here"
AuthType Basic
AuthUserFile /var/www/html/htpasswd.users
Require valid-user

Your apache config file should look like this:

  1. <Directory "/var/www/html">
  2.  
  3. #
  4. # Possible values for the Options directive are "None", "All",
  5. # or any combination of:
  6. #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
  7. #
  8. # Note that "MultiViews" must be named *explicitly* --- "Options All"
  9. # doesn't give it to you.
  10. #
  11. # The Options directive is both complicated and important.  Please see
  12. # http://httpd.apache.org/docs-2.0/mod/core.html#options
  13. # for more information.
  14. #
  15.     Options Indexes FollowSymLinks
  16.  
  17. #
  18. # AllowOverride controls what directives may be placed in .htaccess files.
  19. # It can be "All", "None", or any combination of the keywords:
  20. #   Options FileInfo AuthConfig Limit
  21.     AllowOverride All
  22.  
  23. #
  24. # Controls who can get stuff from this server.
  25. #
  26.     Order allow,deny
  27.     Allow from all
  28.     AuthName "Login Message Here"
  29.     AuthType Basic
  30.     AuthUserFile /var/www/html/htpasswd.users
  31.     Require valid-user
  32.  
  33. </Directory>

Same configuration for Debian Linux, edit the file /etc/apache2/sites-enabled/000-default.

vi /etc/apache2/sites-enabled/000-default

Find the line AllowOverride None under the <Directory /var/www/> and change to AllowOverride All.

Add these following lines:

AuthName "Login Message Here"
AuthType Basic
AuthUserFile /var/www/html/htpasswd.users
Require valid-user

after the line that says allow from all.

  1. <Directory /var/www/>
  2.        Options Indexes FollowSymLinks MultiViews
  3.        AllowOverride All
  4.        Order allow,deny
  5.        allow from all
  6.        AuthName "Login Message Here"
  7.        AuthType Basic
  8.        AuthUserFile /var/www/htpasswd.users
  9.        Require valid-user
  10.        # This directive allows us to have apache2's default start page
  11.        # in /apache2-default/, but still have / go to the right place
  12.        #RedirectMatch ^/$ /apache2-default/
  13. </Directory>

Create the password file htpasswd.users file, your password will be stored in this file. To create a password for your username, type this command in the console.

cd /var/www/html

or for Debian

cd /var/www
htpasswd -c htpasswd.users username

You can also use the .htaccess file to protect your web directory, just create a .htaccess file inside the web directory you want to secure and put these following lines:

AuthName "Login Message Here"
AuthType Basic
AuthUserFile /var/www/html/htpasswd.users
Require valid-user

After you have created the .htaccess file, create a password for your username, just type this command:

htpasswd -c htpasswd.users username

This will require your web site visitors to login with a user id and password. If they failed to enter the specified username and password, the browser will display an error message.

That's how it works... Cheers!!!

No votes yet

Comments

Anonymous's picture

I'm trying to open forum but sometimes there are no images on it :(

Anonymous's picture

[...] any authentication. If you want to put an authentication to your Asterisk-stat, just follow this howto. It will require web site visitors to login with a user id and [...]

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may quote other posts using [quote] tags.

More information about formatting options