Password Protect Your Webserver Pages

September 9, 2011

              Password Protect Your Webserver Pages 
 
  
M
anaging user access to a Linux Intranet server is painless and quick as long as you know the basics of .htaccess files.
Yet, I find some new administrators get frustrated by the lack of complete information on the internet. In some HOWTO guides, details regarding .htaccess are given, without mentioning that Apache must be configured and offering solutions. My hope is that this brief article gives a complete look at managing access with the use of Apache overrides and .htaccess. Since the goal is to make it as easy as pie, I will cut the techno talk and get to the points.
 

First, although this article applies to just about any Apache/Linux web server, it is most beneficial in the context of Intranet servers or when a website needs to control or secure certain web pages within directories for a set of specific users.

Second, you do not have to be a Linux administrator or expert to do these steps. You only need rudimentary knowledge of Linux, have root or super user access to the server, and be familiar with a text editor like vi, emacs, or something like kwrite. You can find good text editor help here.


Step 1. Configure Apache to Allow Access Authorization

You need to find the httpd.conf file on your Linux server. 

This file is the Apache web server configuration file that includes lots of very useful Apache web server controls. 

For now, simply open it using a text editor. For Fedora users this is done by simply going to /etc/httpd and opening httpd.conf. For others using various flavors, try using this command to identify the location of the httpd.conf file and edit it: locate httpd.conf

Once you open this file using a text editor, please scroll down until you see not the first but the second occurance of this text:   AllowOverride None

Change the line that says: 
AllowOverride None 
to instead say: 
AllowOverride AuthConfig

Be sure to NOT CHANGE THE first occurance of this in the apache file which is the default. Change the secondoccurance which is actually the overide. This is VERY IMPORTANT! If you run into trouble make a backup of your httpd.conf file (type: cp httpd.conf httpd.conf.back) and then try using this example. Be sure to reboot the server after you copy our example file.

Step 2. Identify the Folder/Directory to Protect
You should now identify which folders (aka Directories) under your web server you would like to protect. For instance if I want to only allow a certain list of users to access my html files under the Private folder it would look something like the following.

On the Linux server the actual directory path would be:
/var/www/html/Private
On the web browser the path would be:
http://office.server.com/Private

Obviously, I'm giving an example to help you see the difference between the folder/directory name on the Linux server and how it looks to web browsers. You MUST change to the appropriate directory/folder when using the steps below. So in my case I type this command first before beginning on my Fedora server:
cd /var/www/html/Private

Step 3. Add Access Files to the Folder
Once you identify the folder you wish to safeguard, then you need to create two files in this folder. The files are: .htaccess and .htpasswd. The .htaccess file displays the access login information needed for users and also includes the list of specific users who can login. The .htpasswd file includes the individual users and their passwords.

Create .htaccess file in your Folder by using a text editor to create .htaccess. Notice that you must include the . (dot) before the file name!

The file should atleast include these lines:
AuthName "Login to the Private Area"
AuthType Basic
AuthUserFile /var/www/html/Private/.htpasswd
Require user andrea 

Note that the AuthName requires quotes and whatever is in quotes will display on the login window when a user tries to access your private folder with a web browser. It is vital that you properly set the path for the AuthUserFile and obviously replace the word Private with whatever folder you are trying to password protect.

Also be sure to include the user login names of the people you plan to allow to this folder next to the Require user line. In my case, I simply added myself to this folder as a user (andrea).

Now, create the .htpasswd file in the same Folder but NOT by using a text editor. Instead use this command from the command line on your Linux server.

Type this command at the prompt:
htpasswd -cmb .htpasswd andrea ann2cute

Note that you must use your own name and password (replace andrea and ann2cute) and that the option cmb does the following: First it forces Creating of a new .htpasswd file. Since this is your first time adding a user it is necessary. Next the m option forces encryption and b allows you to include the user name and password immediately. In my case I created a new .htpasswd file, then added the user andrea and her password ann2cute. 

Step 4. Add Additional Users
To add users you simply need to edit both files again. First, add a user to the .htaccess file by opening it in a text editor and including the new person (my example is bradley).

The .htaccess file should include these lines:
AuthName Login to the Private Area
AuthType Basic
AuthUserFile /var/www/html/Private/.htpasswd
Require user andrea bradley

Remember to save the file when youre done adding the new user!
Now add the user (my example being bradley) to the .htpasswd file using this command:
htpasswd mb .htpasswd bradley brad4chad

In my example, I used the htpasswd command to add using encryption the user bradley to the .htpasswd file that already exists and include his password as brad4chad. That's it!

Step 5. Test the Password Function
Now test that the Apache server is accepting this new protected folder by going to it in a web browser. In my case I test the url http://office.server.com/Private and up comes a pop-up window that requires User Name and Password. I type in my user name and password and instantly I see the index.html page I put in my folder! People who don't have a login won't get access to your web pages within this folder.

What if it didnt work? Almost always this is a result of the httpd process not being restarted. You can easily restart this process to pick up the changes in your updated httpd.conf file by either rebooting or restarting the process. A reboot works fine, so long as you can tolerate a web server outage for a minute or two. Or, on most flavors you can type this command as root user:   ./httpd start 

Step 6. What About Removing Users
There may come a time when you need to delete users from the access. You can do this easily enough by again editing the .htaccess file and running a command to delete the user from the .htpasswd file. 

First, edit the .htaccess file and remove the user you do not wish to allow access to and save the file.

Second, delete the user from the .htpasswd file by typing this command at the prompt:   htpasswd D bradley
The option D is for delete. It should prompt you that user bradley was deleted. 
 

Beginner Server Administrator Commands

September 9, 2011

Beginner Server Administrator Commands

Command

Summary Use

arp

Command mostly used for checking existing Ethernet connectivity and IP address

Most common use: arp

This command should be used in conjunction with the ifconfig and route commands. It is mostly useful for me to check a network card and get the IP address quick. Obviously there are many more parameters, but I am trying to share the basics of server administration, not the whole book of commands.


df

Display filesystem information

Most common...


Continue reading...
 

How to configure kickstart with remote installation in rhel5 [kickstart + dhcp]

September 6, 2011

How to configure kickstart with remote installation in rhel5 [kickstart + dhcp] or centos5


###KickStart [Unattended Installation]###
Kickstart helps to install system unattended. It uses kickstart configuration file and dhcp service for this.
In windows systems kickstart file is known as answer file.

###These are the steps happening.###
Server[dhcp+ks] client
1. asks IP and location of kickstart file
2.gives the info
3. Takes the ks.cfg file and mounts share of OS

###dhcp [nfs/ftp/http] + ks.cfg [nfs]...

Continue reading...
 

How to install and configure phpmyadmin on redhat linux

September 6, 2011

How to install and configure phpmyadmin on redhat linux or centos linux


First of all add the rpmforge repository to user system. Explained here

Now :

[root@localhost ~]# yum install phpmyadmin

[root@localhost html]# vi /usr/share/phpmyadmin/config.inc.php

And add the blowfish secret,
$cfg['blowfish_secret'] = 'a8b7dafdferHJHJ^&U^45776TIUuiYc6d';

http://127.0.0.1/phpmyadmin/

Now edit the phpmyadmin configuration file for apache

[root@eastonch /]# vi /etc/httpd/conf.d/phpmyadmin.conf


  Order Deny,Allow
  ...

Continue reading...
 

RAID

September 6, 2011

How to create a Raid Device using madadm in linux rhel5 with level 5, 0 and 1


The following article explains  what is Raid, what are important levels and how to install and configure a raid device in a linux system using the software mdadm. This is tested in Redhat rhel5 and also works with other distributions as fedora, centos etc. 


What is RAID?
RAID is redundant array of independent or inexpensive disks. It is mainly used for data protection. It protects our data storage from failures and dat...

Continue reading...
 

Installing and configuring Dansguardian with Squid proxy

September 6, 2011

Installing and configuring Dansguardian with Squid proxy in linux rhel5 or centos5


 Installing and configuring squid internet proxy can be found in the following link. 
http://helpinlinux.blogspot.com/2010/04/how-to-install-and-configure-squid.html

After configuring the Squid proxy, You can install and configure Dansguardian.

Dansguardian is an add-on for squid. DG is having lots of list files to which we can add and remove entries easily. No need to write complicated ACL rules in squid.


DG acts a...

Continue reading...
 

How to enable user authentication in squid proxy

September 6, 2011

How to enable user authentication in squid proxy


This post explains how to enable user authentication on a squid proxy. 


Suppose you have an ip 1.2.3.4 to be enabled for a user test_user 


Syntax :
acl acl_name proxy_auth username REQUIRED
tcp_outgoing_address ipaddress acl_name
http_access allow acl_name

eg:-
acl auth_user2 proxy_auth test_user REQUIRED
tcp_outgoing_address 1.2.3.4 auth_user2
http_access allow auth_user2

And also you can create passwords for the users by using the following command
#htpa...

Continue reading...
 

How to configure syslog server or a centralized log server

September 6, 2011

How to configure syslog server or a centralized log server in redhat linux rhel5 or centos


This article will explain installing and configuring a syslog log server in redhat enterprise linux. It'll work in other redhat distributions like centos, fedora etc.

Centralized log server (syslog server)

Suppose we have a server and 5 client machines. And we want to monitor the logs of all those client machines. In situations like this, we will use centralized server as a log server. Whatever events are ...

Continue reading...
 

How to set chattr attribute on a file

September 6, 2011

How to set chattr attribute on a file


The following post explains how to set ch attribute on a file. 
root@me [~]# chattr +i /etc/fstab
root@me [~]# lsattr /etc/fstab
----i--A----- /etc/fstab
root@me [~]# chattr -i /etc/fstab

root@me [~]# lsattr /etc/fstab
-------A----- /etc/fstab
root@me [~]#
The letters "acdijsuADST" select the new attributes for the files: append only (a), compressed (c), no dump (d), immutable (i), data journalling (j), secure deletion (s), no tail-merging (t), undeletable (u),no ...

Continue reading...
 

How to log a rule in iptables

September 6, 2011

How to log a rule in iptables


This post explains how to write a rule to reject something in iptables and how to log the working of iptables

First of all enable iptables loging in /etc/syslog.conf 
#vi /etc/syslog.conf 
kern.*        /var/log/firewall
:wq

Restart the syslog daemon.
#service syslog restart

Create the log file specified in /etc/syslog.conf 
#touch /var/log/firewall  #log file.

Now Define the rules in following order. Log rule must be first.

This is a simple rule which will block response t...

Continue reading...
 

Recent Posts