Install Memcache in CentOS 5.4 (Linux + Nginx + PHP + Memcached)

Source: Internet
Author: User
Tags zts

I. Prepare the source code package

The server is mainly installed on the memcache server, the latest version is memcached-v1.4.4.
Download: http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
In addition, Memcache uses the libevent library for Socket processing, so you also need to install libevent,

The latest version of libevent is the libevent-1.4.13-stable. (If your system has already installed libevent, you do 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 the Memcached PHP extension source code installation package:
Official Website: http://pecl.php.net/get/memcache-2.2.5.tgz

Download Linux commands:

Copy codeThe Code is 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

Ii. installation and configuration
1. Install libevent firstCopy codeThe Code is 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 whether the libevent is successfully installed.Copy codeThe Code is 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.
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 may have different file lists.

3. Install memcached and specify the libevent installation location during installation.Copy codeThe Code is as follows: tar zxvf memcached-1.4.4.tar.gz
Cd memcached-1.4.4
./Configure-with-libevent =/usr
Make & make install

After the installation is complete, memcached will be automatically put to/usr/local/bin/memcached

4. Test whether memcached is successfully installed.Copy codeThe Code is as follows: ls-al/usr/local/bin/mem *
-Rwxr-xr-x 1 root 201869 12-14/usr/local/bin/memcached

5. install PHP extension for Memcache

① Install the memcache extension of PHPCopy codeThe Code is 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

② After the above installation, a message similar to this will be displayed:

Installing shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/

③ Modify extension_dir = "./" in php. iniCopy codeCode: extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx /"

④ Add a row to load the memcache extension: extension = memcache. so

III. Basic settings of memcached
1. Start the Memcache Server:Copy codeThe Code is as follows: memcached-d-m 10-u root-l 202.207.177.177-p 11211-c 256-P/tmp/memcached. pid

Parameter description:

-D option is to start a daemon,
-M indicates the amount of memory allocated to Memcache. The unit is MB. Here I am 10 MB,
-U is the user who runs Memcache. Here I am root,
-L is the IP address of the listener server. If there are multiple IP addresses, I have specified the Server IP address 202.207.177.177,
-P is the port for Memcache listening. I have set port 11211 here, preferably port 1024 or above,
-The "c" option is the maximum number of concurrent connections. The default value is 1024. I have set 256 here, which is based on the load of your server,
-P is the pid file for saving Memcache. Here I save it in/tmp/memcached. pid,

2. to end the Memcache process, run:

Copy codeThe Code is as follows: kill 'cat/tmp/memcached. Pi'

You can also start multiple daemon processes, but the ports cannot be repeated.

3. Check whether Memcached is enabled.Copy codeThe Code is as follows: netstat-ant
Tcp 0 0 202.207.177.177: 11211 0.0.0.0: * LIST

Port 11211 is enabled, indicating that Memcached has started properly.

4. Restart CentOSCopy codeThe Code is as follows: reboot

Iv. Test the Memcache Environment
Run the following PHP file. If This is a test! is output !, Indicates that the environment is successfully set up. Start your journey to Memcache!Copy codeThe Code is as follows: <? Php
$ Mem = new Memcache;
$ Mem-& gt; 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 extension:Copy codeThe Code is 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. References
If you have any questions about Memcached, refer to the following articles:
Install Memcache in Linux: http://www.ccvita.com/257.html
Nginx 0.8.x + PHP 5.2.10 (FastCGI) build a Web server that is 10 times better than Apache: http://blog.s135.com/nginx_php_v5/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.