1. Installing Memcache and Memcached on linux servers
SSH to server as root.
Install the required packages.
root@jijo [~]# cd /usr/src
root@jijo [/usr/src]# yum install libevent libevent-devel -y
Now we need to download the latest memcached source
root@jijo [/usr/src]# wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
root@jijo [/usr/src]# tar xvf memcached-*
root@jijo [/usr/src]# cd memcached-*
root@jijo [/usr/src/memcached-1.4.15]# ./configure
root@jijo [/usr/src/memcached-1.4.15]# make
root@jijo [/usr/src/memcached-1.4.15]# make install
Now we need to create a memcached configuration file
root@jijo [/usr/src/memcached-1.4.15]# touch /etc/memcached.conf
root@jijo [/usr/src/memcached-1.4.15]# vi /etc/memcached.conf
add the following lines to it
----------------------------
#Memory a user
-m 16
# default port
-p 11211
# user to run daemon nobody/apache/www-data
-u nobody
# only listen locally
-l 127.0.0.1
now we need to create a startup script for memcached
3. }
stop() {
echo -n $"Shutting down $DESC: "
killproc $NAME
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
4. *)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
-----------------------------
root@jijo [/usr/src/memcached-1.4.15]# chmod +x /etc/init.d/memcached
root@jijo [/usr/src/memcached-1.4.15]# touch /usr/local/bin/start-memcached
root@jijo [/usr/src/memcached-1.4.15]# vi /usr/local/bin/start-memcached
add the following lines to it
-----------------------------
#!/usr/bin/perl -w
# start-memcached
use strict;
if ($> != 0 and $< != 0) {
print STDERR "Only root wants to run start-memcached.n";
exit;
}
my $etcfile = shift || "/etc/memcached.conf";
my $params = [];
my $etchandle;
# This script assumes that memcached is located at /usr/bin/memcached, and
# that the pidfile is writable at /var/run/memcached.pid
my $memcached = "/usr/local/bin/memcached";
my $pidfile = "/var/run/memcached.pid";
my $fd_reopened = "/dev/null";
sub handle_logfile {
my ($logfile) = @_;
5. $fd_reopened = $logfile;
}
sub reopen_logfile {
my ($logfile) = @_;
open *STDERR, ">>$logfile";
open *STDOUT, ">>$logfile";
open *STDIN, ">>/dev/null";
$fd_reopened = $logfile;
}
# This is set up in place here to support other non -[a-z] directives
my $conf_directives = {
"logfile" => &handle_logfile
};
if (open $etchandle, $etcfile) {
foreach my $line (<$etchandle>) {
$line =~ s/#.*//go;
$line = join ' ', split ' ', $line;
next unless $line;
next if $line =~ /^-[dh]/o;
if ($line =~ /^[^-]/o) {
my ($directive, $arg) = $line =~ /^(.*?)s+(.*)/;
$conf_directives->{$directive}->($arg);
next;
}
push @$params, $line;
}
6. }
unshift @$params, "-u root" unless (grep $_ eq '-u', @$params);
$params = join " ", @$params;
if (-e $pidfile) {
open PIDHANDLE, "$pidfile";
my $localpid = <PIDHANDLE>;
close PIDHANDLE;
chomp $localpid;
if (-d "/proc/$localpid") {
print STDERR "memcached is already running.n";
exit;
} else {
`rm -f $localpid`;
}
}
my $pid = fork();
if ($pid == 0) {
reopen_logfile($fd_reopened);
exec "$memcached $params";
exit(0);
} elsif (open PIDHANDLE,">$pidfile") {
print PIDHANDLE $pid;
close PIDHANDLE;
} else {
print STDERR "Can't write pidfile to $pidfile.n";
}
7. -----------------------------
root@jijo [/usr/src/memcached-1.4.15]# chmod +x /usr/local/bin/start-memcached
start memcached
root@jijo [/usr/src/memcached-1.4.15]# /etc/init.d/memcached start
Starting memcached: [ OK ]
install memcache on server now
root@jijo [/usr/src]# wget http://pecl.php.net/get/memcache-2.2.7.tgz
root@jijo [/usr/src]# tar xvf memcache-*
root@jijo [/usr/src]# cd memcache-*
root@jijo [/usr/src/memcache-2.2.7]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
root@jijo [/usr/src/memcache-2.2.7]# ./configure
root@jijo [/usr/src/memcache-2.2.7]# make
root@jijo [/usr/src/memcache-2.2.7]# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20090626/
add extension in php.ini
root@jijo [/usr/src/memcache-2.2.7]# php -i |grep php.ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini
root@jijo [/usr/src/memcache-2.2.7]# vi /usr/local/lib/php.ini
add 'extension=memcache.so' under Dynamic Extensions and restart apache
root@jijo [/usr/src/memcache-2.2.7]# /etc/init.d/httpd restart
create a phpinfo page to check memcache support.