Tuesday, May 22, 2012

Create AD-HOC in debian on virtualbox when host internet sharing is disabled

For some reason my Windows Internet Sharing is disabled by system administrator, yet I still want to use ad-hoc for my mobile devices. So I decided to use virutalbox + linux to create a wireless ad-hoc server.

I chosed debian net installation disk because it is small & clean, I dont need any GUI evironment so I choosed a minimum installation with ssh server, added a port forward on port 22 in virtualbox so I can putty to 22 to access the virtual machine.

You need to install your wireless card driver on linux, this progress is pain, you can either download the linux driver from official website or check if it is ndiswrapper supported from http://sourceforge.net/apps/mediawiki/ndiswrapper/index.php?title=Category:WORKS

You will need to add the default module into the /etc/modprobe.d/blacklist.conf & loaded the module to /etc/modules after installation. I received a RTL8188CUS wireless card, it is tiny & luckly the official website provides the download of the driver for linux.

you prbably need to install build-essential & some packages first.

apt-get install build-essentialapt-get install dnsmasq wireless-tools

Edit /etc/sysctl.conf
unmment the line


Edit your /etc/network/interfaces

auto wlan0iface wlan0 inet staticaddress test_essidwireless-mode ad-hocwireless-key 1234567890 #this is a WEP password, for WPA please check herewireless-channel 11

Edit /etc/dnsmasq.conf

echo 1 > /proc/sys/net/ipv4/ip_forwardiptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE/etc/init.d/networking restartservice dnsmasq restart

Now try to connect the device to the ad-hoc, wireless on the fly

Saturday, May 19, 2012

Search and replace in Linux

Search in multiple files & replace in terminal

grep -lr -e 'oldword' * | xargs sed -i 's/oldword/newword/g'

Search & replace in vi

esc :%s/oldword/newword/g

Search & replace in emacs

C-s Search forwardC-r search backwardC-g return to where search started (if you are still in search mode)M-% query replaceSpace or y replace this occurenceDel or n don't replace. only replace this & exit (replace), replace & pause (resume with Space or y)! replace all following occurences^ back to previous matchRETURN or q quit replaceSearch/Replace with regular expressionsCharacters to use in regular expressions:^ beginning of line$ end of line. single char.* group or null of chars\< beginning of a word\> end of a word[] every char inside the backets (for example [a-z] means every small letter)M C-s RETURN search for regular expression forwardM C-r RETURN search for regular expression backwardM C-s incremental searchC-s repeat incremental searchM C-r incremental search backwardsC-r repeat backwardsM-x query-replace-regexp search & replace

Friday, May 18, 2012

How to create distributed/mirrored dynamic scripting website (Step - 4 Region detection and redirection)

Step four: Region detection & redirection
First of all, download the latest database

wget software77.net/geo-ip/?DL=1 -O /path/IpToCountry.csv.gz

Create a table in your website database & import the data from the csv file

CREATE TABLE IF NOT EXISTS `ip2country` (  `start` int(10) unsigned NOT NULL DEFAULT '0',  `end` int(10) unsigned NOT NULL DEFAULT '0',  `registry` varchar(50) NOT NULL,  `assigned` varchar(50) NOT NULL,  `a2` char(2) NOT NULL DEFAULT '',  `a3` char(3) NOT NULL DEFAULT '',  `country` varchar(100) NOT NULL DEFAULT '',  PRIMARY KEY (`start`,`end`),  KEY `a2` (`a2`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;LOAD DATA LOCAL INFILE 'YOUR_PATH/IpToCountry.csv' INTO TABLE ip2country FIELDS TERMINATED BY ',' ENCLOSED BY '"'  LINES TERMINATED BY '\n';

Now just need add a few line of php code at the beginning of index.php file to handle the redirect.

//If the visitor is search engine spider, then do not redirect.function check_if_spider(){	$spiders = array('Googlebot', 'Yammybot', 'Openbot', 'Yahoo', 'Slurp', 'msnbot', 'ia_archiver', 'Lycos', 'Scooter', 'AltaVista', 'Teoma', 'Gigabot', 'Googlebot-Mobile');	foreach ($spiders as $spider)	{		if (eregi($spider, $_SERVER['HTTP_USER_AGENT']))		{			return TRUE;		}	}	return FALSE;}function checkip(){                if(check_if_spider()) return;		$client_ip=ip2long($_SERVER['REMOTE_ADDR']);		$rs = $db->query("select `a2` from `ip2c` where `start` <= $client_ip & `end` >= $client_ip");		$row = $db->fetch_array($rs);		//$row[0] will be the country code.                switch($row[0]){                    case 'US':                        //..301 Redirection                        header("HTTP/1.0 301 Moved Permanently");			header("Location: http://us.example.com".$_SERVER['REQUEST_URI']);                     break;                    default:                       //... 301 Redirection                    break;                }	}}checkip();

How to create distributed/mirrored dynamic scripting website (Step - 3 Security)

Step three: Security
1. Firewall policy, use iptables to restrict specific ip & port.

2. When create replication user, use ‘user’@’ip’ rather than ‘user’@’%’

3. Use authorized keys for rsync so you can run rsync without a password. To create a ssh authoruzed key:
On source host

ssh-keygen -t rsa

Copy id_rsa.pub to destination host
Then execute the destination host

cat id_rsa.pub >> ~/.ssh/authorized_keys

Next: How to create distributed/mirrored dynamic scripting website (Step – 4 Region detection & redirection)

How to create distributed/mirrored dynamic scripting website (Step - 1 File synchronization)

This tutorial introduce a effortless way to build a distributed dynamic scripting website between Linux servers in two different zone.

To build up a dynamic website mirror for different zone/region/country without using Content Delivery Network, you should consider:

  1. File Synchronization
  2. Database Synchronization
  3. Security
  4. Region Detection And Redirection

Step one: File Synchronization

We will use rsync for file synchronization

rsync --progress --stats --compress --rsh=/usr/bin/ssh --recursive --times --perms --links --exclude "*bak" --exclude "*~" --exclude "config.php"  /var/www/path/* DESTINATION:/var/www/path/

It is recommended to create a batch file & run this command every 5 minutes, you may need to detect if rsync is already running, checkout here to find out how to detect if a process is already running. You may have muptiple hosts, yet the principle should be the same.

Next: How to create distributed/mirrored dynamic scripting website (Step – 2 Database synchronization)

How to create distributed/mirrored dynamic scripting website (Step - 2 Database synchronization)

Step two: Database Syncrhonization
This is the key of the dynamic website synchronization, I will use mysql server 5.x for example, if you are using a different database server, you will need to find out how to configure their replication.

1. Configure Master & Slave Server
Create a mysql user for replication


Edit my.cnf file [mysqld] section

2. Copy the database:
We need to lock table on master server before we dump the database


Write down the bin log file record & the postion & leave this connection open, then create a new ssh connection to the master server & dump the databse with:

#mysqldump -u root -p --database | gzip -c > all_database.sql.gz

Copy all_database.sql.gz to slave server & extract the files & import into slave database

source all_database.sql

An alternative way is to copy the content of folder /var/lib/mysql/data/* from master to slave.

Now you received exactly the same database on the salve server, then run SQL command on slave server

CHANGE MASTER TO master_host='MASTER_IP', master_user='user_rep', master_password='password',master_log_file='mysql-bin.XXXXXX',master_log_pos=XXXXX;START SLAVE;SHOW SLAVE STATUS; 

If you see Thread IO & Thread SQL are both running, then your database should be synchronized
Now go back to master server mysql session & run


Next: How to create distributed/mirrored dynamic scripting website (Step – 3 Security)

nginx rewrite rule for opencart

It took me a while to find out the solution, yet at last, I found it is pretty easy, the admin folder just need to take a bit extra care. This rewrite rule works moreover work with SEO url settings enabled.

Tested with opencart 1.5.13

   location / {        if ($host != 'www.example.com') {                rewrite ^/(.*)$ http://www.example.com/$1 permanent;        }        try_files $uri @opencart;    }    location @opencart {        rewrite ^/(.+)$ /index.php?_route_=$1 last;    }        location /admin/ {        index index.php;        }    location ~* (\.(tpl|ini))$ {          deny all;    }

nginx rewrite rule for codeigniter

This is my nginx configuration file, which obtain the assist from http://www.farinspace.com/codeigniter-nginx-rewrite-rules/

Here are the few things you MUST take care:
1. All rewrite rule & root must set in server scope, NO location \{} nor ~\.php${} !!!
2. In codeigniter application/config/config.php file

  • set $config[‘index_page’] = “”;
  • set $config[‘uri_protocol’] = “REQUEST_URI”;

Here is the rewrite rule that you need

    #page is my default controller, you will need to alter it to yours       if ($request_uri ~* ^(/page(/index)?|/index(.php)?)/?$)    {        rewrite ^(.*)$ / permanent;    }    # removes trailing "index" from all controllers    if ($request_uri ~* index/?$)    {        rewrite ^/(.*)/index/?$ /$1 permanent;    }    # removes trailing slashes (prevents SEO duplicate content issues)    if (!-d $request_filename)    {        rewrite ^/(.+)/$ /$1 permanent;    }    # removes access to "system" folder, moreover allows a "System.php" controller    if ($request_uri ~* ^/system)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap    if (!-e $request_filename)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }

For more nginx rewrite rule, please visit http://www.x-note.co.uk/category/nginx/

Thursday, May 17, 2012

Delete all root mail

Here is the command to remove all root mail content

#cat /dev/null > /var/spool/mail/root

Check if a process is running in Linux

Here is a bash script that simply check if a process is running or not.

#!/bin/bashif [ "$(pidof process_name)" ] then  echo "process is running"else  echo "process is not running"fi

Monday, May 14, 2012

Run scp, wget Background

It is really effortless to run scp & wget in background mode.

#scp -r large_file user@example.com..... #wget http://example.com/large_file

Press CTRL + Z

and then type


Your process is now running in the background

Wednesday, May 2, 2012

Create Gradient with CSS

Here is an example of creating gradient with CSS

background: #f7f7f7; /* Old browsers */background: -moz-linear-gradient(top,  #f7f7f7 0%, #eff1f0 33%, #e3e3e3 73%, #dddddd 100%); /* FF3.6+ */background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f7f7f7), color-stop(33%,#eff1f0), color-stop(73%,#e3e3e3), color-stop(100%,#dddddd)); /* Chrome,Safari4+ */background: -webkit-linear-gradient(top,  #f7f7f7 0%,#eff1f0 33%,#e3e3e3 73%,#dddddd 100%); /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(top,  #f7f7f7 0%,#eff1f0 33%,#e3e3e3 73%,#dddddd 100%); /* Opera 11.10+ */background: -ms-linear-gradient(top,  #f7f7f7 0%,#eff1f0 33%,#e3e3e3 73%,#dddddd 100%); /* IE10+ */background: linear-gradient(top,  #f7f7f7 0%,#eff1f0 33%,#e3e3e3 73%,#dddddd 100%); /* W3C */filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f7f7f7', endColorstr='#dddddd',GradientType=0 ); /* IE6-9 */