Friday, September 28, 2012

Configuring A High Availability Cluster (Heartbeat) On CentOS

      In this example we will configure a webserver using apache and we will cluster it. It can be implemented on centos, fedora and other redhat flavors.

Pre-Configuration Requirements

1] Following are the hostnames and ipv4 addresses that will be used:
  • Primary Node : IP - Hostname - 
  • Secondary Node : IP - Hostname - 
  • Virtual IP for Apache -
Assign virtual IP address using following steps on both node :
# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0 :0

2] Download and install the heartbeat package. In our case we are using CentOS so we will install heartbeat with yum:

[root@server1 ~]# yum install heartbeat

or download below package

3] Now we have to configure heartbeat on our two node cluster. We will deal with three files. These are: (authkeys,, haresources)

[root@server1 ~]# cp /usr/share/doc/heartbeat-2.1.2/{authkeys,,haresources} /etc/ha.d/

4] First we will deal with the authkeys file, we will use authentication method 2 (sha1). For this we will make changes in the authkeys file as below.

[root@server1 ~]# vi /etc/ha.d/authkeys
Add the following lines at the end of file & save:
auth 2
2 sha1 test-ha
5] Also the authkeys file must be read only root:

[root@server1 ~]# chmod 600 /etc/ha.d/authkeys

6] Make changes in second file of hearteat (haresources) :

[root@server1 ~]# vi /etc/ha.d/ haresources
#Add the following line at the end of file & save:  httpd

7] The final piece of work in our configuration is to edit the file which is important :

[root@server1 ~]# vi /etc/ha.d/
#Add / Uncommnet below lines in file & save.
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth0
udpport 694
auto_failback on

8] As we want httpd highly enabled let's start configuring httpd:

[root@server1 ~]# vi /etc/httpd/conf/httpd.conf
#Add following line:

9] Copy the /etc/ha.d/ directory from server1 to server2:

[root@server1 ~]# scp -rvp /etc/ha.d/

10] Copy httpd.conf file from server1 to server2:

[root@server1 ~]# scp -rvp /etc/httpd/conf/httpd.conf

11] Create index.html file on both server under /var/www/html directory.

[root@server1 ~]# echo "SERVER1 Apache test server" > /var/www/html/index.html

[root@server2 ~]# echo "SERVER2 Apache test server" > /var/www/html/index.html

12 ] Now start heartbeat on the primary server1 and slave server2:
# /etc/init.d/heartbeat start
13] Open web-browser and type in the URL:
It will show apache test server.
13. Now stop the hearbeat daemon on
# /etc/init.d/heartbeat stop
In your browser type in the URL and press enter.
It will show apache test server.

Tuesday, September 25, 2012

Install and configure Apache with PHP on Windows machine


Install and configure Apache with PHP on Windows machine :

Apache Installation :
1] Download the latest apache at

2] Run the installer apache_2.2.9-win32-x86-no_ssl-r2.msi.

3] First screen is the welcome note. Click Next.

4] Second screen is the licence agreement. Accept the terms in the license agreement, click Next.

5] Third screen, brief desription of the software. Next.

6] Server Information Dialogue will ask you for the following values:
Network Domain – name of your computer across a network connection. To know the Full Computer Name, right-click My Computer icon, choose Properties, click Computer Name Tab, and look for Full computer name. Or you can simply type localhost as its value.
Server Name – the same value with the Network Domain. Or localhost.
Admnistrator’s Email Address
Choose the radio button for All Users, on Port 80, as a Service. Click Next.
7] Select Typical for Set up Type. Next

8] Select Typical for Set up Type. Next

9] Destination Folder. Leave the default value, click Next.

10] Next screen is Ready to Install the Program. Click Install.

11] To Test Your Installation
Open a browser and point to
http://localhost or You should see the Apache Installation page.

PHP Configuration :

1] Download the latest php at .

2] Unzip the contents of package to C:\PHP folder.

3] Find and rename the file php.ini-recommended to php.ini under PHP folder.

4] Open the file php.ini, look and uncomment the following directives (Click Edit > Find or Ctrl F) and save file.
[ Uncomment – remove the ; before a directive to activate it. ]
display_errors = on
register_globals = off
magic_quotes_gpc = on
extension_dir = /PHP/ext
session.save_path = /tmp Create a folder named TMP under the root directory (Drive C:)

--> 5] Attach PHP to Apache as a module. Open the Apache configuration file (C:\Program Files\Apache Software Foundation\Apache2.2\Conf\httpd.conf).
Search for LoadModule. Add this at the end:
#LoadModule vhost_alias_module modules/
LoadModule php5_module "c:/php/php5apache2_2.dll"
PHPIniDir "C:/PHP"

Search for DirectoryIndex and add index.php.<IfModule dir_module>
DirectoryIndex index.php index.html
Add the following under <IfModule mime_module>:AddType application/x-httpd-php .php

6] Copy libmysql.dll file from your C:\PHP folder to the C:\WINNT\system32 directory or C:\Windows\System32 directory(Windows XP).

7] Restart Apache.
To Test PHP
Create phpinfo.php, type:<?php echo phpinfo(); ?>
Save as phpinfo.php in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\
8] Open a browser and point to http://localhost/phpinfo.php or .

Friday, September 21, 2012

Find and Replace work in Multiple Files

Easy Search and Replace word in Multiple Files on Linux Command Line

I was trying to find a solution to do a find & replace word across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

Syntax : [root@server ~]# grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'

Example : [root@server ~]# grep -lr -e 'country' * | xargs sed -i 's/country/India/g'

This command broken down:
  • grep for the word in a files, use recursion (to find files in sub directories), and list only file matches
  • xargs passes the results from the grep command to sed
  • sed -i uses a regular expression (regex) to evaluate the change: s (search) / search word / target word / g (global replace)
Find & Replace word of specific files:
I want to replace the word “country” with “India” from those files which are having “.php” extension. To do so use the following command :
Syntax : [root@server ~]# find . -name “*.php” -print | xargs sed -i 's/old word/new word/g'
Example : [root@server ~]# find . -name “*.php” -print | xargs sed -i 's/country/India/g'

It looks bit complicated but quite simple. There are three components to the command.
  1. find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:
  2. xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
    arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
  3. sed -i 's/country/India/g' – Stream Editor is a tool which should be in every sys admin’s toolkit. In this case every occurence of “country” is replaced by “India” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it.