在PHP中使用redis

yipeiwu_com7年前PHP代码库

在Mac OS上安装redis
首先是安装,它会默认安装到/usr/local/bin下

复制代码 代码如下:

cd /tmp
wget http://redis.googlecode.com/files/redis-2.6.9.tar.gz
tar -zxf redis-2.6.9.tar.gz
cd redis-2.6.9
make
sudo make install

然后下载一些配置文件(主要就是把deamon打开之类的,没对比与默认配置的区别)
复制代码 代码如下:

wget https://github.com/ijonas/dotfiles/raw/master/etc/redis.conf
sudo mv redis.conf /etc/redis.conf
sudo /usr/local/bin/redis-server redis.conf
说到这里备注下,如果没有目录权限,是无法建立
/var/log/redis/redis.log
/var/lib/redis/

导致redis启动失败
ok,现在已经大功告成,你的redis已经成功运行起来了。
试试看吧!
复制代码 代码如下:

/opt/redis/redis-cli
#会看到提示 redis 127.0.0.1:6379>说明已经连接服务。
set anythink helloworld
get anythink
exit

good 看到了helloworld,说明一切正常。
如果我需要停止redis或者需要重新启动呢?
复制代码 代码如下:

cat /opt/redis/redis.pid
#cat后会得到一个pid,我的是44277
sudo kill 44277
# 启动方法和之前一样。

设置开机自启动、后台运行
然后以root身份做以下事情:
在/Library/LaunchDaemons下新建com.redis.plist,内容如下:

复制代码 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.redis</string>
        <key>RunAtLoad</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/bin/redis-server</string>
                <string>/etc/redis.conf</string>
        </array>
</dict>
</plist>

之后运行
复制代码 代码如下:

sudo launchtcl load /Library/LaunchDaemons/com.redis.plist
sudo launchtcl start com.redis

检查一下情况:
复制代码 代码如下:

$ cat /var/run/redis.pid

如果出来pid的数字,说明就运行了~
安装php-redis扩展
如果你需要在PHP中使用redis,那么请继续往下看
复制代码 代码如下:

curl -O https://nodeload.github.com/nicolasff/phpredis/zip/master
tar -zxf master
cd phpredis-master/
phpize
./configure
make
sudo make install

# 这时候会提示一个路径
# /usr/lib/php/extensions/no-debug-non-zts-20090626/
# 表示已经将扩展放置在该位置
vim /etc/php.ini

#增加如下内容
extension=redis.so

#重启apache
sudo httpd -k restart

#查看扩展安装情况
php -m |grep redis
#出现 redis 表示安装成功。

如果执行phpize提示如下错误
Cannot find autoconf. Please check your autoconf installation
and the $PHP_AUTOCONF environment variable.
Then, rerun this script.

请分别下载M4,autoconf编译安装

复制代码 代码如下:

curl -O http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz

注意,以上使用的apache、php均为MacOS自带的,如果是自己安装的phpize请指定绝对路径。
图形管理工具
另:redis还有一个基于WEB的图形界面管理工具,叫phpRedisAdmin,如果刚开启服务会出现一些Undefined index,过一会就好了。如果想试试可以使用如下命令安装(git推荐使用SourceTree安装)该管理工具支持String、Hash、List、Set、Zset
复制代码 代码如下:

git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git
cd phpRedisAdmin/
git clone https://github.com/nrk/predis.git

相关文章

php 根据自增id创建唯一编号类

在开发过程中,我们数据表一般都使用自增数字作为id主键,而id是数字型,不容易理解。我们把id按一定格式转为编号后,很容易根据编号知道代表的是什么内容。 例如订单表id=20160111...

php数组索引的Key加引号和不加引号的区别

今天在看一个PHP博客时留意了这么一句话:“PHP中的索引KEY值如果不用引号括起来的话,会将索引KEY值解释为一个常量,当找不到该常量的定义时,才将其解释为一个字符串”。我有点不太相信...

PHP的preg_match匹配字符串长度问题解决方法

项目中,用preg_match正则提取目标内容,死活有问题,代码测得死去活来。 后来怀疑PHP 的preg_match有字符串长度限制,果然,发现“pcre.backtrack_limi...

PHP获取文件绝对路径的代码(上一级目录)

PHP获取文件绝对路径 复制代码 代码如下: <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirnam...

php实现URL加密解密的方法

本文实例讲述了php实现URL加密解密的方法。分享给大家供大家参考,具体如下: <html xmlns="http://www.w3.org/1999/xhtml" lang=...