目录
一、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-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/>';
echo '<br/>';
//释放资源
$redis->close();
?>
安装完nginx+php+mysql后运行php报错有一下几种:
1.No input file specified.
删除原先文件夹中 .user.ini 的隐藏文件
sudo rm .user.ini
给网站文件夹足够的权限
sudo chmod -R 777 [网站目录]
2.Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
这是因为在上一步中并没有安装PHP-mysql插件,解决方法:
sudo apt-get install php-mysql
然后重启下php-fpm
service php7.0-fpm restart
nginx -s reload
/etc/nginx/sites-available/中的default改成下面这样(红色标记),sites-enabled 也同步映射:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /wwwroot;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME /wwwroot$fastcgi_script_name;
include fastcgi_params;
#
# # 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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
fastcgi_params最后一行加上
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
最后重启下nginx
sudo nginx -s reload
大功告成!