Install Memcached on CentOS and configure Memcache extension for PHP
1. download the required source code package
All installation packages use the latest one.
1. Download libevent2.0.22
:http://libevent.org/
Download memcached1.4.24
:http://memcached.org/
Download php extension memcache3.0.8
:http://pecl.php.net/package/memcache
2. Installation Steps
First, download the three software packages to the/tmp directory.
1. Install libevent
Cd/tmp # first enter the directory of the downloaded package tar zxvf libevent-2.0.22-stable.tar.gz # decompress the package cd libevent-2.0.22-stable # Enter the directory to decompress. /configure -- prefix =/usr/local # pre-compilation configuration, generate the Makefile file, and change the path to make; make install # compile + install
Test whether the installation is successful
2. Install memcached
Cd/tmp # first enter the directory of the downloaded package tar zxvf memcached-1.4.24.tar.gz # decompress the package cd memcached-1.4.24 # Enter the directory to decompress. /configure-with-libevent =/usr/local # pre-compilation configuration to generate the Makefile file. The path must be consistent with make in libevent; make install # compilation + Installation
Test whether the installation is successful
The above operations complete the installation of the memcached server. It's very easy! Now let's get started with memcache php extension installation.
3. Install Extension
Cd/tmp # first enter the directory of the downloaded package tar zxvf memcache-3.0.8.tgz # decompress the package cd memcache-3.0.8 # enter the unzipped directory/opt/lampp/bin/phpize # add extensions for php dynamically. The phpize path may be inconsistent. /configure-enable-memcache-with-php-config =/opt/lampp/bin/php-config-with-zlib-dir # php-config fill in make according to your environment; make install # compile + install
When I completed the above steps, I clicked enter in a pleasant mood and prepared to have a mouthful of water. The result showed an error (this is also the reason why I wrote this blog, or I am too lazy to record it)
After reading the code error message, zlib. h cannot be found. If you cannot find one, give it to him. Really
Install zlib
Download zlib-1.2.8.tar.gz
:http://www.zlib.net/
Cd/tmp # first enter the directory of the downloaded package tar zxvf zlib-1.2.8.tar.gz # decompress the package cd zlib-1.2.8 # Enter the directory to decompress. /configure -- prefix =/usr/local/zlibmake; make install # compile + install
Then configure the system file and load the library file generated by zlib compiled and installed just now.
Vi/etc/ld. so. conf. d/zlib. conf
Add the following content and save and exit:/usr/local/zlib/lib
That is, to add the file path of the installation directory, the library file. After ldconfig is run, the installed library file will be loaded.
OK. Now, execute the make; make install command again in the php extension of memcache.
If the following information is displayed, the operation is successful:
3. Configure the php. ini file
First, use the phpinfo function to find the php. ini path. my options are:/Opt/lampp/etc/php. ini
Add the following content:
- Modify the extension_dir path:
Extension_dir = "/opt/lampp/lib/php/extensions/no-debug-non-zts-20121212 /"
- Extension = memcache. so
[Memcache]
Memcache. allow_failover = 1
Memcache. max_failover_attempts = 20
Memcache. chunk_size = 8192
Restart apache and view the situation through phpinfo again. If the following content exists, the configuration is successful:
4. Configure the memcached Server
- Start the memcached Server
Memcached-d-m 10-u root-l 127.0.0.1-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 127.0.0.1,
-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 am saving it in/tmp/memcached. pid
2. End memcached.
Killcat /tmp/memcached.pid
3. Check whether memcached has been started.
5. Test the memcache extension of php
<?php $mem = new Memcache; $mem->connect("192.168.12.201", 13001); $mem->set('key','This is a test!', 0, 60); $val = $mem->get('key'); echo $val;?>
Later: If you are interested, you can use memcache to save the session. Php itself uses the file method, which is not very good. The total size of the file is GB.
Memcached installation and startup script
Performance problems of using Memcached in PHP
Install Memcached in Ubuntu and its command explanation
Install and apply Memcached
Use Nginx + Memcached's small image storage solution
Getting started with Memcached
For details about Memcached, click here
Memcached: click here