My first Cron Job

5 11 2008

So it’s been a little over a month since I’ve posted anything, and that’s cause I’ve been kind of busy with school, and therefore haven’t done much with the server. I did install phpBB3, but I didn’t think that was really worth a mention.

What I just got done doing is writing my first shell script and assigning it as a daily cron job. Basically what the job does is run through my Pictures directory–which I linked to at /var/www/Pictures/–and places an instance of the file from the “Image Thumbnails” post in each of the subdirectories. I did this because I found it annoying to have to constantly check the Pictures directory to see if anyone uploaded any new pictures, and then having to place the file in there myself. So, what the script does is run through all the directory’s contents and if it finds a directory it changes into that directory and copies a file called noindex.php from the maindirectory into a file called index.php in the directory it’s in. It then changes back out and continues the process until the entire contents of the folder is processed. Here’s the script, and a sample output.

#Script Name:
#Author Name: Willem Ellis
#Date: Tues Nov 4 17:22:00 CST 2008
#Description: This is a file used to insert
# index.php into newly created
# sub-directories within a main
# directory.
cd /var/www/Pictures/
for f in $FILES
if [ -d "$f" ]
echo "Processing $f..."
chmod 777 "$f"
cd "$f"
cat "../noindex.php" > index.php
cd ..
elif [ ! -d "$f" ]
echo "Not a directory..."

Processing Folder1…
Not a directory…
Processing Folder2…

This site is where I got a small part of this script, it explains what the for loop does. Basically it stores the list of files within the directory as the variable FILES. The for loop scrolls through the loop and saves each individual item in the list as f and works with each item one at a time until all the items have been processed. It’s saying for the variable f in the list FILES do the following… Then my if else if loop just says if the file you are on (f) is a directory change into it and copy noindex.php to index.php inside the directory. Otherwise if (elif) the file is not a directory, print out “Not a directory…” Each time the for loop completes, it moves to the next item and stores that as f until there’s nothing left in the list, at which point the script is done.

After I wrote the script, all I did was save the script as in /etc/cron.daily/ and edited /etc/crontab to run the job every day at 6:00PM.

00 17 * * * root /etc/cron.daily/

Hope someone else finds this handy.

Image Thumbnails

30 09 2008

So I have a directory on my server with several (more than 50) sub-directories filled with pictures. Obviously this is a lot of pictures, and since I did not name the images as I uploaded them, I have no idea what’s what. I just know where and when they were taken from the folder titles. I thought something that would generate thumbnails would be great for browsing through the pictures, especially if you were looking for a certain image. So I found this script, and added a two lines that would automatically create a thumbs directory in the Image directory, and that would redirect to the newly generated html page.

Basically what the script does is resize all the .jpg files in the directory, and saves them in the newly created thumbs directory. It then creates a file called gallery.html which shows all the new thumbnails with links to their originals. It does pretty much exactly what I was looking for. Originally the script was called create_thumbs.php, but I renamed it to index.php so it would load each time you opened the folder. In case images had been added. Then I added a redirect line so that it would automatically redirect to gallery.html. This is what I ended up with.
This is the PHP code for the How to Create Thumbnail Images using PHP Tutorial
This script creates all of the thumbnail images and the gallery.html page.
Note: Make sure that PHP has permission to read and write
to the directory in which .jpg files are stored and the directory
in which you're trying to create thumbnails.
You may use this code in your own projects as long as this
copyright is left in place. All code is provided AS-IS.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
Copyright 2007

header( 'Location: ./gallery.html' ) ;
mkdir("./thumbs", 0777);
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )

// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new tempopary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
// close the directory
closedir( $dir );

function createGallery( $pathToImages, $pathToThumbs )
echo "Creating gallery.html <br />";

$output = ";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";

// open the directory
$dir = opendir( $pathToThumbs );

$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";

$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
// close the directory
closedir( $dir );

$output .= "</tr>";
$output .= "</table>";
$output .= "<br />";
$output .= "<a href = ' Sr Pictures'>Return To Pictures</a>";
$output .= "</body>";
$output .= "</html>";

// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );

// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links

You place it in the folder where the images are located, name it index.php, make sure the folder is chmodded to 777 and when you navigate to that folder, it will create a thumbs directory and the gallery.html file. Then it will redirect to your gallery.

Finally, since I had to place this file in a lot of directories, I placed a copy of the file in the parent directory. Changed to the parent directory, and issued the following Unix command. Then I deleted the copy located in the parent directory.

sudo for dir in *; do [ -d “$dir” ] && cp index.php “$dir” ; done

I got the script from here.


14 09 2008

So after I made my music available in my web root, I felt it necessary to password protect it. I don’t want anyone to download my music or stream it or anything like that. No one except me, that is. No illegal activity should be coming from my server I figured. So I thought of the most basic way to do it which was htaccess.

Basically, you create a file called .htaccess with certain parameters. Here’s mine:

AuthType Basic
AuthName "Music"
AuthUserFile /var/.htpasswd
require valid-user

I’m not exactly sure what


is, but


is whatever you wanna call the folder you’re protecting. It doesn’t change the folder name, this is just what shows up in the little login window that pops up when you navigate to this folder.


is where the .htpasswd file is stored. The .htpasswd file specifies which user and password combinations are allowed to access this folder.

require valid-user

just lets it know that any valid user specified within the .htpasswd file is required for access.

In .htpasswd, each individual user should have his own line, and username and password should be seperated by a colon. i.e.


Finally, in your apache settings, specifically if you’re using Ubuntu the file will be located at /etc/apache2/sites-available/default. Find the line

AllowOverride None

and change it to

AllowOverride All

Real Quick

10 09 2008

Sorry for the two short posts after each other, but this is definitely worth a mention.

With the external HDD working, and everything being saved on there, I wasn’t sure how to make it accessible without having to connect via FTP every time. More importantly, everyone doesn’t necessarily wanna download every song they wanna listen to, so streaming is nice.

Since all the music and pictures are saved outside the webroot (/var/www/) I wasn’t sure how to make them accessible within the webroot. Well, after searching around, I found this line. It creates what’s called a Symbolic Link. For those familiar with windows, it’s basically like a shortcut, except it’s a little different because it treats it as though the files are saved in the location where the link is. I don’t know if I did a good job of explaining that, but you’ll see what I’m talking about if you have to do this for yourself. So the line is

sudo ln -s source_file my_file

The sudo gives you the correct permissions to do this, which is necessary in Ubuntu. source_file gets replaced with the path to the file (or folder) you wanna make the symbolic link for. my_file gets replaced with the path where you want to place this link.

So now when I open up my page and click on the music link, it takes me to all my music and I can stream it straight through my browser. Pretty sweet!

Worth a mention

10 09 2008

Well, this is kind of short, but I got wordpress loaded on the server. I was having trouble accessing the MySQL database. What happened was, I forgot the password I originally set for the root user, and thought I could just use my username and password. So all I had to do was reinstall MySQL and actually remember what the password was this time. I assume you already know how to install wordpress. If not there are plenty of help topics on it.

I know there is a command for resetting the MySQL root user password, I just forgot what it is.

External HDD

9 09 2008

So the most recent thing I’ve done is add on a 1TB external hard drive. This will be where basically all media such as photos and videos and music will be stored to be shared with the family.

When I first plugged in the drive, I couldn’t even move files on and off it. I wasn’t sure what was wrong, but eventually found out you have to make it your own. So you chown it by doing the following:

sudo chown -R user.user mountpoint

Where user is replaced with you username, in my case muzak, and mountpoint replaced with the location where the drive is mounted, in my case /media/disk.

This should’ve worked, only it didn’t. The problem was that the disk was using the FAT32 filesystem, which doesn’t support permissions. So basically what I had to do was reformat it. I opened up gparted,

sudo gparted

Navigated to the external disk and unmounted and reformatted it to ext3. After that I unplugged the disk and plugged it back in, ran the same command and everything was great.

Another way you can modify the permissions is to enter

sudo "filemanager"

filemanager being whatever you use to manage your filesystem, in my case dolphin. It will open up the filesystem, but you’ll be treated as the root user. You can now right click where the drive is mounted, in my case /media/disk, and click on the permissions tab. This is the thread where I got all my help from.


13 08 2008

Well, I was commissioned by my dad to install a mailserver, and I can say I have successfully done so in a day. It wasn’t easy, I got many errors, and sometimes had absolutely no idea what I was doing, but thanks to the forums I was able to get it working perfectly.

The first thing I had to do was find out what all was necessary for the mailserver. I found this great source from the Ubuntu documentation site which laid everything out nicely. I decided to use Postfix for my Mail Transfer Agent (MTA) since it is the default for Ubuntu. Nothing was terribly difficult about installing it, I just followed the default list of settings they give in the guide. One problem I did run into however, was with Comcast not allowing its customers to directly send out e-mail. They had set their service up so that their block of IP’s could not send e-mails out from their own outbound servers. They listed these IP’s on This means that if I sent an e-mail to certain addresses, depending on how strictly they followed the lists from Spamhause, the e-mails could possibly not be delivered. This ended up being a real easy fix. In /etc/postfix/, I just had to change the “relayhost = ” value to whatever Comcast’s outgoing mailserver address was. In this case,

Since I had no idea what I was doing to start, I felt the Mail filtering option was something I could add later when I had the server working perfectly. So I skipped over that section and to the Mail Delivery Agent (MDA). For that, I chose Dovecot, since they say it was written primarily with security in mind. From the choices of Protocols, POP3, POP3S, IMAP, and IMAPS, I chose IMAP and IMAPS. There was really no specific reason for this, I read on wikipedia that they were all pretty much the same, IMAPS and POP3S just being more secure. For the choice of mailboxes, I went with Maildir instead of the default mbox. This was because they explained Maildir as saving each individual e-mail to its own file. This reduces the chance of accidentally losing an e-mail, because the files are never modified. With mbox however, each file may contain several e-mails, and so that means the files are modified to write new e-mails to them.

I must say that I did not know anything about either OpenWebmail or SquirrelMail. I just chose one quickly. I am not too impressed with SquirrelMail which I am currently using. It’s kind of ugly and that makes it hard to look at. I just read over OpenWebMail and am considering using that just because you simply download and it’s ready, and looking at the screenshots, it looks a lot nicer.

These are all of the problems I ran into installing this server. Everything works perfectly for me now. I know the forums helped me a lot, and without people replying to my threads there, I’d probably still be stuck. The guide which I linked to at the very beginning of this post was a really big help too. Every step is spot on, and really that’s the only reason I even knew which direction I needed to go.