Category Archives: Web Development

Subversion: How do you install and set up Subversion for revision control?

Code revision control is very crucial part of development.  It ensures you know who has changed which files and you are able to rollback to older versions in case new changes are breaking your website page or entire project.  There are few options for you if you are looking into setting up code revision control.  My preferred option is Subversion.  Subversion is an open-source revision control system which is becoming more and more popular every day.  It is very easy to install and setup your project under Subversion.  If you want detailed instructions, please see the Subversion Book.  They do a great job explaining what revision control is, what are the different types of revision control are out there, detailed instructions on installing subversion, doing administrator tasks, etc.  Here are instructions on how to set up Subversion under CentOS.  Same instructions apply to most linux distributions. 

YUM Install 

Easiest way to install subversion is via “yum” by typing:

yum install subversion

Once install is done, confirm it by typing “svn” at the prompt and you should get:

Type 'svn help' for usage.

At this point you can skip to “Setting up Subversion repository” part.

SOURCE Install

Obtain source from:  Subversion.  At the time of writing, latest (1.4.3) version can be obtain from this link:  http://subversion.tigris.org/downloads/subversion-1.4.3.tar.gz

tar zxf subversion*
cd subversion*
./configure
make && make install

after everything goes well, type svn at the prompt and you should get:

Type 'svn help' for usage.

Setting up Subversion repository

adduser svnusers
mkdir repos/branches -p
mkdir repos/trunk -p
mkdir repos/tags -p
mkdir /svn
svnadmin create /svn/demorepo
svn import --message "Initial set up" repos file:///svn/demorepo
cd /svn/demorepo
chown .svnusers . -R
chmod 775 * -R
chmod +s db

Thats all there is to it to setup Subversion repository.  At this point, you should add any users you want to have access to the repository by adding them to “svnusers” group.

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

Using url rewrite engine to serve default avatar to avoid checking for file in PHP

One of the great features of Apache is URL Rewrite. It has helped me to do so many different tasks that I had to blog about this and share the love. One of the things I used it for recently was to provide a solution for a simple problem which was being checked at PHP level.

Problem: Check an avatar file for user, if it exists on file system, use it, else show them a default avatar image.

Solution 1: edit all of the php files which does anything with avatars to put that check in. Not the best way to go and easily forgotten if new piece of code is put in which may cause showing broken images on the site. Therefore solution 2 is more appropriate.

Solution 2(preferred): Use Apache URL Rewriterule feature to take care of this issue. Put .htaccess file in the directory where your avatars are with following content:

RewriteEngine On
RewriteBase /images/avatars/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /images/avatars/default.gif [L]

Lets look at this line at a time.

First line turns the Rewrite engine on just in case if it wasn’t turned on already.
Second line sets the base to the directory where we are going to be working in.
Third line is where we check to see if the file requested exists or not (! negates -f which is is file?)
Fourth line is where it redirects you to default avatar if requested avatar doesn’t exist.

There you go. Quick, robust, non-code solution. URL Rewrite engine is VERY powerful. What I showed here barely scratches the surface of all the features URL Rewrite engine has to offer. Apache software foundation does a great job providing documentation and numerous examples you may want to look at. The URL Rewriting Guide is one of the resources there.

MySQL wait_timeout setting

We were having issues with mysql threads where they would be in sleep mode and wouldn’t die off for long time. At the same time we started having issues with our servers where the load will spike and eventually server will come to halt unless we killed all the apache processes and restarted apache (which seems to be the hung application). We traced it back eventually and noticed that the time when server hung was when it burned through all the ram and was using up all the swap also. So we started to work backwards and tried to resolve one thing at a time. We started with MySQL. We put in wait_timeout = 30 in to my.cnf and restarted mysql. Than I closely watched the server for few hours and noticed that we didn’t have any more of those sleep connections. GREAT! A work around until we get to bottom of whats causing this. That was on Friday. Sat we started noticing different problem. Problem worsened and we started to look into what might’ve caused it and found out that we had a script which was pulling row at a time, processing it, and deleting the row. Except, it was never getting to delete the row due to timeout would kick in and close the connection. We found this out when we watched error logs and saw: Mysql has gone away message.

We took out the wait timeout and everything seems to started to work fine. Did anybody ever notice this behavior where you would loose connection to the mysql server due to timeout? The script which processes line by line and deletes line by line takes fraction of second to process that particular line. Does wait timeout starts counting from the starting of the connection? Does it mean that wait timeout is actually a max connection time limit? Suggestions/comments?

Edit 5/31/09: Friend of mine was getting this error: database error: Lost connection to MySQL server during query
he got around it by adjusting wait_timeout setting.