在PHP中使用redis_php技巧

來源:互聯網
上載者:User

在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.