红联Linux门户
Linux帮助

Ubuntu16.04下安装nginx+mysql+php+redis

发布时间:2017-07-12 09:52:44来源:linux网站作者:luminary
一、redis简介
Redis是一个key-value存储系统。和Memcached类似,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。在部分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++(hiredis),C#,PHP,JavaScript,Perl,Object-C,Python,Ruby等客户端,使用很方便。
 
二、架构图
Ubuntu16.04下安装nginx+mysql+php+redis
大致结构就是读写分离,将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();
?>
 
本文永久更新地址:http://www.linuxdiyf.com/linux/31935.html