Memcache installation steps under CentOS 5.4 of Linux+Nginx+PHP+Memcached

  • 2020-05-06 12:00:48
  • OfStack

One, source package preparation

The most recent 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 libevent-1.4.13-stable. If you already have libevent installed on your system, you don't need to install
Liverpoolfc.tv: 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 installation package:
Website: http: / / pecl. php. net get/memcache - 2.2.5. tgz

Linux instruction download:

 
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 of
1. Install libevent
first
 
tar zxvf libevent-1.4.13-stable.tar.gz 
cd libevent-1.4.13-stable 
./configure --prefix=/usr 
make 
make install 


2. Test whether libevent has been installed successfully
 
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 may have different file lists.

3. To install memcached, the installation location
of libevent shall be specified in the installation
 
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 will put memcached automatic/usr/local/bin/memcached

4. Test whether memcached
has been successfully installed
 
ls -al /usr/local/bin/mem* 
-rwxr-xr-x 1 root root 201869 12-14 21:44 /usr/local/bin/memcached 

5. Install Memcache's PHP extension

Install PHP's memcache extension
 
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, there will be a prompt like this:

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

Change extension_dir = "./ "in php.ini to
 
extension_dir =  " /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/ "  

Add a line to load the memcache extension: extension= memcache.so

Three, memcached basic Settings
1. Start Memcache server:
 
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 is the amount of memory allocated to Memcache in units of MB, which I'll call 10MB,
-u is the user who runs Memcache, so root,
-l is the IP address of the listening server. If there are multiple addresses, I have specified the IP address of the server 202.207.177.177,
-p is the port where Memcache is set to listen. I set 11211 here, preferably 1024 or above,
-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,
-P is the pid file set to save Memcache, I'm saving it in /tmp/ memcached.pid,

2. To terminate the Memcache process, execute:

 
kill `cat /tmp/memcached.pid` 

Multiple daemons can also be started, but the ports cannot be repeated.

3. Check whether Memcached starts
 
netstat -ant 
tcp 0 0 202.207.177.177:11211 0.0.0.0:* LIST 

Port 11211 has been opened, indicating that Memcached has started normally.

4. Restart CentOS
 
reboot 

4. Memcache environment test
Run the php file below, if there is output This is a test! , means the environment is built successfully. Start your Memcache journey!
 
<?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 the Memcached extension:
 
<?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(); 
} 
} 
?> 

5. Reference materials
Those who have questions about Memcached can refer to the following article:
Linux Memcache installation: under http: / / www ccvita. com / 257 html
Nginx 0.8. x + PHP 5.2.10 (FastCGI) set up more than ten times Apache Web server: http: / / blog s135. com/nginx_php_v5 /


Related articles: