欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux安装memcache,PHP配置memcache,nginx配置memcache

程序员文章站 2022-05-17 13:59:41
...

Linux安装memcache

yum install memcached  
#设置memcache开机启动
systemctl enable memcached
#启动memcached
systemctl start memcached

PHP配置memcache

cd /server/package
wget  http://pecl.php.net/get/memcache-4.0.5.2.tgz
tar zxvf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2
配置php
执行
/server/php/bin/phpize

./configure --help
//可以看到配置php

linux安装memcache,PHP配置memcache,nginx配置memcache

./configure --with-php-config=/server/php/bin/php-config
make && make install

linux安装memcache,PHP配置memcache,nginx配置memcache

vim /server/php/etc/php.ini	
#PHP配置文件增加memcache扩展
extension=/server/php/lib/php/extensions/no-debug-non-zts-20160303/memcache.so
#重新启动PHP进程
pkill -9 php
/server/php/sbin/php-fpm
vim /data/html/index.php

<?php
	echo phpinfo();

linux安装memcache,PHP配置memcache,nginx配置memcache
至此,php添加memcache扩展已成功

nginx配置memcache

#连接memcached
telnet localhost 11211
#存储 key => value
add  /user1.html 0 0 8
I am Ycm

vim /server/nginx/conf/vhost/test001.com.conf

 location /{
            set $memcached_key "$uri";
            memcached_pass 127.0.0.1:11211;
    #       root /data/html;
            #index index.php index.html index.htm;
    }

完整配置

server{
    listen 80;
    server_name test001.com  www.test001.com;
    root /data/html;
    access_log /logs/nginx/access_test001.log main;
    error_log /logs/nginx/error_test001.log ;
    location /{
            set $memcached_key "$uri";
            memcached_pass 127.0.0.1:11211;
            error_page 404 /callback.php;
    #       root /data/html;
            #index index.php index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html{
            root /data/html;
    }

    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
    location ~* \.(jpg|jpeg|gif|ico){
            #root /data/html/;
            expires 1d;
    }

#保存并重启nginx

nginx -s reload

访问test001.com/user1.html
linux安装memcache,PHP配置memcache,nginx配置memcache
此时,访问test001.com/user2html,返回404,需要进行优化

location /{
            set $memcached_key "$uri";
            memcached_pass 127.0.0.1:11211;
            #增加error_page判断,并调取callback.php
            error_page 404 /callback.php;
    #       root /data/html;
            #index index.php index.html index.htm;
    }

#编辑回调文件

vim /data/html/callback.php

<?php
#print_r($_SERVER);
$uri = $_SERVER['REQUEST_URI'];
$uid = substr($uri,5,strpos($uri,'.')-5);
echo $uid;

$con = mysqli_connect('localhost','root','aaa@qq.com');
$sql = 'use test';
mysqli_query($con,$sql);
$sql2 = 'set names utf8';
mysqli_query($con,$sql2);

$sql3= 'select * from user where uid='.$uid;
$rs = mysqli_query($con,$sql3);

$user = mysqli_fetch_assoc($rs);
echo 'From mysql query';
if(empty($user)){
        echo 'no this user';
}else{
    $html = '<h1>'.$user['uname'].'</h1>';
    echo $html;
    $mem = new memcache();
    $mem->connect('localhost','11211');
    $mem->add($uri,$user,0,300);
    $mem->close();
}

退出telnet localhost 11211
先按 ctrl + ]
然后输入 quit 按下 enter 退出