时间:2021-07-01 10:21:17 帮助过:44人阅读
二、架构图
大致结构就是读写分离,将mysql中的数据通过触发器同步到redis中
三、安装LNMP环境
1.apt-get安装
- apt-get <span style="color: #0000ff">install</span> nginx mysql-server php
2.配置nginx,支持php
- <span style="color: #0000ff">vi</span> /etc/nginx/sites-available/<span style="color: #000000">default
- ......
- # 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">
- #
- location </span>~<span style="color: #000000"> \.php$ {
- incloude snippets</span>/fastcgi-<span style="color: #000000">php.conf;
- #
- # # With php7.</span><span style="color: #800080">0</span>-<span style="color: #000000">cgi alone;
- # 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">;
- # # With php7.</span><span style="color: #800080">0</span>-<span style="color: #000000">fpm;
- fastcgi_pass unix:</span>/run/php/php7.<span style="color: #800080">0</span>-<span style="color: #000000">fpm.sock;
- }
- ......</span>
3.重启nginx,测试
- <span style="color: #0000ff">vi</span> /var/www/html/<span style="color: #000000">info.php
- </span><?php phpinfo();?>
然后访问页面看到php的相关信息,基础环境就算搭建完成了。
四、安装redis
1.安装redis和php的redis扩展
- apt-get <span style="color: #0000ff">install</span> redis-<span style="color: #000000">server
- apt</span>-get <span style="color: #0000ff">install</span><span style="color: #000000"> git php-dev
- git clone </span>-b php7 https:<span style="color: #008000">//</span><span style="color: #008000">github.com/phpredis/phpredis.git</span>
- cd phpredis/<span style="color: #000000">
- phpize
- .</span>/<span style="color: #000000">configure
- </span><span style="color: #0000ff">make</span>
- <span style="color: #0000ff">make</span> <span style="color: #0000ff">install</span>
2.配置php的redis扩展
- <span style="color: #0000ff">vi</span> /etc/php/<span style="color: #800080">7.0</span>/fpm/conf.d/<span style="color: #000000">redis.ini
- extension</span>=redis.so
3.重启fpm,访问info.php,就能看到 扩展
- /etc/init.d/php7.<span style="color: #800080">0</span>-fpm restart
五、读取测试
- <?<span style="color: #000000">php
- </span><span style="color: #008000">//</span><span style="color: #008000">连接本地Redis服务 </span>
- <span style="color: #800080">$redis</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> Redis();
- </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">);
- </span><span style="color: #008000">//</span><span style="color: #008000">$redis->auth(‘admin123‘); //登录验证密码,返回【true | false】</span>
- <span style="color: #800080">$redis</span>->ping(); <span style="color: #008000">//</span><span style="color: #008000">检查是否还再链接,[+pong]</span>
- <span style="color: #800080">$redis</span>->select(0);<span style="color: #008000">//</span><span style="color: #008000">选择redis库,0~15 共16个库
- //设置数据 </span>
- <span style="color: #800080">$redis</span>->set(‘school‘,‘WuRuan‘<span style="color: #000000">);
- </span><span style="color: #008000">//</span><span style="color: #008000">设置多个数据 </span>
- <span style="color: #800080">$redis</span>->mset(<span style="color: #0000ff">array</span>(‘name‘=>‘jack‘,‘age‘=>24,‘height‘=>‘1.78‘<span style="color: #000000">));
- </span><span style="color: #008000">//</span><span style="color: #008000">存储数据到列表中 </span>
- <span style="color: #800080">$redis</span>->lpush("tutorial-list", "Redis"<span style="color: #000000">);
- </span><span style="color: #800080">$redis</span>->lpush("tutorial-list", "Mongodb"<span style="color: #000000">);
- </span><span style="color: #800080">$redis</span>->lpush("tutorial-list", "Mysql"<span style="color: #000000">);
- </span><span style="color: #008000">//</span><span style="color: #008000">获取存储数据并输出 </span>
- <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>
- </span><span style="color: #800080">$tl</span>=<span style="color: #800080">$redis</span>->lrange("tutorial-list", 0 ,5<span style="color: #000000">);
- </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>
- </span><span style="color: #008000">//</span><span style="color: #008000">释放资源</span>
- <span style="color: #800080">$redis</span>-><span style="color: #000000">close();
- </span>?>
Ubuntu16.04下nginx+mysql+php+redis
标签:扩展 1.7 cat lnmp环境 new listen 搭建 技术 缓存