17 nginx鏈接memcached

一:配置php擴展memcached

wget http://memcached.googlecode.com/files/memcached-1.4.9.tar.gzphp

 
 

# tar zvxf memcached-1.4.9.tar.gz
# cd memcached-1.4.9html

 
 

 /usr/bin/phpize   #生成configure編譯文件mysql

 
 
 
 

  若是這裏出現linux

 
 

    Can’t find PHP headers in /usr/include/php
    The php-devel package is required for use of this commandnginx

 
 

   那麼執行 yum install php-develweb



 ./configure --with-php-config=/usr/bin/php-config \


make && make insll

會提示找到memcache.so位置
Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/      [不一樣的電腦提示也不一樣]

  修改php.ini sql

  先找到Php.ini的位置數據庫

  find / -name php.inivim

  vim xxx/xxx/xxx/php.ini瀏覽器

extension=/根據提示的mecached的位置/memcache.so ;

殺死php進程:pkill -9 php

重啓php:./usr/bin/php-fpm
最後檢查memcached是否安裝成功  127.0.0.1 查看是否有memcache 模塊。
---------------------------------------------------------
利用telnet 測試memcached

  超找memcached的位置 find / -name memcached

  ./usr/local/memcached/bin/memcached -m 64 -p 11211 -vvv 

  【注意不能是root用戶開啓】  ./usr/local/memcached/bin/memcached -u nobody -vv -p 11211 

----------------------------------------------------------------------------------------------------------------

若是上面的不可以安裝memcached就按這裏的方式來安裝。

wget -c http://www.memcached.org/files/memcached-1.4.17.tar.gz

tar -zxvf memcached-1.4.17.tar.gz


cd memcached-1.4.17
./configure --prefix=/usr/local/memcached
 make
 sudo make install

第三步:啓動memcached

cd /usr/local/memcached
./bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211

第三步:檢查是否安裝成功

$ps -aux | grep memcache 

-----------------------------------------------------------------------------------------------------

 

 

  測試是否安裝了telnet

  rpm -q telnet-server 無信息回覆表示沒安裝

  安裝 telnet

  yum install telnet

  在linux上打開一個窗口 打開telnet服務

  telnet 127.0.0.1 11211 回車

  ctrl+] 打開回顯回車。

  在linux上打開一個窗口 測試mecached

  add news 0 0 8 /

  12345678 回車

  get news 回車

-------------------------------------------------------

二:nginx配置

  (1)配置nginx.conf 信息
   location / {

     #設置key
     set $memcached_key "$uri";
     #根據key查找對應的服務器
     memcached_pass 127.0.0.1:11211;
     #出錯的時候回調php,查找數據庫中的數據
     error_page 404 /callback.php;

   }

   在linux上打開一個窗口 打開telnet服務

   telnet 127.0.0.1 11211 回車

   ctrl+] 打開回顯回車。


   在linux上打開一個窗口添加一個memcache key

   add /4.html 0 0 10 /
   hello word

   瀏覽器上測試:輸入 127.0.0.1/4.html

   瀏覽器上顯示:hello word

 

(2)新建測試數據
mysql -uroot -p;
use test;

CREATE TABLE `abc_table` (

   `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `name` varchar(25) NOT NULL,
    PRIMARY KEY (`uid`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 insert abc_table values(null,'1.html page');
 insert abc_table values(null,'2.html page');

 insert abc_table values(null,'3.html page');

 insert abc_table values(null,'4.html page');

 insert abc_table values(null,'5.html page');

 insert abc_table values(null,'6.html page');

 insert abc_table values(null,'7.html page');

 insert abc_table values(null,'8.html page');

 insert abc_table values(null,'9.html page');

 insert abc_table values(null,'10.html page');

 select * from abc_table
+-----+--------------+
| uid | name |
+-----+--------------+
| 1 | 1.html page |
| 2 | 2.html page |
| 3 | 3.html page |
| 4 | 4.html page |
| 5 | 5.html page |
| 6 | 6.html page |
| 7 | 7.html page |
| 8 | 8.html page |
| 9 | 9.html page |
| 10 | 10.html page |
+-----+--------------+

(3)編寫callback.php  要是在nginx的html目錄文件夾中編寫。

<?php
  header("Content-type: text/html; charset=utf-8");

  $uri=$_SERVER['REQUEST_URI'];

  #/user4243.html

  #字符串截取獲取數字

  $uid=substr($uri,5,strpos($uri,'.')-5);
  # echo $uid;die;
  #鏈接數據庫併入memcached

  $con=mysql_connect("localhost","root","root");

  $sql='use test';
  mysql_query($sql,$con);
  $sql="set names utf8";
  $sql="select * from abc_table where uid=".$uid;
  # echo $sql ;die;

  $rs=mysql_query($sql,$con);

  $sql="set names utf8";
  $sql="select * from abc_table where uid=".$uid;
  # echo $sql ;die;

  $rs=mysql_query($sql,$con);

  $user=mysql_fetch_assoc($rs);

  if(empty($user)){
     echo "用戶不存在";
   }else{
    #寫入memcached
    $men=new memcache();
    $men->connect('localhost',11211);
    #echo '2222';
    $men->add($uri,$user['name'],0,300);
    $men->close();
}

(4)測試是否鏈接成功memcache
瀏覽器輸入:127.0.0.1/user1.html 出現空白
再次刷新 出現數據

能夠經過telnet窗口觀察其中取值的變化。




--------------------------------------------------------------------------------------------------------------
請求過程以下圖

相關文章
相關標籤/搜索