当前位置: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安装

  1. apt-get <span style="color: #0000ff">install</span> nginx mysql-server php

2.配置nginx,支持php

  1. <span style="color: #0000ff">vi</span> /etc/nginx/sites-available/<span style="color: #000000">default
  2. ......
  3. # pass the PHP scripts to FastCGI server listening on </span><span style="color: #800080">127.0</span>.<span style="color: #800080">0.1</span>:<span style="color: #800080">9000</span><span style="color: #000000">
  4. #
  5. location </span>~<span style="color: #000000"> \.php$ {
  6. incloude snippets</span>/fastcgi-<span style="color: #000000">php.conf;
  7. #
  8. # # With php7.</span><span style="color: #800080">0</span>-<span style="color: #000000">cgi alone;
  9. # fastcgi_pass </span><span style="color: #800080">127.0</span>.<span style="color: #800080">0.1</span>:<span style="color: #800080">9000</span><span style="color: #000000">;
  10. # # With php7.</span><span style="color: #800080">0</span>-<span style="color: #000000">fpm;
  11. fastcgi_pass unix:</span>/run/php/php7.<span style="color: #800080">0</span>-<span style="color: #000000">fpm.sock;
  12. }
  13. ......</span>

 3.重启nginx,测试

  1. <span style="color: #0000ff">vi</span> /var/www/html/<span style="color: #000000">info.php
  2. </span><?php phpinfo();?>

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

四、安装redis

1.安装redis和php的redis扩展

  1. apt-get <span style="color: #0000ff">install</span> redis-<span style="color: #000000">server
  2. apt</span>-get <span style="color: #0000ff">install</span><span style="color: #000000"> git php-dev
  3. git clone </span>-b php7 https:<span style="color: #008000">//</span><span style="color: #008000">github.com/phpredis/phpredis.git</span>
  4. cd phpredis/<span style="color: #000000">
  5. phpize
  6. .</span>/<span style="color: #000000">configure
  7. </span><span style="color: #0000ff">make</span>
  8. <span style="color: #0000ff">make</span> <span style="color: #0000ff">install</span>

2.配置php的redis扩展

  1. <span style="color: #0000ff">vi</span> /etc/php/<span style="color: #800080">7.0</span>/fpm/conf.d/<span style="color: #000000">redis.ini
  2. extension</span>=redis.so

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

  1. /etc/init.d/php7.<span style="color: #800080">0</span>-fpm restart

五、读取测试

  1. <?<span style="color: #000000">php
  2. </span><span style="color: #008000">//</span><span style="color: #008000">连接本地Redis服务 </span>
  3. <span style="color: #800080">$redis</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> Redis();
  4. </span><span style="color: #800080">$redis</span>->connect(‘localhost‘,‘6379‘) or <span style="color: #0000ff">die</span> ("Could net connect redis server!"<span style="color: #000000">);
  5. </span><span style="color: #008000">//</span><span style="color: #008000">$redis->auth(‘admin123‘); //登录验证密码,返回【true | false】</span>
  6. <span style="color: #800080">$redis</span>->ping(); <span style="color: #008000">//</span><span style="color: #008000">检查是否还再链接,[+pong]</span>
  7. <span style="color: #800080">$redis</span>->select(0);<span style="color: #008000">//</span><span style="color: #008000">选择redis库,0~15 共16个库
  8. //设置数据 </span>
  9. <span style="color: #800080">$redis</span>->set(‘school‘,‘WuRuan‘<span style="color: #000000">);
  10. </span><span style="color: #008000">//</span><span style="color: #008000">设置多个数据 </span>
  11. <span style="color: #800080">$redis</span>->mset(<span style="color: #0000ff">array</span>(‘name‘=>‘jack‘,‘age‘=>24,‘height‘=>‘1.78‘<span style="color: #000000">));
  12. </span><span style="color: #008000">//</span><span style="color: #008000">存储数据到列表中 </span>
  13. <span style="color: #800080">$redis</span>->lpush("tutorial-list", "Redis"<span style="color: #000000">);
  14. </span><span style="color: #800080">$redis</span>->lpush("tutorial-list", "Mongodb"<span style="color: #000000">);
  15. </span><span style="color: #800080">$redis</span>->lpush("tutorial-list", "Mysql"<span style="color: #000000">);
  16. </span><span style="color: #008000">//</span><span style="color: #008000">获取存储数据并输出 </span>
  17. <span style="color: #0000ff">echo</span> <span style="color: #800080">$redis</span>->get(‘school‘<span style="color: #000000">); </span><span style="color: #000000"><span style="color: #000000"><br><span style="color: #0000ff">echo ‘<br/>‘<span style="color: #000000">; <br></span></span></span></span><span style="color: #800080">$gets</span>=<span style="color: #800080">$redis</span>->mget(<span style="color: #0000ff">array</span>(‘name‘,‘age‘,‘height‘<span style="color: #000000">)); <br></span><span style="color: #008080">print_r</span>(<span style="color: #800080">$gets</span><span style="color: #000000">);<span style="color: #000000"><br><span style="color: #0000ff">echo ‘<br/>‘<span style="color: #000000">; </span></span></span>
  18. </span><span style="color: #800080">$tl</span>=<span style="color: #800080">$redis</span>->lrange("tutorial-list", 0 ,5<span style="color: #000000">);
  19. </span><span style="color: #008080">print_r</span>(<span style="color: #800080">$tl</span><span style="color: #000000">); <span style="color: #000000"><span style="color: #000000"><br><span style="color: #0000ff">echo ‘<br/>‘<span style="color: #000000">; </span></span></span></span>
  20. </span><span style="color: #008000">//</span><span style="color: #008000">释放资源</span>
  21. <span style="color: #800080">$redis</span>-><span style="color: #000000">close();
  22. </span>?>

 

Ubuntu16.04下nginx+mysql+php+redis

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

人气教程排行