« Posts tagged php5

A PHP-Based Server Monitor

The other day I decided that the little ‘Network Monitor’ desktop gadget I was using to monitor my few servers just wasn’t cutting it. Instead, I wanted to make use of a spare iMac and have something a little flashier. A Google search for Server Monitors brought up a plethora of options that were either horribly ugly, platform specific, or just didn’t work the way I needed (most required that the target server be running some form of web server, such as IIS or Apache to retrieve headers to see if the server was up – most of my servers don’t run those). As such, I decided to write a small script from scratch.

I figured the easiest way to accomplish my goal of a platform-independent monitoring script was to use PHP. After enabling Apache2/PHP5 on my Snow Leopard-running iMac (a topic for another blog post later), I searched through the PHP.net function list until I found fsockopen(). This function is quite ideal, as it will work with any open port. The first step was to make a quick function to utilize fsockopen and return some testable results:

function checkServer($ip,$port)
{
$fp = fsockopen($ip,$port,$errno,$errstr,1);
if (!$fp)
{
return ‘Down’;
} else {
return ‘Up’;
}
}

I added this to a <?php ?> block in the <head></head> of the document – to call the function and perform the test, I used the following line below:

$servername = checkServer(’192.168.1.100′,’53′);

In this example I’m checking the availability of a DNS server, so I use port 53. When this runs, the $servername is set to either ‘Up’ or ‘Down’ depending on whether or not a connection can be opened on that port.

The only thing left now was to display this output. I made a fancy table-based page with graphics where each server is a cell and the background changes between green and red depending on the $servername value. However, all you really need is the code below in a <?php ?> block in the body of the page:

echo(‘Server Example Status: ‘.$servername);

Changing Example to the name of your server. If you have more than one server to check, just make another variable, use the checkServer function to give it a value (make sure to change the IP address and use an open port!), and then add another echo line.

That’s it! To be fancy, you can add a javascript automagic page refresh to – just change the <body> tag to:

<body onLoad=”Javascript:timedRefresh(30000); display();”>

And put the following in the <head></head> section:

<script type=”text/Javascript”>

<!–

function timedRefresh(timeoutPeriod) {

setTimeout(“location.reload(true);”,timeoutPeriod);

}

//  –>

</script>

And you’re done! If the server is up, every 30 seconds your page will refresh and show:

Server Example Status: Up

WordPress Installation on Server 2003 x86 with Apache2/PHP5/MySQL5

(This assumes you already know how to actually install Apache2 and MySQL5 properly, and setup a database with a user for WordPress).

A quick note about WordPress installations on Windows Server 2003. After installing Apache2, MySQL Server Community Edition, and extracting PHP5 (Protip: don’t use the installer, just download the .zip file and extract it to a folder on your server, eg. C:php), configure the php.ini file in the PHP directory as normal, with the following changes to avoid issues with MySQL not integrating with PHP correctly:

1) Set extension_dir to the absolute path of the extensions directory. So if you extracted PHP to C:php, you need to set extension_dir to “C:phpext”. Do not use relative paths!

2) Remove the semi-colon from the line extension=php_mysql.dll.

3) Set the upload_tmp_dir path to an existing folder. I’d suggest making a new temp folder, such as C:phptemp.

4) Configure mysql.default_port and mysql.default_host with the correct values for your MySQL server.

5) Set the doc_root parameter to where-ever you plan on storing your the files for your site. This needs to be the same as the document_root parameter in Apache2′s httpd.conf.

Now you just need to configure Apache2 to correctly use PHP5. Add the following lines to httpd.conf (note the slashes used – don’t use backslashes!):

LoadModule php5_module “c:/php/php5apache2_2.dll”
AddType application/x-httpd-php .php
PHPIniDir “C:/php”

Even though the installation instructions included with PHP say to use php5apache2.dll, we have to use php5apache2_2.dll, otherwise Apache2 will fail to start. All that’s left now is to copy libmysql.dll and php5ts.php from the PHP directory to C:WindowsSystem32.

With this done, you should be able to restart Apache2, and you hopefully won’t get any errors! To test and make sure that PHP5 is working, and that MySQL5 is accessible, create a new file in your site root (the same folder that doc_root and document_root point to) called info.php, and put the following line in it:

<?php phpinfo(); ?>

Open your webbrowser, and type http://localhost/info.php in to the address bar, and you should get the standard PHP Info page. Do a search for MySQL, and you should have a heading for it. If so, you’re ready to install WordPress. If not, then double-check your configuration.