Wildcard Apache2 Sub Domains

Sub domains in Apache…

Here’s the apache config used for this site.

This tells apache to serve up all sub domains of dtbaker.com.au from the same /var/www/dtbaker directory

<VirtualHost *>
ServerName dtbaker.com.au
ServerAdmin dtbaker@email.com
ServerAlias *.dtbaker.com.au

DocumentRoot /var/www/dtbaker/

<Directory />

<Directory /var/www/dtbaker>
Options Indexes FollowSymLinks MultiViews
Order allow,deny
allow from all

ErrorLog /var/log/apache2/error.log

LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On


Using PHP to restart Apache

It can be a little tricky if you need to restart Apache from a PHP script, this is because restarting Apache will kill the running PHP script, so the user will receive no output (or sometimes a “file download” dialog) when apache is restarted.

People may try doing something like this:


instead you need to run something in the “background” like this:

passthru("/path/to/custom_apache_restart &");

that way execution goes straight back to the php script, and our custom apache restart script waits a few seconds before restarting apache.

Another way to do this is call reload, instead of restart.

/etc/init.d/apache2 reload

Website Screenshot Thumbnail Creator Script

One day I needed to create thumbnails of about 300 websites, this
would usually take a very long time, so I wacked together a simple
shell script which makes use of Firefox, Xvfb, and Imagemagick

script creates a virtual X desktop, starts a Firefox instance, and
then, using the openUrl command, proceeds to load each website and
capture a screenshot one at a time.

If you cannot get Xvfb
working, you should be able to run this with little modification on a
spare linux desktop. I suggest you play with image magicks cropping
tools to remove the firefox decal.

here is the original bash script:

echo "Starting virtual screen......";
Xvfb :3 -screen 3 1024x768x24 &
sleep 5;
export DISPLAY=:3;
# needed fluxbox becuase mozilla width/height wouldnt work without a w/m
echo "starting fluxbox....";
fluxbox &
sleep 20;
# start the firefox process full screen on the virtual buffer
echo "starting firefox.....";
mozilla-firefox -width 1024 -height 678 &
sleep 15;
for i in `cat sites.list`; do
echo "processing $i .....";
if ( host $i 2>&1 > /dev/null ); then
echo " ... $i exists..";
mozilla-firefox -remote "openUrl(http://www.$i)"
echo " ... waiting for page to load";
sleep 10;
import -display :3 -w root ~/web/$i.jpg;
convert -geometry 200x200 ~/web/$i.jpg ~/web/thumb/$i.jpg;
echo "..... done";

Simple PHP MySQL Database Functions

A simple bit of code to make PHP database connections a little easier to use.<?php
function db_connect(){
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("Cannot connect");
$dbcnx = mysql_select_db(DB_NAME) or die ("Cannot select db");
return $db;
function query($sql,$db){
$res = mysql_query($sql,$db) or die("Error in sql $sql: ".mysql_error());
return $res;
function qa($sql,$db){
$res = query($sql,$db);
$data = array();
while($row = mysql_fetch_assoc($res)){
if($res['id'])$data[$res['id']] = $row;
else $data[] = $row;
return $data;
function qa1($sql,$db){
return array_shift(qa($sql,$db));

and to use it do something like:

$db = db_connect();
$sql = "SELECT * FROM foo";
$result_array = qa($sql,$db);

Update page while PHP script executes

If you have a script that runs for a long time and you would like to
update the browser with some progress reports here is a very simple way
of doing it:


<div id="messages"></div>
function w($message){
?> <script language="javascript">
document.getElementById('messages').innerHTML =
document.getElementById('messages').innerHTML + "<?=$message;?>";
</script> <?php
@ob_flush(); @flush();
w("Here is a message");
w("And another message");
w("And yet another message");


View an example here: click

Internal/External Trusted/Untrusted Bind9

Ever wanted to provide different dns responses depending on where the request came from?

example, a query for mywebsite.com coming from the local network will respond with, however a query for
mywebsite.com coming from the internet address will respond

Here’s a simple way to do it using a single bind instance.

config coming soon

Uploading a file using Curl in PHP

Here’s how to upload files using curl in php: (it’s very easy)

notice the @ infront of the file path, this is the magic part.

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);