Tuesday, March 31, 2015

configure php5.3 php-fpm and nginx from source on ubuntu 14.04 lts server

Installing nginx 1.6.2 on ubuntu 14.04

--before everything lets clean up the old installed nginx.
sudo apt-get autoremove nginx
sudo apt-get --purge remove nginx
sudo apt-get autoremove && sudo apt-get autoclean
sudo find / | grep nginx | sudo xargs rm –rf
--now add the nginx stable repository
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update && sudo apt-get -f install nginx
sudo dpkg -l | grep nginx
--check you machine ip and mac address
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

In order to install required modules for your drupal/site compatibility sake, it is required to build nginx from source.
Nginx Upload Progress Module
Create /opt/upload directory; cd /opt/upload; init git; git clone –url can be download from nginx website: http://wiki.nginx.org/HttpUploadProgressModule
Get Nginx sources and dependencies
Alright, now the parts that might be more unfamiliar to you. We're going to get the Nginx packages from ppa:nginx/stable, adjust them, rebuild them, and then reinstall Nginx.
This assumes you've added the ppa:nginx/stable repository already. Ensure that the file /etc/apt/sources.list.d/nginx-stable-trusty.list exists. Not that mine says "trusty" because I'm using Ubuntu Trusty (14.04).
First, edit the PPA's sources file and ensure the deb-src directive is not commented out. This will let us get the sources files:
$ cat /etc/apt/sources.list.d/nginx-stable-trusty.list
deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
# deb-src http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
If, like mine, your deb-src line is commented out, edit that file and remove the #:
Then update:
$ cat /etc/apt/sources.list.d/nginx-stable-trusty.list
deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
deb-src http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
sudo apt-get update
Now we can continue to get the source packages of Nginx, adjust it, build it and install it!
First we'll get the source package and any needed system dependencies:
# Install package creation tools
sudo apt-get install -y dpkg-dev
sudo mkdir /opt/rebuildnginx
cd /opt/rebuildnginx
# Get Nginx (ppa:nginx/stable) source files
sudo apt-get source nginx
# Install the build dependencies
sudo apt-get build-dep nginx
Then we will adjust the build files to add the extra modules. As of the time of this writing, the latest stable Nginx installed on Ubuntu Trusty is 1.6.2.
$ ls -lah /opt/rebuildnginx
drwxr-xr-x 10 root root   4096 Dec 14 16:37 nginx-1.6.2/
-rw-r--r--  1 root root 934244 Dec 14 02:10 nginx_1.6.2-5+trusty0.debian.tar.gz
-rw-r--r--  1 root root   2798 Dec 14 02:10 nginx_1.6.2-5+trusty0.dsc
-rw-r--r--  1 root root 804164 Sep 18 21:31 nginx_1.6.2.orig.tar.gz
To adjust add the modules into the build, edit the /opt/rebuildnginx/nginx-1.6.2/debian/rules file.

The ppa:nginx/stable has multiple Nginx packages available: common, light, full and extras. When you run apt-get install -y nginx, you're installing the "full" version.

The "common" package is the base install upon which the others are built. The "lighter" package, however is in fact lighter than the "common" package due to the use of the --without-* flags.

For this example, we'll simply edit the "full" version to add our modules. This assumes we'll keep using the "full" package as normal when installing Nginx.

From the rules file, here's the "full" version configuration with our 2 new modules added at the end:
full_configure_flags := \
                        $(common_configure_flags) \
                        --with-http_addition_module \
                        --with-http_dav_module \
                        --with-http_geoip_module \
                        --with-http_gzip_static_module \
                        --with-http_image_filter_module \
                        --with-http_spdy_module \
                        --with-http_sub_module \
                        --with-http_xslt_module \
                        --with-mail \
                        --with-mail_ssl_module \
                        --with-http_flv_module \
                        --with-http_mp4_module \
                        --with-http_random_index_module \
                        --with-http_secure_link_module \
                        --add-module=$(MODULESDIR)/nginx-auth-pam \
                        --add-module=$(MODULESDIR)/nginx-dav-ext-module \
                        --add-module=$(MODULESDIR)/nginx-echo \
                        --add-module=$(MODULESDIR)/nginx-upstream-fair \
                        --add-module=/opt/upload/nginx-upload-progress-module \
                        --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module
Once that's edited and saved, we can build Nginx!
cd /opt/rebuildnginx/nginx-1.6.2
sudo dpkg-buildpackage -b
This will take a few minutes.
In Once the build is complete, we'll find a bunch of .deb files added in /opt/rebuildnginx. We can use these to install Nginx.

We adjusted the "full" package, and so we'll want to use that build to install Nginx. Looking at the deb files, we'll see two "full" packages:

nginx-full-dbg_1.6.2-5+trusty0_amd64.deb
nginx-full_1.6.2-5+trusty0_amd64.deb
I'm on a 64bit version of Ubuntu, so I'm using the amd64 version. The dbg version is a debug version, so we'll use the other package to install Nginx:

# .deb files appear one level above the `nginx-1.6.2` directory
cd /opt/rebuildnginx
sudo dpkg --install nginx-full_1.6.2-5+trusty0_amd64.deb
That step should be done quickly.

Our recompiled Nginx is installed! We can use nginx -V (capital "V") to see which flags were used during compilation, letting us know which modules are installed:stall nginx.
Note: if install fails, try to remove the current installed nginx version and try again.
 If you have already drupal website running with nginx you can copy the whole /etc/nginx forlder to the new server. Or you can download perusio configuration from the internet [end of document ref to url].
Make neginx init script to start/stop nginx server:
vi /etc/init.d/nginx ; then add the following script in the file:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nginx init.d dash script for Ubuntu <=9.10.
# Description:       nginx init.d dash script for Ubuntu <=9.10.
### END INIT INFO
#------------------------------------------------------------------------------
# nginx - this Debian Almquist shell (dash) script, starts and stops the nginx
#         daemon for ubuntu 9.10 and lesser version numbered releases.
#
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server.  This \
#                                 script will manage the initiation of the \
#                                 server and it's process state.
#
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /acronymlabs/server/nginx.pid
# Provides:    nginx
#
# Author:  Jason Giedymin
#          <jason.giedymin AT gmail.com>.
#
# Version: 2.0 02-NOV-2009 jason.giedymin AT gmail.com
# Notes: nginx init.d dash script for Ubuntu <=9.10.
#
# This script's project home is:
#
#------------------------------------------------------------------------------
#                               MIT X11 License
#------------------------------------------------------------------------------
#
# Copyright (c) 2009 Jason Giedymin, http://Amuxbit.com formerly
#                                                                                                http://AcronymLabs.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------
. /lib/lsb/init-functions

#------------------------------------------------------------------------------
#                               Consts
#------------------------------------------------------------------------------
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx

PS="nginx"
PIDNAME="nginx"                         #lets you do $PS-slave
PIDFILE=$PIDNAME.pid                    #pid file
PIDSPATH=/var/run

DESCRIPTION="Nginx Server..."

RUNAS=root                              #user to run as

SCRIPT_OK=0                             #ala error codes
SCRIPT_ERROR=1                          #ala error codes
TRUE=1                                  #boolean
FALSE=0                                 #boolean

lockfile=/var/lock/subsys/nginx
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 #------------------------------------------------------------------------------
#                               Simple Tests
#------------------------------------------------------------------------------

#test if nginx is a file and executable
test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

#set exit condition
#set -e

#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------

setFilePerms(){

        if [ -f $PIDSPATH/$PIDFILE ]; then
                chmod 400 $PIDSPATH/$PIDFILE
        fi
}

configtest() {
       $DAEMON -t -c $NGINX_CONF_FILE
}

getPSCount() {
       return `pgrep -f $PS | wc -l`
}

isRunning() {
        if [ $1 ]; then
                pidof_daemon $1
                PID=$?

                if [ $PID -gt 0 ]; then
                        return 1
                else
                        return 0
                fi
        else
                pidof_daemon
                PID=$?

                if [ $PID -gt 0 ]; then
                        return 1
                else
                        return 0
                fi
        fi
}

#courtesy of php-fpm
wait_for_pid () {
        try=0

        while test $try -lt 35 ; do

                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;

                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac

                #echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}

status(){
       isRunning
       isAlive=$?

       if [ "${isAlive}" -eq $TRUE ]; then
                echo "$PIDNAME found running with processes:  `pidof $PS`"
        else
                echo "$PIDNAME is NOT running."
        fi


}

removePIDFile(){
       if [ $1 ]; then
                if [ -f $1 ]; then
                                            rm -f $1
               fi
        else
                                    #Do default removal
                                    if [ -f $PIDSPATH/$PIDFILE ]; then
                                            rm -f $PIDSPATH/$PIDFILE
               fi
        fi
}

start() {
        log_daemon_msg "Starting $DESCRIPTION"

       isRunning
       isAlive=$?

        if [ "${isAlive}" -eq $TRUE ]; then
                log_end_msg $SCRIPT_ERROR
        else
                start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
                -- -c $NGINX_CONF_FILE
                setFilePerms
                log_end_msg $SCRIPT_OK
        fi
}

stop() {
       log_daemon_msg "Stopping $DESCRIPTION"

       isRunning
       isAlive=$?
        if [ "${isAlive}" -eq $TRUE ]; then
                start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE

                                    wait_for_pid 'removed' $PIDSPATH/$PIDFILE

                if [ -n "$try" ] ; then
                        log_end_msg $SCRIPT_ERROR
                else
                        removePIDFile
                       log_end_msg $SCRIPT_OK
                fi

        else
                log_end_msg $SCRIPT_ERROR
        fi
}

reload() {
       configtest || return $?

       log_daemon_msg "Reloading (via HUP) $DESCRIPTION"

        isRunning
        if [ $? -eq $TRUE ]; then
                                    `killall -HUP $PS` #to be safe

                log_end_msg $SCRIPT_OK
        else
                log_end_msg $SCRIPT_ERROR
        fi
}

quietupgrade() {
       log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"

        isRunning
        isAlive=$?
        if [ "${isAlive}" -eq $TRUE ]; then
                                    kill -USR2 `cat $PIDSPATH/$PIDFILE`
                                    kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`

                                    isRunning
                                    isAlive=$?
                                    if [ "${isAlive}" -eq $TRUE ]; then
                                                                 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
                                                                 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
                        removePIDFile $PIDSPATH/$PIDFILE.oldbin

                                                                 log_end_msg $SCRIPT_OK
                                    else
                                                                 log_end_msg $SCRIPT_ERROR

                                                                 log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"

                                                                 kill -HUP `cat $PIDSPATH/$PIDFILE`
                                                                 kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
                                                                 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`

                                                                 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
                        removePIDFile $PIDSPATH/$PIDFILE.oldbin

                                                                 log_end_msg $SCRIPT_ok
                                    fi
        else
                log_end_msg $SCRIPT_ERROR
        fi
}

terminate() {
        log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"

       PIDS=`pidof $PS` || true

       [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`

       for i in $PIDS; do
                                    if [ "$i" = "$PIDS2" ]; then
                                    kill $i
                        wait_for_pid 'removed' $PIDSPATH/$PIDFILE
                                                                 removePIDFile
                                    fi
       done

       log_end_msg $SCRIPT_OK
}

destroy() {
       log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
       killall $PS -q >> /dev/null 2>&1
       log_end_msg $SCRIPT_OK
}

pidof_daemon() {
    PIDS=`pidof $PS` || true

    [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`

    for i in $PIDS; do
        if [ "$i" = "$PIDS2" ]; then
            return 1
        fi
    done
    return 0
}

case "$1" in
  start)
       start
        ;;
  stop)
       stop
        ;;
  restart|force-reload)
       stop
       sleep 1
       start
        ;;
  reload)
       $1
       ;;
  status)
       status
       ;;
  configtest)
        $1
        ;;
  quietupgrade)
       $1
       ;;
  terminate)
       $1
       ;;
  destroy)
       $1
       ;;
  *)
       FULLPATH=/etc/init.d/$PS
       echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
       echo "       The 'destroy' command should only be used as a last resort."
       exit 1
       ;;
esac
 exit 0
Make the init script executable and create the system startup links:
chmod 755 /etc/init.d/nginx
update-rc.d nginx defaults
Finally start nginx:
/etc/init.d/nginx start
Installing php5.3 and php-fpm on Ubuntu 14.04 server
It is possible to use multiple PHP versions on one server and select the optimal PHP version for a website. This feature works with PHP-FPM (starting with PHP 5.3). Here we build PHP 5.3  as a PHP-FPM version on an Ubuntu 14.04 server. These PHP versions can be used together with the default PHP (installed through apt) in ISPConfig.
I install PHP 5.3.29 I will also show how to build some additional PHP extensions such as memcache and memcached.
PHP-FPM PHP 5.3 with --enable-fpm and one time with --enable-cgi directory, such as /opt/php-5.3 for PHP-FPM.
Download and extract PHP 5.3.29:
sudo mkdir /opt/php-5.3
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
PHP_VERSION=5.3.29
tar xvf php-$PHP_VERSION.tar.bz2
cd php-$PHP_VERSION
sudo apt-get install aspell curl libaspell-dev libbz2-dev libc-client-dev libfreetype6 libfreetype6-dev libjpeg62 libjpeg62-dev libmcrypt4 libmcrypt-dev libmhash2 libmhash-dev libming-dev libncurses-dev libpng3 libpng12-dev libpspell-dev libreadline-dev libsasl2-dev libsnmp-dev libt1-5 libt1-dev libtidy-dev libxml2 libxml2-dev libxml2-utils libxpm4 libxpm-dev libxslt1-dev   libcurl4-openssl-dev libpq-dev libmariadbclient-dev pkg-config libssl-dev build-essential libfcgi-dev libfcgi0ldbl libssl-dev libc-client2007e libc-client2007e-dev
sudo apt-get build-dep php5
(The following command is required  if you build PHP with --with-imap, because otherwise ./configure will stop with the following error:)
checking for crypt in -lcrypt... yes
configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.
ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a
(The following installation is optional so, perhaps it is not require to be installed).
sudo apt-get install php5-ldap php5-gd php5-mcrypt php5-xmlrpc php5-xsl (drupal requires ldap, gd support).
(Using the following script we tell to php buid to select the ldap libraries from a sepecific directory).
sudo sed -i -e 's~^\([[:space:]]*\)\(if test -f \$LDAP_LIBDIR/liblber\.a || test -f \$LDAP_LIBDIR/liblber\.\$SHLIB_SUFFIX_NAME; then\)$~\1if test -f \$LDAP_LIBDIR/x86_64-linux-gnu/liblber\.a || test -f \$LDAP_LIBDIR/x86_64-linux-gnu/liblber\.\$SHLIB_SUFFIX_NAME; then\n\1  LDAP_LIBDIR=$LDAP_LIBDIR/x86_64-linux-gnu\n\1fi\n\n\1\2~' configure;
Configure and build PHP 5.3.29 as follows (you can adjust the ./configure command to your needs, take a look at ./configure --help)
sudo ./configure --prefix=/opt/php-5.3 --with-pdo-pgsql --with-zlib-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-freetype-dir=/usr --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-pdo-mysql --with-mysqli --with-jpeg-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --enable-ftp --without-imap --without-imap-ssl --with-kerberos --with-gettext --enable-fpm --with-libdir=/lib/x86_64-linux-gnu --with-libdir=lib --with-ldap --with-ldap-sasl
NOTE: if you encounter an error like configure: error: freetype.h not found even-though you installed libfreetype6-dev, then execute the following commands and try the ./configure again:
mkdir /usr/include/freetype2/freetype
ln -s /usr/include/freetype2/freetype.h /usr/include/freetype2/freetype/freetype.h
Make and install the configuration as following commands
make
make install
Copy php.ini and php-fpm.conf to the correct locations:
cp /usr/local/src/php5-build/php-$PHP_VERSION/php.ini-production /opt/php-5.3/lib/php.ini
cp /opt/php-5.3/etc/php-fpm.conf.default /opt/php-5.3/etc/php-fpm.conf
Open /opt/php-5.3/etc/php-fpm.conf and adjust the following settings - in the listen line you must use an unused port (e.g. 8999; port 9000 might be in use by Ubuntu's default PHP-FPM already), and you must add the line include=/opt/php-5.3/etc/pool.d/*.conf at the end:
vi /opt/php-5.3/etc/php-fpm.conf
[...]
pid = run/php-fpm.pid
[...]
user = www-data
group = www-data
[...]
listen = 127.0.0.1:8999
[...]
include=/opt/php-5.3/etc/pool.d/*.conf
Create the pool directory for PHP-FPM:
mkdir /opt/php-5.3/etc/pool.d
You can add www.conf under the pool. Or you can copy past the www.conf from other server if you migrate the site.
Next create an init script for PHP-FPM:
vi /etc/init.d/php-5.3-fpm ; copy the following script in the php-5.3-fpm file.
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-5.3-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-5.3-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.3/sbin/php-fpm
php_fpm_CONF=/opt/php-5.3/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.3/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                       echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac
exit 0
Make the init script executable and create the system startup links:
chmod 755 /etc/init.d/php-5.3-fpm
update-rc.d php-5.3-fpm defaults
Finally start PHP-FPM:
/etc/init.d/php-5.3-fpm start
That's it - now install some additional modules like memcache and memcached.
sudo apt-get -y install php-pear
cd /opt/php-5.3/etc
pecl -C ./pear.conf update-channels
The memcache extension can be installed as follows:
pecl -C ./pear.conf install memcache-beta (because memcache stable is 2.7.7 not compatible with drupal)
Open /opt/php-5.3/lib/php.ini...
vi /opt/php-5.3/lib/php.ini
... and add the line extension=memcache.so at the end of the file:
[...]
extension=memcache.so
The memcached extension can be installed as follows:
sudo apt-get install libmemcached-dev
pecl -C ./pear.conf install memcached
... and add the line extension=memcached.so at the end of the file:
[...]
extension=memcached.so

you can adjust the php.ini, www.conf parameters according the site needs.
Installing drush 1.6.2
sudo pear channel-discover pear.drush.org
sudo pear install drush/drush