參考文檔:php
本文簡單介紹memcached服務器端的安裝配置,與php-memcache客戶端鏈接服務器端的配置與操做。html
Memcached是一款開源、高性能、分佈式內存對象緩存系統,可應用各類須要緩存的場景,其主要目的是經過下降對Database的訪問來加速web應用程序。python
Memcached通常的使用場景是:經過緩存數據庫查詢的結果,減小數據庫訪問次數,以提升動態Web應用的速度、提升可擴展性。mysql
本質上,memcached是一個基於內存的key-value存儲,用於存儲數據庫調用、API調用或頁面引用結果的直接數據,如字符串、對象等小塊任意數據。nginx
工做流程以下:c++
Server:CentOS-7-x86_64-1511 web
IP:10.11.4.190 sql
libevent-2.1.8數據庫
官網:http://libevent.org/vim
下載:http://ftp.lfs-matrix.net/pub/blfs/conglomeration/libevent/
memcached-1.4.39
下載:https://memcached.org/files/memcached-1.4.39.tar.gz
Memcached服務器端的安裝相對簡單。
#Memcached依賴於libevent API,libevent是個程序庫,它將Linux的epoll、BSD類操做系統的kqueue等事件處理功能封裝成統一的接口,即便對服務器的鏈接數增長,也能發揮O(1)的性能 [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://ftp.lfs-matrix.net/pub/blfs/conglomeration/libevent/libevent-2.1.8-stable.tar.gz [root@memcached src]# tar -zxvf libevent-2.1.8-stable.tar.gz [root@memcached src]# cd libevent-2.1.8-stable [root@memcached libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent [root@memcached libevent-2.1.8-stable]# make [root@memcached libevent-2.1.8-stable]# make install #測試是否安裝成功 [root@memcached libevent-2.1.8-stable]# ll /usr/local/libevent/lib/ | grep libevent
[root@memcached ~]# cd /usr/local/src/ [root@memcached src]# tar -zxvf memcached-1.4.39.tar.gz.tar [root@memcached src]# cd memcached-1.4.39
#注意編譯前,生成Makefile文件時,libevent的路徑 [root@memcached memcached-1.4.39]# ./configure --prefix=/usr/local/memcached -with-libevent=/usr/local/libevent [root@memcached memcached-1.4.39]# make [root@memcached memcached-1.4.39]# make install
#簡單經過軟連接的方式設置環境變量 [root@memcached ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
#tcp11211端口是memcached默認監聽端口 [root@memcached ~]# vim /etc/sysconfig/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 11211 -j ACCEPT [root@memcached ~]# service iptables restart
[root@memcached ~]# /usr/local/memcached/bin/memcached -d -m 256 -u root -l 10.11.4.190 -p 11211 -c 1024 -P /usr/local/memcached/memcached.pid 參數說明: -d:啓動一個守護進程,若是前臺運行,配合-vv參數,可查看調試信息(主要是存儲的信息); -m:分配給memcached使用的內存數量,單位是MB; -u:運行memcached的用戶,好比root或者memcached,建議採用非root帳號; -l:服務器監聽地址,不設置時默認監聽本地全部IP地址; -p:設置memcached的監聽端口,默認爲11211; -c:設置最大併發鏈接數,默認是1024; -P:設置保存memcached的pid文件; -v:輸出警告和錯誤信息; -vv:打印客戶端的請求和返回信息; -h:打印幫助信息; -i:打印memcached和libevent的版權信息。
[root@memcached ~]# netstat -tunlp
#能夠經過telnet鏈接memcached服務器進行數據存儲,及數據獲取; #詳細的memcached的命令可參考:http://blog.mimvp.com/2015/01/memcache-start-telnet-command-xiangjie/; #如下紅色字體是命令輸入,截圖可見命令輸入與回顯 [root@memcached ~]# telnet 10.11.4.190 11211 version #查看版本 set test 0 0 5 #設置」key」, <command name> <key> <flags> <exptime> <bytes> mymem #輸入「value」值,<data block>,字節數與key中的設的「bytes」相同 get test #獲取已設的key的數據 quit #退出
[root@memcached ~]# vim /etc/rc.d/init.d/memcached #!/bin/sh # # memcached: MemCached Daemon # # chkconfig: - 90 25 # description: MemCached Daemon # # Source function library. . /etc/rc.d/init.d/functions . /etc/sysconfig/network #注意:可執行文件路徑根據狀況調整 MEMCACHED="/usr/local/memcached/bin/memcached" start() { echo -n $"Starting memcached: " #注意:參數根據狀況調整 daemon $MEMCACHED -u root -d -m 256 -p 11211 -c 1024 -P /usr/local/memcached/memcached.pid echo } stop() { echo -n $"Shutting down memcached: " killproc memcached echo } [ -f $MEMCACHED ] || exit 0 # See how we were called. case "$1" in start) start ;; stop) stop ;; restart|reload) stop start ;; condrestart) stop start ;; *) echo $"Usage: $0 {start|stop|restart|reload|condrestart}" exit 1 esac exit 0 [root@memcached ~]# chmod 775 /etc/rc.d/init.d/memcached [root@memcached ~]# chkconfig --level 35 memcached on
Memcache支持多客戶端,如perl,php,python,c/c++等等,這裏主要基於php配置。
其中nginx與php的詳細配置請見:http://www.cnblogs.com/netonline/p/7327409.html
#nginx版本:1.12.0 #建立用戶 [root@memcached ~]# groupadd www [root@memcached ~]# useradd -g www -s /sbin/nologin www #安裝依賴包 [root@memcached ~]# yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y #編譯安裝 [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz [root@memcached src]# tar -zxvf nginx-1.12.0.tar.gz [root@memcached src]# cd nginx-1.12.0 [root@memcached nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module [root@memcached nginx-1.12.0]# make [root@memcached nginx-1.12.0]# make install #修改用戶/組 [root@memcached nginx-1.12.0]# chown -R www:www /usr/local/nginx #設置開機啓動 [root@memcached ~]# vim /etc/rc.d/init.d/nginx [root@memcached ~]# chown www:www /etc/rc.d/init.d/nginx [root@memcached ~]# chmod 775 /etc/rc.d/init.d/nginx [root@memcached ~]# chkconfig --level 35 nginx on #啓動 [root@memcached ~]# service nginx start
#php版本:5.6.31 #安裝依賴包 [root@memcached ~]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel xml2 xml2-devel openssl openssl-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl libcurl-devel gdbm-devel db4-devel libXpm libXpm-devel libX11 libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel #編譯安裝libmcrypt庫, [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz [root@memcached src]# tar -zxvf libmcrypt-2.5.8.tar.gz [root@memcached src]# cd libmcrypt-2.5.8 [root@memcached libmcrypt-2.5.8]# ./configure [root@memcached libmcrypt-2.5.8]# make [root@memcached libmcrypt-2.5.8]# make install #編譯安裝php [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://php.net/distributions/php-5.6.31.tar.bz2 [root@memcached src]# tar -jxvf php-5.6.31.tar.bz2 [root@memcached src]# cd php-5.6.31 [root@memcached php-5.6.31]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-pdo-mysql --with-mysqli --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-mhash --with-mcrypt --with-bz2 --enable-zip --with-curl --with-gettext --with-iconv --with-xmlrpc --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --enable-pdo --enable-libxml --enable-xml --enable-soap --enable-session --enable-ctype --enable-ftp --enable-bcmath --enable-shmop --enable-inline-optimization --enable-opcache --enable-mbregex --enable-pcntl --enable-cgi --enable-wddx [root@memcached php-5.6.31]# make [root@memcached php-5.6.31]# make install #php.ini文件 [root@memcached ~]# cp /usr/local/src/php-5.6.31/php.ini-production /usr/local/php/etc/php.ini [root@memcached ~]# ln -s /usr/local/php/etc/php.ini /etc/php.ini #php-fpm.conf文件,取消」;pid = run/php-fpm.pid」的註釋,同時修改運行帳號通nginx服務的運行帳號一致 [root@memcached ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@memcached ~]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf [root@memcached ~]# sed -i 's|;pid = run/php-fpm.pid|pid = run/php-fpm.pid|g' /usr/local/php/etc/php-fpm.conf [root@memcached etc]# sed -i 's|user = nobody|user = www|g' /usr/local/php/etc/php-fpm.conf [root@memcached etc]# sed -i 's|group = nobody|group = www|g' /usr/local/php/etc/php-fpm.conf #設置開機啓動 [root@memcached ~]# cp /usr/local/src/php-5.6.31/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@memcached ~]# chown www:www /etc/rc.d/init.d/php-fpm [root@memcached ~]# chmod 755 /etc/rc.d/init.d/php-fpm [root@memcached ~]# chkconfig --level 35 php-fpm on #設置nginx支持php [root@memcached ~]# vim /usr/local/nginx/conf/nginx.conf #第2行,取消user的註釋,修改運行帳號爲www www,與/usr/local/php/etc/php-fpm.d/www.conf中的user/group配置一致 user www www; #第45行,添加index.php index index.html index.htm index.php; #第65~71行,取消FastCGI server部分location的註釋;注意fastcgi_param行的參數,改成$document_root$fastcgi_script_name,或者使用絕對路徑 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #驗證測試 [root@memcached ~]# echo -e "<?php\nphpinfo();\n?>" > /usr/local/nginx/html/index.php [root@memcached ~]# chown -R www:www /usr/local/nginx/html/ [root@memcached ~]# chmod -R 700 /usr/local/nginx/html/ [root@memcached ~]# service nginx restart [root@memcached ~]# service php-fpm start
#memcached在1.2.4版本(含)以上增長了CAS(Check and Set)協議,即對同一key的多進程的併發處理問題; #類比數據庫,若是同時有多個進程對同一張表的同一數據進行更新,數據庫能夠鎖定整張表,也能夠鎖定表內某一行數據,memcached的CAS功能與此類似; #但php-memcache擴展不支持CAS,須要先安裝php-memcached擴展(注意與php-memcache擴展的區別),php-memcached擴展基於libmemcached,因此要先安裝libmemcached,即php-memcached的庫。 #libmemcached版本:1.0.18 #https://launchpad.net/libmemcached/+download
#下載libmemcached [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget https://launchpadlibrarian.net/165454254/libmemcached-1.0.18.tar.gz #編譯安裝,生成Makefile文件時,切記「--with-memcached」參數 [root@memcached src]# tar -zxvf libmemcached-1.0.18.tar.gz.tar [root@memcached src]# cd libmemcached-1.0.18 [root@memcached libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached --with-memcached [root@memcached libmemcached-1.0.18]# make [root@memcached libmemcached-1.0.18]# make install
#php-memcached版本:2.2.0(3.0.0及以上版本針對php7.0及以上版本) #http://pecl.php.net/package/memcached #php擴展分原生擴展與第三方擴展,在php的源碼解壓包下的「ext/」目錄下可查看全部的原生擴展,php-memcached及php-memcache屬於第三方擴展。 #下載php-memcached [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://pecl.php.net/get/memcached-2.2.0.tgz #編譯安裝 #phpize: 用於擴展php模塊,經過phpize能夠創建php的外掛模塊 #--enable-memcached: 配置編譯環境,編譯器編譯php源碼時使能相應擴展 #--with-php-config:指定php-config文件路徑 #--with-libmemcached-dir:指定libmemcached安裝目錄 #--disable-memcached-sasl:去使能sasl認證,由於沒有預安裝相應功能 #make:把源碼編譯成xxxxx.so文件 #make install: 把xxxxx.so文件移動到當前安裝php的擴展目錄 [root@memcached src]# tar -zxvf memcached-2.2.0.tgz [root@memcached src]# cd memcached-2.2.0 [root@memcached memcached-2.2.0]# /usr/local/php/bin/phpize [root@memcached memcached-2.2.0]# ./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl [root@memcached memcached-2.2.0]# make [root@memcached memcached-2.2.0]# make install
#php-memcached版本:2.2.7(stable version) #http://pecl.php.net/package/memcache #下載php-memcache [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://pecl.php.net/get/memcache-2.2.7.tgz #編譯安裝 [root@memcached src]# tar -zxvf memcache-2.2.7.tgz [root@memcached src]# cd memcache-2.2.7 [root@memcached memcache-2.2.7]# /usr/local/php/bin/phpize [root@memcached memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config [root@memcached memcache-2.2.7]# make [root@memcached memcache-2.2.7]# make install
#php-intl版本:3.0.0 #http://pecl.php.net/package/intl #php-intl是php國際化擴展,是ICU庫的一個包裝器,安裝php-intl擴展前要先安裝ICU庫 [root@memcached ~]# yum install -y icu libicu libicu-devel #下載php-intl; #經過查看php源碼解壓包下的「ext/」目錄,php-intl屬於原生擴展,理論上能夠不用下載,直接在「ext/」下相應擴展目錄下編譯安裝便可; [root@memcached ~]# cd /usr/local/src/ [root@memcached src]# wget http://pecl.php.net/get/intl-3.0.0.tgz #編譯安裝 [root@memcached src]# tar -zxvf intl-3.0.0.tgz [root@memcached src]# cd intl-3.0.0 [root@memcached intl-3.0.0]# /usr/local/php/bin/phpize [root@memcached intl-3.0.0]# ./configure --enable-intl --with-php-config=/usr/local/php/bin/php-config [root@memcached intl-3.0.0]# make [root@memcached intl-3.0.0]# make install
php啓用擴展有2種方式(本文介紹方式1):
#修改php.ini,添加擴展,可在第732行後添加擴展(非必須);
#第732行的」extension_dir」路徑修改成絕對路徑(非必須,相對路徑也可),即以上各擴展「make install」以後的安裝路徑 [root@memcached ~]# vim /usr/local/php/etc/php.ini [Intl] extension = intl.so [Memcached] extension = memcached.so [Memcache] extension = memcache.so [root@memcached ~]# service php-fpm restart
#查看擴展模塊方式 [root@memcached ~]# /usr/local/php/bin/php -m | grep -E 'memcache|intl'
#phpinfo()方式,利用前面已經生成的index.php文件便可 http://10.11.4.190/index.php
#修改index.php [root@memcached ~]# cd /usr/local/nginx/html/ [root@memcached html]# cp index.php index.php.bak [root@memcached html]# echo "" > index.php [root@memcached html]# vim index.php <?php $memcache = new Memcache; #建立一個memcache對象 $memcache->connect('localhost', 11211) or die ("Could not connect"); #鏈接memcached服務器 $memcache->set('key', 'memcache-test'); #設置1個變量到內存中,key=key, value=memcache-test $get_value = $memcache->get('key'); #從內存取出key的value echo $get_value; #回顯 ?>
瀏覽器訪問:http://10.11.4.190/index.php