Ubuntu16.04下nginx+mysql+php+redis

來源:互聯網
上載者:User

標籤:擴充   1.7   cat   lnmp環境   new   listen   搭建   技術   緩衝   

一、redis簡介
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-serverapt-get install git php-devgit clone -b php7 https://github.com/phpredis/phpredis.gitcd phpredis/phpize./configuremakemake install

2.配置php的redis擴充

vi /etc/php/7.0/fpm/conf.d/redis.iniextension=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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.