CentOS installation memcached and (Nginx) memcache extension Detailed tutorials

Source: Internet
Author: User
Tags fpm memcached zts server port
Download memadmin, http://www.junopen.com/memadmin/ and create a new site in IIS. Test address: http://wap.yousawang.com/mem,

1. Restart apache

Service httpd restrat

/Etc/init.d/httpd stop

/Etc/init.d/httpd start

2. Restart mysql

Service mysqld restart

/Etc/init.d/mysqld stop

/Etc/init.d/mysqld start

3. Restart Nginx

Service nginx restart

/Etc/init.d/nginx stop

/Etc/init.d/nginx start

4. Restart fpm

Restart and terminate operation commands of php-fpm in php 5.3.3

 

Memcache is a high-performance distributed memory object caching system. By maintaining a unified huge hash table in memory, it can be used to store data in various formats. Simply put, the data is called into memory, and then Read from memory, which greatly improves the reading speed.

working principle:

① When the client accesses the application for the first time, it will retrieve the data from the database (RDBMS) and return it to the client; at the same time, it will also save the retrieved data to memcached.

② During the second access, because the data is already cached, you don't need to go to the database to query it, just take it from memcached.
So how is the speed and efficiency of memcached reflected? We all know that RDBMS is a file-based database, which is ultimately stored on disk in the form of a file. Memcached is different. It is a key: value relational database, which is stored in memory. Then I don't need to explain it anymore. The read and write speed of memory is much faster than that of disk. http://blog.rekfan.com/?p=172

③ memcached is based on the event processing of libevent. libevent is a program library that encapsulates event processing functions such as epoll for Linux and kqueue for BSD operating systems into a unified interface. Even if the number of connections to the server increases, the performance of O (1) can be exerted. memcached uses this libevent library, so it can exert its high performance on Linux, BSD, Solaris and other operating systems. Event processing will not be described in detail here, you can refer to Dan Kegel's The C10K Problem For more information about libevent: http://monkey.org/~provos/libevent/.

Installation Environment:
Operating system: CentOS 6.2 X86_64
Libevent: http://www.monkey.org/~provos/libevent/
Memcache: http://www.danga.com/memcached/dist/
PHP extension: http://pecl.php.net/package/memcache

Steps to install Memcached:

1. Compile and install Libevent

cd ~
curl -O https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
tar -zxvf libevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure
make
make install
Or directly
# yum install libevent libevent-devel -y
Check to see if it is installed:
ls -al / usr / lib | grep libevent
#Display the following statement indicating success
libevent.so-> libevent-2.0.so.5.1.7

2. Install Memcache

cd ~
curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -zxvf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --prefix = / usr / local / memcached
make
make install

Check if the installation is successful
ls -al / usr / local / memcached / bin / mem *
#Display the following statement indicating success
/ usr / local / memcached / bin / memcached

3. Configure environment variables:
Enter the user's host directory, edit .bash_profile, and add a new directory for the system environment variable LD_LIBRARY_PATH
# vi .bash_profile
MEMCACHED_HOME = / usr / local / memcached
LD_LIBRARY_PATH = $ LD_LIBRARY_PATH: $ MEMCACHED_HOME / lib
Refresh user environment variables:
# source .bash_profile

4. Write memcached service start and stop scripts

# cd /etc/init.d
vi memcached
The script content is as follows:
-------------------------------------------------- ------------------------------
#! / bin / sh
##
# Startup script for the server of memcached
#http: //blog.rekfan.com/? p = 172
# processname: memcached
# pidfile: /usr/local/memcached/memcached.pid
# logfile: /usr/local/memcached/memcached_log.txt
# memcached_home: / etc / memcached
# chkconfig: 35 21 79
# description: Start and stop memcached Service

# Source function library
. /etc/rc.d/init.d/functions

RETVAL = 0

prog = "memcached"
basedir = / usr / local / memcached
cmd = $ {basedir} / bin / memcached
pidfile = "$ basedir / $ {prog} .pid"
#logfile = "$ basedir / memcached_log.txt"

# Set memcached startup parameters
ipaddr = "192.168.1.101" # Bind listening IP address
port = "11211" # service port
username = "root" # The identity of the user who runs the program
max_memory = 64 # default: 64M | Maximum memory usage
max_simul_conn = 1024 # default: 1024 | Maximum number of simultaneous connections
# maxcon = 51200
# growth_factor = 1.3 # default: 1.25 | Block size growth factor
# thread_num = 6 # default: 4
#verbose = "-vv" # View detailed startup information
# bind_protocol = binary # ascii, binary, or auto (default)

start () {
echo -n $ "Starting service: $ prog"
$ cmd -d -m $ max_memory -u $ username -l $ ipaddr -p $ port -c $ max_simul_conn -P $ pidfile
RETVAL = $?
echo
[$ RETVAL -eq 0] && touch / var / lock / subsys / $ prog
}

stop () {
echo -n $ "Stopping service: $ prog"
run_user = `whoami`
pidlist = `ps -ef | grep $ run_user | grep memcached | grep -v grep | awk‘ {print ($ 2)} ’`
for pid in $ pidlist
do
# echo "pid = $ pid"
kill -9 $ pid
if [$? -ne 0]; then
return 1
fi
done
RETVAL = $?
echo
[$ RETVAL -eq 0] && rm -f / var / lock / subsys / $ prog
}

# See how we were called.
case "$ 1" in
start)
start
;;
stop)
stop
;;
#reload)
# reload
# ;;
restart)
stop
start
;;
#condrestart)
# if [-f / var / lock / subsys / $ prog]; then
# stop
# start
# fi
# ;;
status)
status memcached
;;
*)
echo "Usage: $ 0 {start | stop | restart | status}"
exit 1
esac
exit $ RETVAL

-------------------------------------------------- ------------------------------
http://blog.rekfan.com/?p=172
5. The setup script can be executed:

# chmod + x memcached
# chkconfig --add memcached
# chkconfig --level 35 memcached on

6. Set up a firewall

vi / etc / sysconfig / iptables

Add the following line

-A RH-Firewall-l-INPUT -p tcp -m tcp --dport 11211 -j ACCEPT
Note: add memcached to the firewall allow access rules
service iptables restart
Note: firewall restart

7. Start memcached
# service memcached start
// When starting, it actually calls the following command to start memcached in the form of a daemon
/ usr / local / memcached / bin / memcached -d -m 64 -u root -l 192.168.1.101
-p 11211 -c 1024 -P /usr/local/memcached/memcached.pid

8. Check if memcached is started:

# ps -ef | grep memcached

9. Memcached command parameter explanation
http://blog.rekfan.com/?p=172

Parameter explanation and explanation
-p <num> listening port
-l <ip_addr> Connected IP address, the default is this machine. The -l option may not be used, which means monitoring on all network interface addresses. The recommendation is -l <ip_addr> to specify an internal network IP address to avoid being the target of external network attacks
-d start start memcached service
-d restart restart memcached service
-d stop | s
hutdown shut down the running memcached service
-d install install memcached service
-d uninstall uninstall memcached service
-u <username> Run as <username> (only valid when running as root)
-m <num> Maximum memory usage in MB. 64MB by default
-M returns an error when the memory is exhausted instead of deleting the item
-c <num> The maximum number of simultaneous connections, the default is 1024
-f <factor> block size growth factor, default is 1.25
-n <bytes> Minimum allocated space, key + value + flags default to 48
-h show help
Install the PHP Memcache extension:

You can use the pecl installer that comes with php
# / usr / local / servers / php / bin / pecl install memcache

Can also be installed from source
# wget http://pecl.php.net/get/memcache-2.2.7.tgz
# tar zxf memcache-2.2.7.tgz
# cd memcache-2.2.7
# / usr / local / php / bin / phpize
# ./configure --enable-memcache = / usr / local / memcached --with-php-config = / usr / local / php / bin / php-config
# make && make inst

After installation, there will be a prompt similar to this:
Installing shared extensions: / usr / local / servers / php5 / lib / php / extensions / no-debug-non-zts-20060922 /
Remember this, then modify php.ini,
Add a line
extension = "/ usr / local / php / lib / php / extensions / no-debug-non-zts-20090626 / memcache.so"

Test code:

<? php

$ memcache = new Memcache;
$ memcache-> connect (‘localhost‘, 11211) or die ("Could not connect");

$ version = $ memcache-> getVersion ();
echo "Server ‘s version:". $ version. "<br/> n";

$ tmp_object = new stdClass;
$ tmp_object-> str_attr = ‘test’;
$ tmp_object-> int_attr = 123;

$ memcache-> set (‘key‘, $ tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds) <br/> n";

$ get_result = $ memcache-> get (‘key‘);
echo "Data from the cache: <br/> n";

var_dump ($ get_result);

?>

Show results:

Server ‘s version: 1.2.6
Store data in the cache (data will expire in 10 seconds)
Data from the cache:

object (stdClass) [3] public ‘str_attr’ => string ‘test’ (length = 4) public ‘int_attr’ => int 123

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 timed out
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 ();
}
}
?>

Centos Install Memcached and (Nginx) Memcache Extension Detailed Tutorial
Related Article

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.