Archive for February, 2006

How to: Apache logging using name-based virtual hosts

Saturday, February 25th, 2006

I spent a bit of time today getting logging to work correctly on my server. The server is using name-based virtual hosts, so getting logging set up was going to take an extra step. The first thing was to add another LogFormat line in the apache2.conf file which would use the comonvhost option.

Code:
LogFormat “%v %h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" comonvhost
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined
LogFormat “%h %l %u %t \”%r\” %>s %b” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

Adding this LogFormat line, lets Apache distinguish between the various virtual hosts when logging. To learn more about what the options mean (i.e. %u, etc.) you can look here. Each virtual host was given its own log file, so in the VirtualHost directive, the following line was added using CustomLog:

Code:
ServerName www.sematopia.com
DocumentRoot /var/www/sematopia.com
ServerAlias sematopia.com *.sematopia.com sematopia.net *.sematopia.net
CustomLog /var/log/apache2/sematopia/access.log comonvhost

Then for all other VirtualHost directives, a similar line was added. Once that’s done, delete or move the old logs in /var/log/apache2. Then run:

Code:
apache2 –k restart

to restart Apache. Remember, any time you delete your log files or change your apache2.conf file restart apache.

How to: Backup MySQL database & email results using PHP

Thursday, February 16th, 2006

Update Aug 29: You can also try an updated version of this script that uses Amazon S3 instead of email.

There are a bunch of scripts on the Internet that overly complicate the issue of automatically backing up your database. Here is a PHP script I wrote to backup a mysql database and email the results. Just set a cron job (/etc/crontab) to run this script with the following command: php /…./dbBackUp.php

Code:
< ?php

/*

Quickly and easily backup your MySQL database and have the tgz emailed to you.  You need PEAR installed with the Mail and Mail_Mime packages installed. Read more about PEAR here: http://pear.php.net. This will work in any *nix enviornment. Make sure you have write access to your /tmp directory.

*/

require_once('Mail.php');
require_once('Mail/mime.php');

// mysql & minor details..
$tmpDir = "/tmp/";
$user = "root";
$password = "pass";
$dbName = "db";
$prefix = "db_";

// email settings...
$to = "someone@gmail.com";
$from = "another@gmail.com";
$subject = "db - backup";
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$attachment = $tmpDir.$prefix.date('Y_m_d').".tgz";

$creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
$createZip = "tar cvzf $attachment $sqlFile";
exec($creatBackup);
exec($createZip);

$headers = array('From' => $from, 'Subject' => $subject);
$textMessage = $attachment;
$htmlMessage = "";

$mime = new Mail_Mime("\n");
$mime->setTxtBody($textMessage);
$mime->setHtmlBody($htmlMessage);
$mime->addAttachment($attachment, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = &Mail::factory('mail');
$mail->send($to, $hdrs, $body);

unlink($sqlFile);
unlink($attachment);

?>

To download the script click here.

XML Mail – Batch sending and easy logging

Thursday, February 9th, 2006

I ended up purchasing a relay service from Nettica.com, so this batch emailing idea is not going to be of much use to me. In any case, I think its a pretty good way to control emails. If your site sends many emails per day, then a batch email setup might be a good option.

Using a batch emailing method, first off, allows for an inherent logging ability built into the process. Every time the application wants to send an email, it would write the email (XML) to send.xml. Periodically, from an internal or external server, the send.xml file is processed, and then automatically backed up. This can be extended to eventually gzip the XML files, etc.

Secondly, if your site sends many emails, you may not want to consume resources (in real-time) sending them on demand. Instead, sending all emails every say 5 minutes, could possibly improve server performance.

A typical example code would be:

Code:
$mailObj = new xmlMail();
$mailObj->getHandle();
$mailObj->writeEmail($to,$cc,$bcc,$from,$from,$subject,$body);
$mailObj->closeHandle();

See the PHP Class here for more details.

Design Project: POC 3

Tuesday, February 7th, 2006

We’ve come a long way from the last design of our robot. We have a bunch of ideas on the go, and I have some algorithms in the works about keeping the robot centered in the hallway network. We have our third proof of concept Friday. For this, we have to upload a map, make a turn, rescue an egg and exit successfully. Here are some pics of our current design (taken on from my cell phone, so not great resolution).

poc3_3.jpeg

poc3_2.jpeg

poc3_1.jpeg

Your Ad Here