当前位置:Gxlcms > 数据库问题 > Ubuntu16.04下nginx+mysql+php+redis

Ubuntu16.04下nginx+mysql+php+redis

时间:2021-07-01 10:21:17 帮助过:44人阅读


Redis是一个key-value存储系统。和Memcached类似,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。在部分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++(hiredis),C#,PHP,JavaScript,Perl,Object-C,Python,Ruby等客户端,使用很方便。

二、架构图
技术分享

大致结构就是读写分离,将mysql中的数据通过触发器同步到redis中

三、安装LNMP环境

1.apt-get安装

apt-get install nginx mysql-server php

2.配置nginx,支持php

vi /etc/nginx/sites-available/default
......
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    incloude snippets/fastcgi-php.conf;
#
#    # With php7.0-cgi alone;
#    fastcgi_pass 127.0.0.1:9000;
#    # With php7.0-fpm;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
......

 3.重启nginx,测试

vi /var/www/html/info.php
<?php phpinfo();?>

然后访问页面看到php的相关信息,基础环境就算搭建完成了。

四、安装redis

1.安装redis和php的redis扩展

apt-get install redis-server
apt-get install git php-dev
git clone -b php7 https://github.com/phpredis/phpredis.git
cd phpredis/
phpize
./configure
make
make install

2.配置php的redis扩展

vi /etc/php/7.0/fpm/conf.d/redis.ini
extension=redis.so

3.重启fpm,访问info.php,就能看到redis扩展

/etc/init.d/php7.0-fpm restart

五、读取测试

<?php  
//连接本地Redis服务  
$redis=new Redis();  
$redis->connect(‘localhost‘,‘6379‘) or die ("Could net connect redis server!");

//$redis->auth(‘admin123‘); //登录验证密码,返回【true | false】

$redis->ping();  //检查是否还再链接,[+pong]
  
$redis->select(0);//选择redis库,0~15 共16个库 
  
//设置数据  
$redis->set(‘school‘,‘WuRuan‘);  
//设置多个数据  
$redis->mset(array(‘name‘=>‘jack‘,‘age‘=>24,‘height‘=>‘1.78‘));  
//存储数据到列表中  
$redis->lpush("tutorial-list", "Redis");  
$redis->lpush("tutorial-list", "Mongodb");  
$redis->lpush("tutorial-list", "Mysql");  
  
//获取存储数据并输出  
echo $redis->get(‘school‘);  
echo ‘<br/>‘;
$gets=$redis->mget(array(‘name‘,‘age‘,‘height‘));
print_r($gets);
echo ‘<br/>‘;
$tl=$redis->lrange("tutorial-list", 0 ,5); print_r($tl);
echo ‘<br/>‘;
//释放资源 $redis->close(); ?>

 

Ubuntu16.04下nginx+mysql+php+redis

标签:扩展   1.7   cat   lnmp环境   new   listen   搭建   技术   缓存   

人气教程排行