Mitch Satchwell

PHP Developer

Dedicated Server for £5 per Month

I recently moved my site to a dedicated server, not so much because it needs that kind of resource but rather my shared hosting renewal was up, I wanted greater flexibility, and I was interested to see what I could get on a small budget.

Server

I found by far the cheapest place to get a dedicated server is Kimsufi. For £5 a month I am getting:
CPU: Atom N2800
RAM: 2GB
HDD: 500GB

Kimsufi is part of OVH, the biggest host in Europe, and number 3 in the world. It is obviously their budget range and for the reduction in price you get a lesser product – second-hand hardware and limited support, or so I’m told – I’ve not had to use their support personally. They do however monitor your server and will be notified in the event of hardware failure.

They also provide a control panel which allows you to:
- Install OS of choice at the click of a button
- Reboot the server
- View usage stats

Another good place to look is Low End Box – they have offers on VPS and dedicated servers from a variety of providers around the world.

DNS

There are a number of free DNS hosts out there, I opted for Namecheap’s free DNS service. Whilst most registrars provide free DNS hosting for domains registered through them, Namecheap allow you to use their service for any domain, regardless of where it was purchased.

BuddyNS provide a free secondary DNS for up to 0.3 million queries per month, which is useful to have in case your main DNS service goes down.

Backup

No back up space is provided or available to purchase through Kimsufi, as is the case with a lot of budget providers.

Your best options in my opinion are DIY or cheap FTP space.

I opted to backup to my home FreeNAS server.

Quick PHP Linux Server Performance Script

A quick PHP script to get you performance data relating to CPU, RAM, and disk space:

$top = explode("\n", shell_exec('top -b -n 1'));
preg_match('/Cpu\(s\):\s+(\d+\.\d+)%us/', $top[2], $cpu_usage);
$performance['cpu'] = array(
    'usage' => $cpu_usage[1],
    'load' => sys_getloadavg(),
    'cores' => shell_exec('grep --count processor /proc/cpuinfo')
);

foreach (explode("\n",file_get_contents('/proc/meminfo')) as $v) {
    $v = explode(':', str_replace(' ','',$v));
    $mem[$v[0]] = (int)$v[1];
}
$performance['ram'] = array(
    'total' => $mem['MemTotal'],
    'free' => $mem['MemFree'] + $mem['Buffers'] + $mem['Cached']
);

$performance['hdd'] = array(
    'total' => disk_total_space('/'),
    'free' => disk_free_space('/')
);

Example array contents:

Array
(
    [cpu] => Array
        (
            [usage] => 5.6
            [load] => Array
                (
                    [0] => 2.18
                    [1] => 2.99
                    [2] => 3.83
                )

            [cores] => 24

        )

    [ram] => Array
        (
            [total] => 49554524
            [free] => 44478636
        )

    [hdd] => Array
        (
            [total] => 587532566528
            [free] => 297976057856
        )

)

The calculations and figures are actually easier to explain in reverse, so that’s what I’ll do.

hdd: total – total size of the filesystem using PHP’s disk_total_space function.

hdd: free – available space on filesystem using PHP’s disk_free_space function.

ram: total – total amount of physical RAM, ‘MemTotal’ figure from /proc/meminfo virtual file.

ram: free – amount of available RAM, ‘MemFree’ + ‘Buffers’ + ‘Cached’ from /proc/meminfo. The ‘Buffers’ and ‘Cached’ amounts are actually being used by the kernel for filesystem buffers etc, but will always be freed when needed for another application.

cpu: usage – percentage of CPU usage, parsed from the top command.

cpu: load – average number of processes in the system run queue over the last 1, 5, and 15 minutes from PHP’s sys_getloadavg function. *

cpu: cores – number of CPU cores, parsed from /proc/cpuinfo virtual file.

* Generally a load of 1 is considered full capacity, anything above that and you’ve got a backlog. This does not take in to account having multiple cores. You can divide the load figure by the number of cores you have, which is to say 1.5 load with two cores is roughly equivalent to 0.75 load on a single core machine. In the above example figures (2.18 avg load, 24 cores) we have a load of approximately 0.1 per core.

Useful Wrappers and Classes for iOS Development

  • STHTTPRequest – simple NSURLConnection wrapper class with some useful features.
  • FMDB – nice and easy to use SQLite wrapper.
  • STKeychain – wrapper for securely storing/retreiving passwords in keychain.
  • Reachability – Apple class for inspecting network state/connectivity.
  • MBProgressHUD – drop in class for an activity indicator for work being done in background threads.
  • UIDropDownMenu – dropdown control for UITextField.

jQuery Watermark Plugin Posted

I have created a jQuery plugin for easily applying watermarks to textboxes, as shown below.


This plugin is compatible with standard textboxes as well as jQuery UI datepicker and autocomplete widgets.

For more information on this plugin and how to use it, visit the following page:
jQuery Watermark Plugin

Creating JavaScript Widgets

Recently I needed to create a JavaScript widget for others to display data from our server on their own pages. I did a bit of reading online to give me some ideas on the best techniques for creating widgets and came across the below guide on building a widget with jQuery using an anonymous function (so not to interfere with any other code on the page). This formed the basic structure of my widget:
How to build a web widget (using jQuery)

One thing I did not like was the need to include a container div with a static id – this prevents you from having multiple instances of the same widget on the same page, plus it would be cleaner to just have the one script tag and no need for the user to include your HTML on their page. What I did instead was to insert the widget’s HTML after the script tag using jQuery’s after function. As script tags are loaded serially, if you call document.getElementByTagName('script') at the start of your widget code you will know the last element in the array is the one you are looking for:

var script_tags = document.getElementsByTagName('script');
var script = script_tags[script_tags.length-1];

Another requirement I had was to be able to pass parameters to my widget using query string values in the script src. Using the technique above to identify your script tag, you can then parse the parameters from the src URL, as shown in the following article:
Javascript pass parameters or arguments to embedded script

Something else I found useful was SitePoint’s ‘ColorLuminance’ function. I used this to calculate colours to use for my widget based on a hex base colour provided as a parameter.

Hopefully this will be of use to anyone else looking to create widget(s).

New Projects Posted

FrendMon is a Facebook app that allows you to monitor to your friends list.

nKrypt is an AES encryptor/decryptor that simulates variable key length cryptography.

DynamicSB is a cross-browser PHP/JS solution that generates soundboards dynamically from a folder listing.

First one is new, the other two are a few months old.

WordPress: Exclude Posts From a Certain Category

I wanted to exclude posts made in the ‘Projects’ category from the main page. To do so I used the following code from http://codex.wordpress.org/Template_Tags/query_posts in index.php:

<?php
if ( is_home() ) {
	query_posts( 'cat=-3' );
}
?>

WordPress Page That Shows Posts In One Category

So this is something I wanted to do but couldn’t quite figure out how (see ‘Projects’ link at the top for an example of what I’m talking about). I did a quick Google and didn’t really find anything of much use, but it lead me to the concept of page templates. I knew WordPress already had links to show posts in categories like so:

http://mitchsatchwell.com/category/projects/

But I wanted this link to appear in the list of pages at the top so I created a very simple page template that takes the title of the page you have created, and redirects to the /category/whatever URL as above:

<?php /*
Template Name: Redir2Cat
*/
header( 'Location: http://www.mitchsatchwell.com/category/'
	. get_the_title() . '/');
?>

I saved this as ‘redir2cat.php’, stuck it in my current theme folder, then created a page with the title ‘Project’, selecting the ‘Redir2Cat’ theme.

Job done.

New Site

I figured it was time I set up an actual website under this domain, so here it is, with the help of WordPress and a couple of plugins:

And a very simple page template I wrote myself (see next post).