Archive for the 'php' Category

Dynamically manage Apache virtualhosts in Linux

This is second part of article to describe how to dynamically manage Apache Virtual host. You can read first article here.

In earlier article I mentioned using a php script to dynamically create/remove virtualhost entry in Apache (httpd) config file and then reload it using cron.

Here I would describe how to manage DNS to dynamically recognize newly created virtualhosts. Again, this might not be the best or efficient way to implement this but this is what worked for me. After creating virtualhosts in Apache, you need to update DNS so that new virtualhosts start working. To update DNS dynamically, your DNS provider should have some way (like API) which enable you to manipulate its records. There are few providers offering this facility. For this experiment, I selected DNSMadeEasy which provides APIs to add/remove/update records on fly using scripts.

PHP script to dynamically create/remove apache virtual hosts/ subdomains

There’s situation with my friend where his team wanted to dynamically create/remove virtual hosts or subdomains using php. This can be achieved in several ways. You can use a control panel which obviously use resources or develop your own script to do this. There’s security aspects attached with script because it needs to update file which is read by Apache and to apply settings, you need to reload Apache. Here I am describing how my friend achieved their goal, again I’m saying that this might not be the best way to do this thing and may be comparatively insecure or inefficient but this is what worked for them in Ubuntu host.

Upgrade/update php to latest 5.2.17 in Linux

In a CentOS 5.5 host, we were required to upgrade php to its latest release which is 5.2.17 when writing this article. Again there are various options to do such upgrade and trying to install with the package manager (yum) is just one (aka easy) of them. You can also trying downloading latest source code and compile yourself to have better control but for ease of use, I’m updating php with yum here.

Again, you won’t find the latest PHP binaries in default repositories available with CentOS and to resolve that we need to have CentAlt repo (or other such repos like epel).

quick web based php script to check replication status of MySQL

This is a basic PHP script to check replication status of single or multiple MySQL servers. You have to update variables in the script to run in browser like user, password, ip of server etc.

Here is the script code:

<?
##########
#
# dbrepstatus.php by Jagbir Singh (jags@jagbir.com)
#
#
#########

?>
 
<html><head>
<title> Online DB Server status </title>
<body>
<form name="ShowStatus" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<br/>
<table border=1 align=center>
<tr align=center bgcolor=gray><td colspan=2><b> Online DB Server Status </b></td> </tr>
 
<!--- TODO: replace your server name in the select list below !>
 
<tr ><td align=centre> Select Server: <select name="server">
<option value="s1" <? if ($_POST[server] == "s1") echo "selected"; ?> > Server 1 
<option value="s2" <? if ($_POST[server] == "s2") echo "selected"; ?> > Server 2 
<option value="s3" <? if ($_POST[server] == "s3") echo "selected"; ?> > Server 3
<option value="s4" <? if ($_POST[server] == "s4") echo "selected"; ?> > Server 4 
</select> </td> 
 <td align=centre> <input type=submit name='show' value='Show Status'></td></tr>
</form>
</table>
 
<? 
$serverUp=1; ## if any of your server not configured, set this variable to 0 below
$serverString="First Server"; ## server name
$serverIP="w.x.y.z"; ## default IP, anyhow will be changed later
$dbuser="demouser"; 
$dbpassword="demopass"; 
 
$imgShow="<img src='led-green.gif' />";
 
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	switch($_POST[server]){
		case "s1":
		 $serverString="Server 1"; 
		 $serverIP="IP1";
		 break;
		case "s2":
		 $serverString="Server 2"; 
		 $serverIP="IP2";
		 break;
		case "s3":
		 $serverString="Server 3"; 
		 $serverIP="IP3";
		 break;
		case "s4":
		 $serverString="Server 4"; 
		 $serverIP="IP4";
		 $serverUp=0;
		 break; 
	}
 
?>
	<br/>
	<table border=1 align=center width=50%>
	<tr align=center bgcolor=gray><td colspan=4><b> Status of <? echo $serverString." : ". $serverIP; ?> </b></td> </tr>
	<tr > <th bgcolor=lightgray> Status </th> <th bgcolor=lightgray > Replication </th> <th bgcolor=lightgray> Delay </th> <th bgcolor=lightgray> Error </th> </tr>
	<?
	if (!$serverUp) {
	?>
			<tr> <td align=center colspan=4> This server is not configured yet! </td> </tr>
	<?
	}
	else {
		$contactAdmin="<a href='mailto:dbadmin@yourmail.com?subject=Replication issue in $serverString'> Contact Admin </a>";	
		$RepStatus = `mysql -h $serverIP -u $dbuser -p$dbpassword -e "show slave status\G;" | egrep "Slave_IO_Running: | Slave_SQL_Running: | Seconds_Behind_Master:" | cut -d':' -f2`; $RepStatus= trim($RepStatus);  
		list($RepRun, $SqlRun, $RepDelay) = split(" ",$RepStatus);
		 $RepRun = trim($RepRun);
		 $SqlRun = trim($SqlRun);
		 $RepDelay = trim($RepDelay);
 
//		echo "<br/> Reprun=$RepRun, IO Run =$SqlRun, Replication delay =$RepDelay";
 
		if($RepRun == "Yes") { $RepRun = "OK"; $contactAdmin=""; } else { $RepRun = "Replication (IO) Not Running!"; $imgShow="<img src='led-red.gif' />"; }
		if($SqlRun == "Yes") { $SqlRun = "None"; $contactAdmin=""; } else {$SqlRun = "SQL thread stopped! "; $imgShow="<img src='led-red.gif' />";}
		if($RepDelay == 0 ) { $RepDelay = "None"; } else { $RepDelay .= " seconds."; $contactAdmin=""; }   
 
 
	?>
		<tr> <td align=center> <? echo $imgShow; ?> </td> <td align=center> <? echo $RepRun; ?> </td> <td align=center> <? echo $RepDelay; ?>  </td> <td align=center> <? echo $SqlRun; ?>  </td> </tr>
 
	<?
	if ($contactAdmin != "") { echo "<tr> <td align=center colspan=4> $contactAdmin </td></tr>"; }
 
	} 
 
	?>
	</table> 
<?
}
?>
 
</html>
</body>

In my case, there are 4 servers. First is master and all other slaves so what I assumed here is that all servers have same user/pass to access information. Don’t forget to change user/pass/ip etc. before trying to run this script in browser. Let me know through comments if you any suggestion/enhancement/issue regarding this.

Download, compile, install and configure php 5.3.5 in Linux

In a CentOS 5.2 Server, there PHP 5.2.4 and due to which PCI complaince test failed. We were in requirement to upgrade PHP to latest stable version. While writing this article, we found 5.3.5 as latest stable release of PHP. Describing here the steps taken to download, install PHP 5.3.5.

Step 1. Check existing PHP modules and Install pre-requisites libraries/apps

As the first step, you should get list of installed PHP modules so that you can incude them with newer PHP as well otherwise functionality of your site/application can break.

Get list of all PHP module installed in Server: