Archive for the 'Linux/Unix' Category

Changing the default config editor in Ubuntu

If you are following the articles at Slicehost, then you may notice that PickledOnion uses nano as the default editor. I somehow like vi more than nano (not ready for a debate ;-) ) and was looking for a way to make vi the default config editor. After some googling, I found how to do it.

I am writing it down here so that all I have to remember is that I just need to search my blog if I need to do it again in future.

Okay the command you have to use is (I am assuming that you are not logged in as root, which is the recommended approach)

sudo update-alternatives --config editor

And then press the number corresponding to the editor which you want to use. Below is the screenshot of how it looked in my slice.

Changing default config editor in Ubuntu

Rotating Apache log files using Cronolog

I must confess that I am a stats freak. If you are a long time reader of my blog, then you would have known that by now yourself. ;-) This explains the reason why I want to preserve my Apache log files in spite of using a variety of stat services like Google Analytics, WordPress stats, statscounter, performancing metrics (before it was closed).

The default Apache configuration preserves the log files only for the last 10 days, but I wanted to permanently archive this files. After some searches in Google I came across an excellent program called Cronolog. Cronolog is a simple filter program which writes each log entry to a separate log file named after the filename format specified. You can use a variety of parameters like current date, time etc to define the filename template.

First we have to install cronolog, either by using aptitude or by downloading it from its download page. Then you have to change the log file name path in the virtual host file. (In Ubuntu Gusty, the virtual host files are situated in the path /etc/apache2/sites-enabled). I am using the following file format for this blog
# Custom log file locations
LogLevel warn
ErrorLog "|/usr/sbin/cronolog /path/to/logs/%Y/%m/%Y-%m-%d-sudarmuthu.com-error.log"
CustomLog "|/usr/sbin/cronolog /path/to/logs/%Y/%m/%Y-%m-%d-sudarmuthu.com-access.log" combined

which will store my log files in separate folders for each year and for each month, like the below hierarchy
/2007/12/2007-11-01-sudarmuthu.com-access.log
/2007/12/2007-11-02-sudarmuthu.com-access.log
......
/2008/01/2008-01-01-sudarmuthu.com-access.log
/2008/01/2008-01-02-sudarmuthu.com-access.log
......

You can use a variety of modifiers for the filename and I have documented some of them in the below table. You can get more information from its documentation.

Specifier Description
Time fields
%H hour (00..23)
%I hour (01..12)
%p the locale’s AM or PM indicator
%M minute (00..59)
%S second (00..61, which allows for leap seconds)
%X the locale’s time representation (e.g.: “15:12:47″)
%Z time zone (e.g. GMT), or nothing if the time zone cannot be determined
Date fields
%a the locale’s abbreviated weekday name (e.g.: Sun..Sat)
%A the locale’s full weekday name (e.g.: Sunday .. Saturday)
%b the locale’s abbreviated month name (e.g.: Jan .. Dec)
%B the locale’s full month name, (e.g.: January .. December)
%c the locale’s date and time (e.g.: “Sun Dec 15 14:12:47 GMT 1996″)
%d day of month (01 .. 31)
%j day of year (001 .. 366)
%m month (01 .. 12)
%U week of the year with Sunday as first day of week (00..53, where week 1 is the week containing the first Sunday of the year)
%W week of the year with Monday as first day of week (00..53, where week 1 is the week containing the first Monday of the year)
%w day of week (0 .. 6, where 0 corresponds to Sunday)
%x locale’s date representation (e.g. today in Britain: “15/12/96″)
%y year without the century (00 .. 99)
%Y year with the century (1970 .. 2038)

Accessing MySQL safely using port forwarding with PuTTY

During my days with dreamhost, in order to access MySQL from my local machine, I used to add my ip to the allowed host list. Even though security is compromised here, I really liked to use HeidiSQL for accessing MySQL database server instead of the built in MySQL console. But after my move to SliceHost, I found a little trick using which I can continue to use HeidiSQL from my local machine without adding my ip to the allowed host list.

This nice little trick is called port forwarding. Let me show you how I configured PuTTY so as to enable port forwarding.
First install MySQL and then configure SSH to use key based authentication and change the default port by following the articles at Slicehost. After installing MySQL and configuring SSH, download and install PuTTY from its download page. I recommend you to download the zip file containing all the files.

Then create a new session in PuTTY by entering the ip address and also the port. Then choose Connection -> SSH -> Tunnels. In the source port field enter a valid port number like 8600. In the destination field enter the value 127.0.0.1:3306. 3306 is the default port in which MySQL runs. The reason why I asked you to enter a different port in the source is that, in future if you run a MySQL server in your local machine for testing, it will clash with your port forwarding. Click the Add button and then start the session. Don’t forget to save the session.

PuTTY

Now open your favourite MySQL GUI client. Mine is HeidiSQL. In the connection settings, enter 127.0.0.1 as the Hostname and enter the port which you specified in the source field in PuTTY (8600) as port. Also enter your username, password, default database name and the click connect.

PuTTY

Now the request which goes to port 8600 of your local machine is forwarded to port 3600 of your MySQL server by PuTTY and you can safely use a GUI client for MySQL without adding any ip to the allowed host list. Note that it will work only when PuTTY is having the session opened.

I hope this is of help to you and let me know how it is working for you. Happy PuTTYing ;-)

Good Bye Dreamhost, Hello SliceHost

Finally, I am out of Dreamhost. My annual plan expired last November and I changed to monthly plan and was searching for a reason to change. The incident which happened last week became the final nail in the coffin and I am out of it now.

I have faced the bad neighbour problem and have had some outages because of it occasionally, but to be fair to Dreamhost, the problems I faced with them were not as bad as the ones which I had with my previous web host.

The other reason for me to change the host is that I have got fed up with shared hosting and wanted to dive into VPS. I thought of going with MediaTemple, but they were expensive and then tried GoDaddy Linux Virtual Dedicated plan. It really sucked! I was not getting the kind of control which I was expecting. More over the control panel itself took too much memory of 256 MB and the server was always at its knees.

It was then I read the review about SliceHost from Hosting Fu. I was doing some research and the moment I found the excellent article repository, I immediately went and registered. Right from the OS, everything is in your control and you can configure or install any software which you want. This is the type of control I was looking for. I selected Ubuntu Gusty as OS and have so far installed and configured Apache, MySQL, PHP, Subversion and Ruby on Rails. Finally I can get my hands dirty with Ruby on Rails, (which was actually one of my resolutions for 2007 :) )

I will keep you guys posted about my experience with SliceHost and also will post the interesting code snippets or configuration tricks which saves me some time. I am back to slicing my slice from slicehost :)

How best Linux compared with windows