Installing PHP MongoDB Driver in Debian/Ubuntu

# install apache php
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
sudo apt-get install apache2 php5 libapache2-mod-php5

# install php-pear
apt-get install php5-dev php-pear

# install mongo
pecl install mongo

The pecl command will end with the message like this.

Build process completed successfully
Installing '/usr/lib/php5/20100525/mongo.so'
install ok: channel://pecl.php.net/mongo-1.4.2
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

Then, add “extension=mongo.so” to /etc/php5/apache2/php.ini

Advertisements

Git push error

I got the error when pushing by HTTPS to my home server.

Initialized empty Git repository in /xxxx/xxxx/.git/
Password: 
error: server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none while accessing https://xxxx@xxxx.xxxx/xxxx/xxxx.git/info/refs

fatal: HTTP request failed

To fix it, execute the following command or add it to .bashrc

export GIT_SSL_NO_VERIFY=1

Keep displaying previous URL to a page in Lithium php framework

Here is the php code to cache the previous access URL (for example an URL of a search request) so that you want to display it again whenever the users go back to the page.

Activate Session in Lithium

// file: app/config/bootstrap.php
// uncomment this
require __DIR__ . '/bootstrap/session.php';

// file: app/config/bootstrap/session.php
use lithium\storage\Session;
Session::config(array(
	'default' => array('adapter' => 'Php')
));

The below code can be put in, for example, your controllers for redirecting to the previous saved URL.

        $protocol = strtolower($this->request->env('SERVER_PROTOCOL'));
        $protocol = substr($protocol, 0, strpos($protocol, "/")) . "://";
        $url = $protocol . $this->request->env('HTTP_HOST')
                         .$this->request->env('REQUEST_URI');
        $referer = $this->request->env('HTTP_REFERER');
        $baseURL = $protocol . $this->request->env('HTTP_HOST') . "/" . $this->request->url;
        if ($url == $baseURL || substr($referer, 0, strlen($baseURL)) != $baseURL) {
            $searchURL = Session::read('Files.url', array());
            if ($searchURL != '' && $url != $searchURL) {
                $this->redirect($searchURL);
            }
        } else {
            Session::write('Files.url', $url, array());
        }

Have fun then!

Write a Cache model in Lithium php framework with mongodb

This is a simple code for creating cache data in mongodb using Lithium php framework.
The data will be kept 1 hour as default.

<?php

namespace app\models;

class Caches extends \lithium\data\Model {
	/**
	*
	* @param unknown $type this is to specify the category of the data
	* @param unknown $key this is the key of the cache
	* @return return the cache if it exists and is not expired
	*/
	public function getArrayCache($type, $key) {
	 // Check cache time
		$cache = Caches::find('first', array(
				'fields' => array('created'),
				'conditions' => array(
						'type' => $type,
						'key' => $key
				)
		));

		if ($cache == null) {
			return null;
		}
		$currentTime = date('Y-m-d H:i:s');
		$duration = strtotime($currentTime) - strtotime($cache->created);
		if ($duration > 60 * 60 * 1 /* 1 hour */) {
		    // Delete expired cache
			Caches::remove(array(
			'type' => $type,
			'key' => $key
			));

			return null;
		}

		$cache = Caches::find('first', array(
				'fields' => array('cache'),
				'conditions' => array(
						'type' => $type,
						'key' => $key
				)
		));
		return json_decode($cache->cache, true);
	}

	/**
	 *
	 * @param unknown $type this is to specify the category of the data
	 * @param unknown $key this is the key of the cache
	 * @param unknown $arrayData an array or object wanted to cache
	 */
	public function putArrayCache($type, $key, $arrayData) {
		Caches::remove(array(
			'type' => $type,
			'key' => $key
		));
				
		$cache = array(
				'type' => $type,
				'key' => $key,
				'created' => date('Y-m-d H:i:s'),
				'cache' => json_encode($arrayData)
		);
		Caches::create($cache)->save();
	}
}

?>

Using of cURL

I found some useful use of cURL in this article.
http://www.semicomplete.com/blog/geekery/ssl-latency.html
So, I collect the sample commands here.

% fping -q -c 5 www.csh.rit.edu
www.csh.rit.edu : xmt/rcv/%loss = 5/5/0%, min/avg/max = 112/115/123

% seq 5 | xargs -I@ -n1 curl -so /dev/null -w "%{time_connect}\n" http://www.csh.rit.edu
0.117
0.116
0.117
0.116
0.116

% curl -kso /dev/null -w "tcp:%{time_connect}, ssldone:%{time_appconnect}\n" https://www.csh.rit.edu
tcp:0.117, ssldone:0.408

terminal1 % sudo tcpdump -ttttt -i any 'port 443 and host www.csh.rit.edu'
...

terminal2 % openssl s_client -connect www.csh.rit.edu:443
...

Tcpdump output trimmed for content:

# Start TCP Handshake
00:00:00.000000 IP snack.home.40855 > csh.rit.edu.https: Flags [S] ...
00:00:00.114298 IP csh.rit.edu.https > snack.home.40855: Flags [S.] ...
00:00:00.114341 IP snack.home.40855 > csh.rit.edu.https: Flags [.] ...
# TCP Handshake complete.

# Start SSL Handshake.
00:00:00.114769 IP snack.home.40855 > csh.rit.edu.https: Flags [P.] ...
00:00:00.226456 IP csh.rit.edu.https > snack.home.40855: Flags [.] ...
00:00:00.261945 IP csh.rit.edu.https > snack.home.40855: Flags [.] ...
00:00:00.261960 IP csh.rit.edu.https > snack.home.40855: Flags [P.] ...
00:00:00.261985 IP snack.home.40855 > csh.rit.edu.https: Flags [.] ...
00:00:00.261998 IP snack.home.40855 > csh.rit.edu.https: Flags [.] ...
00:00:00.273284 IP snack.home.40855 > csh.rit.edu.https: Flags [P.] ...
00:00:00.398473 IP csh.rit.edu.https > snack.home.40855: Flags [P.] ...
00:00:00.436372 IP snack.home.40855 > csh.rit.edu.https: Flags [.] ...

# SSL handshake complete, ready to send HTTP request. 
# At this point, openssl s_client is sitting waiting for you to type something
# into stdin.


% URL=https://s-static.ak.facebook.com/rsrc.php/zPET4/hash/9e65hu86.js
% curl  -w "tcp: %{time_connect} ssl:%{time_appconnect}\n" -sk -o /dev/null $URL -o /dev/null $URL
tcp: 0.038 ssl:0.088
tcp: 0.000 ssl:0.000

% URL=https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
% curl  -w "tcp: %{time_connect} ssl:%{time_appconnect}\n" -sk -o /dev/null $URL -o /dev/null $URL
tcp: 0.054 ssl:0.132
tcp: 0.000 ssl:0.000

Debian Source List

To find the resource list

http://debgen.simplylinux.ch/

Sample source list for Debian Wheezy

deb http://ftp.jp.debian.org/debian stable main contrib non-free
deb-src http://ftp.jp.debian.org/debian stable main contrib non-free

deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
deb-src http://ftp.debian.org/debian/ wheezy-updates main contrib non-free

deb http://security.debian.org/ wheezy/updates main contrib non-free
deb-src http://security.debian.org/ wheezy/updates main contrib non-free

If apt-get update says that it cannot find some ID, run this

apt-get install debian-keyring debian-archive-keyring
apt-key update

Git Error: gnutls_handshake() failed

Error

error: gnutls_handshake() failed: A TLS packet with unexpected length was received. while accessing ...
fatal: HTTP request failed

Solution: need to rebuild git with openssl

sudo apt-get install build-essential fakeroot dpkg-dev
mkdir ~/git-openssl
cd ~/git-openssl
sudo apt-get source git
sudo apt-get build-dep git
sudo apt-get install libcurl4-openssl-dev
sudo dpkg-source -x git_1.7.9.5-1.dsc
cd git_1.7.9.5 8.
sudo vi debian/control
# replace libcurl4-gnutls-dev with libcurl4-openssl-dev
sudo dpkg-buildpackage -rfakeroot -b
# if this is failed, remove TEST=test from debian/rules
sudo dpkg -i ../git_1.7.9.5-1_i386.deb

This is the solution from http://askubuntu.com/questions/186847/error-gnutls-handshake-falied