Personal.X-Istence.com

Bert JW Regeer (畢傑龍)

FastCGI quick and dirty

In this quick and dirty guide for FreeBSD I will assume you have already compiled and installed PHP5 with fcgi mode enabled, which can be checked by running:

/usr/local/bin/php-cgi --version

I will also assume you have installed mod_fastcgi from www/mod_fastcgi in the ports.

Create the fast cgi ipc temp dir, make sure you place this somewhere it will keep existing:

cd /var/tmp
mkdir -p /var/tmp/fcgi-ipc/dynamic
chmod -R 0777 /var/tmp/fcgi-ipc

Let's create the directory where we are going to place our PHP wrapper, this config is for server wide using the same FastCGI PHP config. This is on purpose, since it will allow server wide PHP usage. If you want you could then split it up into virtualhosts if one required a standalone php running for that.

mkdir /usr/local/www/fastcgi-bin/
chmod 755 /usr/local/www/fastcgi-bin/</pre>

Next up we create the very simple wrapper

cat << EOF > /usr/local/www/fastcgi-bin/php5.fcgi
#!/bin/sh
# To use your own php.ini, comment the next line and uncomment the following one
#PHPRC="/usr/local/etc"
export PHPRC
PHP_FCGI_CHILDREN=8
export PHP_FCGI_CHILDREN
exec /usr/local/bin/php-cgi
EOF

Set PHP_FCGI_CHILDREN to whatever value you want, that is how many processes it will use. I suggest the standard which is 8, if you make it more, you use more ram, you make it less Apache will have to wait if all of the PHP processes are in use.

Next up, make it executable:

chmod +x /usr/local/www/fastcgi-bin/php5.fcgi

Last but not least, edit httpd.conf and make sure to add a comment in front of the mod_php5 line, and uncomment the mod_fastcgi line. Then add this to the end of your Apache config:

<IfModule mod_fastcgi.c>
    FastCgiIpcDir /var/tmp/fcgi-ipc/
    FastCgiConfig  -pass-header HTTP_AUTHORIZATION

    AddHandler  fastcgi-script              .fcgi .fcg .fpl

    Action      application/x-httpd-php5    /fastcgi-bin/php5.fcgi
    AddType     application/x-httpd-php5    .php .php5
</IfModule>

ScriptAlias /fastcgi-bin/ "/usr/local/www/fastcgi-bin/"
<Location /fastcgi-bin/>
    Options ExecCGI
    SetHandler fastcgi-script
    Order allow,deny
    Allow from all
</Location>

That's it. Restart apache and watch as the first time you access a PHP script FastCGI will spawn several of them, and they start serving requests. Load usage may seem higher, that is because unlike Apache the standalone running PHP's handle the jobs as they come in with whatever one is first, Apache will try to put as much work in one of it's preforked modules. As the PHP processes come and go through FastCGI the memory usage will go up and down. Mine has an average of about 300 MB, which is very good compared to the 800 MB Apache with mod_php would rack up.

Backup Script

I was working one some backup scripts and I decided I might as well generalise almost all of it (I am sorry about the hard coded paths) so almost anyone can come grab it and use it on their servers. The shell script is pretty self explanatory and I am not very good at documenting code, so the script source itself is your best bet as to why I did something some way. This script is in production use on several servers I administrate, I hope someone can use it as well.

Documentation on how to use it:

New backup script which will mount a /backup, then use rsync to copy files from /usr/home to /backup/yyyy-mm-dd. After it is done, it will remove the oldest backup, 14 days ago. All paths are hardcoded.

If a file .exclude is in a users homedir, it is looked at, and is used to exclude certain directories or files from the rsync. For example:

in /usr/home/xistence/.exclude

public_html/iso

No trailing slash, this will cause the folder /usr/home/xistence/public_html/iso to not be backed up. This might be for various reasons, but for this example it is because backing up GB sized files should not be something that is done, only because it takes ages and wastes valuable space.

Thing to keep in mind is, if you want to backup a 40 GB HD, with 22 GB used, to keep even a 3 day backup of that would require 3 * 22 GB, or 66 GB of available space on the backup drive. It is therefore in ones best interest to exclude very large files, and back them up in a different method since they are most likely not going to change much across revisions.

This script should be set to cron every 30 minutes, as rsync will do one full backup when the day changes, and then from there on do incremental backups.

Maybe removing the --delete option might help with customers that delete files and expect them to still be in the backup for that day, this way however if a file is removed it will still exist in the backup until it is deleted after 14 days.

#!/usr/local/bin/bash

# This file REQUIRES bash, and can not be run under /bin/sh!

###
 # Copyright 2006 Bert JW Regeer. All rights  reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # are met:
 # 1. Redistributions of source code must retain the above copyright
 #    notice, this list of conditions and the following disclaimer.
 # 2. Redistributions in binary form must reproduce the above copyright
 #    notice, this list of conditions and the following disclaimer in the
 #    documentation and/or other materials provided with the distribution.
 #
 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
 # The views and conclusions contained in the software and  documentation are
 # those of the authors and should not be  interpreted as representing official
 # policies, either expressed  or implied, of bsdPanel project.
 #
###

for HOMEDIR in `/usr/bin/find /usr/home -type d -maxdepth 1 | grep -v '^.$'`; do
    if [ -f $HOMEDIR/.exclude ]; then
        echo "Exclude found for $HOMEDIR"
        while read line; do
            if [ "${line:0:1}" != "/" ]; then
                # They are missing a full path, we will be nice and add it
                TMPLINE="$HOMEDIR/$line"
                line=$TMPLINE
            fi
            echo "  Excluding $line"
            echo $line &gt;&gt; /tmp/exclude.$$
        done &lt; $HOMEDIR/.exclude
    fi
done

mount | grep /backup

if [ $? -ne 0 ]; then
    /sbin/mount /backup
    if [ $? -ne 0 ]; then
        logger -s -p user.err -t backup "Backup drive could not be mounted! Bailing!"
        /sbin/umount -f /backup
        exit 1
    fi
fi

BACKUPDIR="/backup/`date "+%Y-%m-%d"`"
OLDDIR="/backup/`date -v-14d "+%Y-%m-%d"`"

if [ ! -d $BACKUPDIR ]; then
    mkdir $BACKUPDIR
fi

if [ $? -ne 0 ]; then
    logger -s -p user.err -t backup "Could not create backup folder $BACKUPDIR. Bailing!"
    /sbin/umount -f /backup
    exit 1
fi

# We got this far, now we have to run rsync. Rsync is very verbose on purpose

/usr/local/bin/rsync -av --relative --delete /usr/home/ $BACKUPDIR --exclude-from=/tmp/exclude.$$

if [ $? -ne 0 ]; then
    logger -s -p user.err -t backup "rsync failed. Do not trust backup ($BACKUPDIR)"
    /sbin/umount /backup
    exit 1
fi

echo "Backup complete! Deleting oldest backup (14 days ago)"

if [ -d $OLDDIR ]; then
    rm -rf $OLDDIR
fi

/sbin/umount /backup
/bin/rm -f /tmp/exclude.$$

logger -s -p user.notice -t backup "Backup complete at $BACKUPDIR."</pre>

Ninja Remote

Recently I recieved one of the really cool items at ThinkGeek, a Ninja Remote. What this device does is allow control of pretty much any TV that is out there on the market, all in a very small size. Hold the mute button until the TV mutes, let go of the button and you have full control. Volume, Channels, and even the input. And best off all, it contains a off button to turn off the TV.

So, I was heading down to Fry's electronics to pick up an employment application, and I figured they would have a ton of TV's to play with. I walk up to the first one, a Mitsubishi, and took control. First I changed the channel to a cooking channel and turned up the volume really loud. Next I walked over to a row of TV's, all of them JVC. I waited until the TV muted, and once again I was in control. I took a step back, and hit the power button. Every TV just went off. I quickly turned them all back on, and walked on to where I saw an employee trying to sell a Sharp TV. Doing the same trick I had just done, I turned off 3 rows of TV's, about 24 total. I almost die of laughter as the guy looks dumbfounded. Poof and all the TV's were off. Out of nowhere I see some guy come with a voltmeter and he starts plugging it into the power outlets only to conclude that there is still power. (In this mean time I am walking around and have turned off yet another set of TV's) They are unplugging TV's and plugging them back in. Never touching the power button. As if I were an innocent bystander I walk over, hit the power button on one TV and on my remote and have them watch in astonishment as ALL the TV's turn back on. They thank me and I am on my way!

That little device is so much fun. It is awesomely small so most people won't even notice what is up, and thus the surprise of having the TV go off is awesome. One couple was looking at a REALLY sweet Samsung LCD TV and I turned it off. The guy had the remote in his hand but had not pressed any buttons, and he quickly hung the remote back on the TV in the holder and quickly walked off as if to say "T'was not my fault".

Yeah, great fun. Now I need to fill in that application and hopefully get employed :D

UAT has exceeded my expectations

When I moved to Arizona, it was my intention to get the best education I could get in the field I wanted to study (Software Engineering). I was expecting cool professors, people I could relate with, especially with regards to technology. I expected that there would be a clear boundary between mentor and student, and this is one of the many ways UAT has exceeded my expectations. My thought of just about any school includes cliques where certain people stick together keeping others out, where there are jocks or people that feel they are superior to anyone else. Herein I describe further why UAT has exceeded my most daring dreams.

The professors do not want to just lecture, they want someone they can share ideas with, and get new ideas back. It is not straight forward lecture. Much of my classes include thinking, not just critical but also other types of thinking. The professors want to challenge me, and at the same time they want to be challenged as well. They all have their claim to fame, but yet they are accepting of students and their ideas, even if they are wrong at times, or just different. At UAT one can get a friend-to-friend relationship with the mentors, where information can be shared freely and criticism is given in a friendly manner. The professors want to be here, they want to work with students.

I have heard from several other people that I went to High School with, or were College students that their professors would just lecture, would not provide help and in general were entire assholes. When I visited a school a while back that was the impression I got as well (School will not be named). That Professors did not want to have input from students, since they studied long and hard to get their doctorate thus they must be right.

The entire friendship thing is portrayed throughout the entire student body. We are all so called rejects, people that would get shoved around by the jocks, looked down upon as a book worm, we all did not fit in with what people called normal society because we liked to play computer games, program computers and read information online all day. We did not play sports, nor looked like we really could. Even then there are exceptions off course, but what all connects us is the fact that we are all passionate about technology. There are no cliques here, not a single person at UAT is critical of someone else because they look different, act different, think differently. We challenge each other through informal debates as well as trying to prove others wrong.

A friend of mine is currently attending a school up north near New Jersey and he told me that people will not give him the time of day. He does not fit in because he is ahead of the pack, dresses differently and has a different mind set in terms of what he knows. They give him a hard time because he is not like them, and they will not accept someone that is not their equal, or even worse, ahead of them. I had visited the same College when looking around for colleges to attend, and I got just bad vibes while walking around campus. Everyone was separated, no general area where everyone comes together to all do work, or all hang out and enjoy life for a bit between classes. The students were not enthusiastic when it came to presenting their school, and the entire way the staff acted did not "feel" right.

During 2004, while I was at NYLF:Tech I had the opportunity to go visit one of the universities I was hoping to go to, but never quite got my grades up for (ended up good though). This was Berkeley in California. Yes the same Berkeley that created BSD. They had a tour guide who was energetic, was full of passion about what was taught, about the professors, about campus, about the town, and it's location. I also got to meet with faculty and ask questions and they were just like I am now experiencing with UAT, down to earth, ready to be challenged by a student, and ready to challenge them to become the masters like they.

UAT has one thing going for them over the other universities/colleges I have visited, UAT staff is HOT!. Yeah, for some odd reason they have awesome staff that is very good looking. All the staff is younger as well. They have just come from universities themselves, or even if they are a bit older they are passionate about one thing as well, technology and helping us (the students) further their knowledge. Anyone that works for UAT, and is at UAT knows what the mission statement is:

To educate students in the fields of advancing technology to become innovators of the future.

Everyone at the university embodies this mission, as well as the core values:

__Integrity__ We promote positive social responsibility and good global citizenry, and we always act with integrity, honesty and ethics that are above reproach. __Quality through continuous improvement__ We recognize that our University and environment may be defined as a system, and that each of its parts may be simplified, improved, innovated or eliminated. We hold that all parts of a system are interconnected through relationships, and that continuous improvement requires understanding all these relationships. We pioneer new methods, meticulously plan, and rely upon feedback systems. __Life Long learning__ We understand that high performance and constant improvement require continuous learning. We cultivate global-mindedness and promote the highest level of student learning and success. We rigorously seek learning opportunities, initiate and nurture individual growth, and expect ongoing individual growth as well as organizational learning. __Teamwork__ We communicate effectively, reinforce support for each other and build alignment between students, alumni, employers, industry, community and departments as well as individuals. We work in collaborative teams to accomplish superior results through shared understanding. These are the values of the University. What this means is that as long as we are part of this community we will strive in all of our actions and decisions to be positive and honest, continue to work to improve ourselves and our processes, to strive to learn and never be satisfied with our current knowledge and work with each other to achieve results.

Besides all this, who would not want to go to a school where there is an entire contest in the student body that is Ninjas vs. Pirates. Even the staff are in on it. During a presentation given by the Vice-President of the Student Government (yes, capital since they are important) enrolment co-ordinators as well as resident life staff ran into the theatre and stared throwing ninja stars at the presenters and people in the crowd. Jokes are played on students, and students play jokes on staff. It is an equilibrium. Staff and students both have the same amount of fun, and that is how it should be, since it provides a better environment to learn, get information and also be accepted by the people around oneself.

I feel sorry for those of you that actually read all this, but then again good on ya! I just needed to get this off my chest.

Samsung T809 and iSync

I seem to be getting quite a few hits on my blog regarding the Samsung T809 and it is syncing with iSync. This unfortunately does not work, and unless Apple releases a fix, so that it will use the Samsung's supported SyncML.

iSync as of the most current version in Mac OS X Tiger does not support SyncML and thus will not be able to sync contacts or data with the phone.

Luckily there are other options out there, OnSync from OnMadeSoft will sync with your phone. Set the phone in the OnSync preferences to the SGH-D500 and from there on all should be peachy. I have used this to sync my contacts back and forth.

Good luck to all using the Samsung T809, I think it is an awesome phone!

Edit: Looking further into the problem, it seems that SyncML might not be supported on the Samsung T809, and that the only way to sync it will be to use OnSync. iSync should have full support for SyncML, at least that is what people are saying, however the reason it is not universally fixed is because mobile phone manufacturers are having their phones created with flawed implementations of SyncML or require the sending of weird data characters over Bluetooth to enable the internal SyncML server.

Syncing is still not where it should be, especially between devices, unfortunately!