First, the source code package preparation
Server side is mainly installed Memcache server side, the latest version is memcached-v1.4.4.
Download: http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
In addition, Memcache used libevent This library for socket processing, so also need to install libevent,
The latest version of Libevent is libevent-1.4.13-stable. (If your system has libevent installed, you may not need to install it)
Official website: http://www.monkey.org/~provos/libevent/
Download: http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
Prepare memcached PHP Extensions for the source installation package:
Official website: http://pecl.php.net/get/memcache-2.2.5.tgz
Linux command download:
Copy Code code as follows:
wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
wget http://pecl.php.net/get/memcache-2.2.5.tgz
second, installation and configuration
1. Install Libevent First
Copy Code code as follows:
Tar zxvf libevent-1.4.13-stable.tar.gz
CD libevent-1.4.13-stable
./configure--PREFIX=/USR
Make
Make install
2. Test Libevent Whether the installation is successful
Copy Code code as follows:
Ls-al/usr/lib | grep libevent
Libevent-1.1a.so.1
libevent-1.1a.so.1.0.2
Libevent-1.4.so.2
libevent-1.4.so.2.1.3
Libevent.a
Libevent_core-1.4.so.2
libevent_core-1.4.so.2.1.3
Libevent_core.a
Libevent_core.la
Libevent_core.so
Libevent_extra-1.4.so.2
libevent_extra-1.4.so.2.1.3
Libevent_extra.a
Libevent_extra.la
Libevent_extra.so
Libevent.la
Libevent.so
Different versions, the list of possible files is different.
3, install memcached, also need to install the designated Libevent installation location
Copy Code code as follows:
Tar zxvf memcached-1.4.4.tar.gz
CD memcached-1.4.4
./configure–with-libevent=/usr
Make && make install
The memcached will be automatically placed on the/usr/local/bin/memcached when the installation is complete
4, test whether the successful installation of memcached
Copy Code code as follows:
ls-al/usr/local/bin/mem*
-rwxr-xr-x 1 root root 201869 12-14 21:44/usr/local/bin/memcached
5, the installation of memcache PHP extension
① install PHP's memcache extension
Copy Code code as follows:
Tar vxzf memcache-2.2.5.tgz
CD memcache-2.2.5
/usr/local/webserver/php/bin/phpize
./configure--enable-memcache--with-php-config=/usr/local/php/bin/php-config--with-zlib-dir
Make
Make install
② The above installation will have a similar hint:
Installing Shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
③ the php.ini in the Extension_dir = "./" revision to
Copy Code code as follows:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/"
④ add a row to load the memcache extension: extension=memcache.so
Three, the basic memcached set
1. Start the Memcache server side:
Copy Code code as follows:
Memcached-d-M 10-u root-l 202.207.177.177-p 11211-c 256-p/tmp/memcached.pid
Parameter description:
The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, I am 10MB here,
-U is the user running memcache, I am here root,
-L is a listening server IP address, if there are more than one address, I specify the IP address of the server 202.207.177.177,
-P is the port that sets the memcache listening, I set 11211 here, preferably 1024 or more ports,
The-c option is the maximum number of concurrent connections to run, the default is 1024, I set 256 here, according to the load of your server to set,
-P is set to save the Memcache pid file, which I am here to save in/tmp/memcached.pid,
2. If you want to end the memcache process, execute:
Copy Code code as follows:
Kill ' Cat/tmp/memcached.pid '
You can also start multiple daemons, but the ports cannot be duplicated.
3. Check if memcached is started
Copy Code code as follows:
Netstat-ant
TCP 0 0 202.207.177.177:11211 0.0.0.0:* LIST
The 11211 port is already open, indicating that the memcached has started normally.
4. Restart CentOS
Copy Code code as follows:
Iv. Environmental testing of Memcache
Run the following PHP file, if the output is a test!, it means that the environment has been built successfully. Start your memcache journey!
Copy Code code as follows:
<?php
$mem = new Memcache;
$mem->connect ("202.207.177.177", 11211);
$mem->set (' key ', ' This is a test! ', 0, 60);
$val = $mem->get (' key ');
Echo $val;
?>
The famous phpcms also supports memcached extensions:
Copy Code code as follows:
<?php
Memcache Server Configuration
Define (' memcache_host ', ' localhost '); Memcache Server Host
Define (' Memcache_port ', 11211); Memcache Server Port
Define (' Memcache_timeout ', 1); S,memcache Server Connection Timeout
Class Cache
{
var $memcache;
function __construct ()
{
$this->memcache = &new memcache;
$this->memcache->pconnect (Memcache_host, Memcache_port, memcache_timeout);
}
function Cache ()
{
$this->__construct ();
}
function Get ($name)
{
return $this->memcache->get ($name);
}
function set ($name, $value, $ttl = 0)
{
return $this->memcache->set ($name, $value, 0, $ttl);
}
function rm ($name)
{
return $this->memcache->delete ($name);
}
function Clear ()
{
return $this->memcache->flush ();
}
}
?>
V. reference materials
Friends who have questions about memcached can refer to the following articles:
Memcache installation under Linux: http://www.ccvita.com/257.html
Nginx 0.8.x + PHP 5.2.10 (FastCGI) build a Web server 10 times times better than Apache: http://blog.s135.com/nginx_php_v5/