« Posts under sysadmin

alias with argument in bash shell

Instead of running “ssh -p 1234 -l shelluser serverX.example.com”, you could do “myssh serverX”. To do that, you can put this into your .bash_profile as an alias:

sshas() { ssh -p 1234 -l shelluser “$1”.example.com; }

2 tips – Check PHP syntax and SVN move multiple files

This is what I have in my .bash_profile. It check for all the PHP files, recursively, for any syntax error.

alias php_check_syntax='find . -type f -name \*.php -exec php -l {} \;'

This other tip is to move multiple files in SVN. You can make it a script if you want.

for i in *.test.php ; do svn move $i tests/ ; done

XAMPP vs. doing it your own

Thinking that I would have more control by setting up the packages by myself, I was wrong. After running into the problem with pre-installed PHP by MacOS X Leopard does not have GD, IMAP and other components, I tried playing with fink, MacPorts and manual compiling. LAMP still rules and not “MAMP” for good reasons. Although Mac is quite dev-friendly, it still takes hours searching and setting up. I find the binaries under Linux/CentOS (eg: yum install/update php-gd) much easier to deal with.

I guess I learned from the process but it would be much simpler to go with XAMPP. The conf files are in one place, easy to find and getting it up and running is really simple. If you plan to have a dev environment, don’t bother with the manual installation that can take hours. Now this Mac is ready for some development.

Adding a second hard drive under Linux/CentOS

Check out the new new drive

In this example, you will see I have 2x500GB drives, sda and sdb. sda already in used and sdb is new.

[root@s3 mathie]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       60801   488279610   8e  Linux LVM

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Partition the new drive

For my purpose, I want the whole sdb to be /home, your partitioning scheme might be different, but fdisk should be very easy to use.

[root@s3 mathie]# fdisk /dev/sdb

The number of cylinders for this disk is set to 60801.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Create a new partition
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-60801, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-60801, default 60801):
Using default value 60801
Check the new drive
Command (m for help): p

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       60801   488384001   83  Linux
Write and save
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Format the new partition(s)

[root@s3 mathie]# /sbin/mkfs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
61063168 inodes, 122096000 blocks
6104800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
3727 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Mount the new drive

mkdir /home
mount /dev/sdb1 /home

#add this into /etc/fstab for auto-mount on booting up
/dev/sdb1               /home                   ext3    defaults        0 0

Getting lighttpd RPM

There are many places you can download the latest lighttpd RPMs:


EL4: http://ftp.freshrpms.net/pub/freshrpms/redhat/testing/EL4/lighttpd/
EL5: http://www.kevindustries.com/media/kw/files/linux/lighttpd/RPMS/EL5/
EL5 x86_64: http://linuxwave.blogspot.com/2007/08/installing-lighttpd-in-centos-5-for.html
1.4.17: http://www.kevindustries.com/media/kw/files/linux/lighttpd/RPMS/

Stress test a new server

Got a new server and want to stress test its CPU & disk? Run Folding@Home or other computing service and also BitTorrent (download some Linux distros like Fedora, CentOS). Testing and yet help the community.

Folding @ Home

wget http://www.stanford.edu/group/pandegroup/folding/release/FAH6.02-Linux.tgz
tar -zxf FAH6.02-Linux.tgz
./fah6 --config
echo "./fah6 -smp -verbosity 9 $* &" > fah
chmod +x fah
./fah > /dev/null &


Download RPM packages from:

nohup launchmany-console –saveas_style 1 –max_upload_rate 600 –display_interv
al 5 . > torrent.log &

How to automatically reboot after a kernel panic?

You should investigate the root cause when possible but if the machine needs to be up and running again without much interruption, you can have it reboot when panicking after a number of seconds. Put this into /etc/sysctl.conf

kernel.panic = 60

pecl and memory limit error

If you run “pecl install [something]” and get this error: “Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate xxx bytes)”, you want to change “/usr/bin/pecl” (or run “locate pecl” to see where it is) and specify a larger memory limit.

exec /usr/bin/php -C -n -q -d include_path=/usr/share/pear \
    -d output_buffering=1 /usr/share/pear/peclcmd.php "$@"


exec /usr/bin/php -C -n -q -d include_path=/usr/share/pear \
    -d memory_limit=16M -d output_buffering=1 /usr/share/pear/peclcmd.php "$@"

ip_conntrack and dropped packets

For busy servers, ip_conntrack can fill up quickly and must be monitored or you will get intermittent packet drops. Check var/log/messages for these error messages. Couple values can be adjusted to the kernel:

more /proc/sys/net/ipv4/netfilter/ip_conntrack_count
more /proc/sys/net/ipv4/netfilter/ip_conntrack_max
=> count should be less than max, if it's near the maximum value, increase max

more /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established
=> default 5 days, might want to lower it

echo 0 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_loose

mod_deflate bug

Hmm, another day, another bug: #33499. This time it is mod_deflate and PHP don’t play well together. PHP makes a GIF/JPG file and yet mod_deflate still compress it. Solution: use mod_filter (which only available from 2.1), or disable Apache’s compression for PHP files and let PHP do the compression via ob_start(‘ob_gzhandler’);