天天看點

redis和redis php擴充安裝(轉)

redis是一個記憶體資料庫,比memcache支援更豐富的value類型,新浪微網誌就使用redis來做緩存。

redis的源碼安裝

1.make時可能會報如下錯誤:

解決辦法: 編輯src/.make-settings裡的OPT,改為OPT=-O2 -march=i686。

2.make test報錯:

解決辦法安裝tcl

make test時報如下錯誤:

!!! WARNING The following tests failed:

*** [err]: Detect write load to master in tests/integration/replication-psync.tcl

Can't detect write load from background clients.

*** [err]: Test replication partial resync: no backlog in tests/integration/replication-psync.tcl

Expected condition '[s -1 sync_partial_err] > 0' to be true ([s -1 sync_partial_err] > 0)

*** [err]: Connect multiple slaves at the same time (issue #141), diskless=yes in tests/integration/replication.tcl

我是在虛拟機中跑的make test,在github上有一個說明

For timing issues, one test isn't very representative. Did you try running them 5-10 times? Is there anything unusual about your machine (very small memory, very slow, shared, overloaded, etc)? Some of the tests are based on timing, so if the machine can't deliver results in time then tests can't complete properly. (you can manually edit some of the tests to increase the timeout waiting)

大意是說有些測試點在配置比較低的機器上會因為逾時而過不了,我在虛拟機上跑的,是以很有可能是這個問題。

http://blog.csdn.net/ldar2011/article/details/42773583

編輯檔案tests/integration/replication-psync.tcl

然後找到after 100 把此值修改成200或者300。重新執行make test就可以了

redis指令介紹

Redis 由四個可執行檔案:redis-benchmark、redis-cli、redis-server、redis-stat 這四個檔案,加上一個redis.conf就構成了整個redis的最終可用包。它們的作用如下:

redis-server:Redis伺服器的daemon啟動程式 redis-cli:Redis指令行操作工具。當然,你也可以用telnet根據其純文字協定來操作 redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能 redis-stat:Redis狀态檢測工具,可以檢測Redis目前狀态參數及延遲狀況 現在就可以啟動redis了,redis隻有一個啟動參數,就是他的配置檔案路徑。

啟動redis

複制源碼包裡的redis.conf到/etc # cd redis-stable # cp redis.conf /etc/redis.conf

編輯/etc/redis.conf ,修改 daemaon no 為daemaon yes ,以守護程序方式啟動程序。

# redis-server /etc/redis.conf

關閉redis  # redis-cli shutdown //關閉所有 關閉某個端口上的redis # redis-cli -p 6397 shutdown //關閉6397端口的redis 說明:關閉以後緩存資料會自動dump到硬碟上,硬碟位址見redis.conf中的dbfilename dump.rdb

redis配置

注意,預設複制過去的redis.conf檔案的daemonize參數為no,是以redis不會在背景運作,這時要測試,我們需要重新開一個終端。修改為yes則為背景運作redis。另外配置檔案中規定了pid檔案,log檔案和資料檔案的位址,如果有需要先修改,預設log資訊定向到stdout.

下面是redis.conf的主要配置參數的意義:

daemonize:是否以背景daemon方式運作 pidfile:pid檔案位置 port:監聽的端口号 timeout:請求逾時時間 loglevel:log資訊級别 logfile:log檔案位置 databases:開啟資料庫的數量 save * *:儲存快照的頻率,第一個*表示多長時間,第三個*表示執行多少次寫操作。在一定時間内執行一定數量的寫操作時,自動儲存快照。可設定多個條件。 rdbcompression:是否使用壓縮 dbfilename:資料快照檔案名(隻是檔案名,不包括目錄) dir:資料快照的儲存目錄(這個是目錄) appendonly:是否開啟appendonlylog,開啟的話每次寫操作會記一條log,這會提高資料抗風險能力,但影響效率。 appendfsync:appendonlylog如何同步到磁盤(三個選項,分别是每次寫都強制調用fsync、每秒啟用一次fsync、不調用fsync等待系統自己同步) 這時你可以打開一個終端進行測試了,配置檔案中預設的監聽端口是6379

redis開機自動啟動

用這個腳本管理之前,需要先配置下面的核心參數,否則Redis腳本在重新開機或停止redis時,将會報錯,并且不能自動在停止服務前同步資料到磁盤上:

# vi /etc/sysctl.conf

vm.overcommit_memory = 1

然後應用生效:

# sysctl –p

建立redis啟動腳本:

# vim /etc/init.d/redis

然後增加服務并開機自啟動:

redis php擴充安裝

wget https://github.com/nicolasff/phpredis/zipball/master -O php-redis.zip

unzip php-redis.zip

cd nicolasff-phpredis-2d0f29b/

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make && make install

完成後redis.so被安裝到 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

vi /usr/local/php/lib/php.ini

添加 extension=redis.so

重新開機php-fpm即可。

configure時可能會遇到,添加--with-php-config參數可以解決。

configure: error: Cannot find php-config. Please use --with-php-config=PATH

http://www.nginx.cn/1024.html

verything you should need to install PhpRedis on your system.

If you would like phpredis to serialize your data using the igbinary library, run configure with <code>--enable-redis-igbinary</code>. <code>make install</code> copies <code>redis.so</code> to an appropriate location, but you still need to enable the module in the PHP config file. To do so, either edit your php.ini or add a redis.ini file in <code>/etc/php5/conf.d</code> with the following contents: <code>extension=redis.so</code>.

You can generate a debian package for PHP5, accessible from Apache 2 by running <code>./mkdeb-apache2.sh</code> or with <code>dpkg-buildpackage</code> or <code>svn-buildpackage</code>.

https://github.com/phpredis/phpredis#installation

在linux機器上,有yum指令就行。

phpize是屬于php-devel的内容,是以隻要運作  yum install php-devel就行。

phpize簡介

phpize 是屬于 php-devel 中的東西,主要是設定 php 外挂子產品的一些設定

是以安裝 php-devel 相關套件就會有 phpize 可以使用 (檔案預設存放于 /usr/bin/phpize )

phpize 指令是用來準備 PHP 外挂子產品的編譯環境的。下面例子中,外挂子產品的源程式位于 extname 目錄中 :

$ cd extname $ phpize $ ./configure ( 注一) $ make $ make install

成功的安裝将建立 extname.so 并放置于 PHP 的外挂子產品目錄中 (預設存放于 /usr/lib/php/modules/ 内) 。

需要調整 php.ini,加入 extension=extname.so 這一行之後才能使用此外挂子產品。

注一:

如在執行 

./configure 時出現  not find –with-php-config 時,

可重下以下指令,因 –with-php-config 預設在 /usr/bin/php-config 可找到

./configure –with-php-config=/usr/bin/php-config

需要調整 php.ini,加入 extension=extname.so 這一行之後才能使用此擴充庫。

phpize給PHP動态添加擴充

使用php的常見問題是編譯php時忘記添加某擴充,後來想添加擴充,但是因為安裝php後又裝了一些東西如PEAR等,不想删除目錄重裝,這裡就需要用到phpize了。

如我想增加bcmath擴充的支援,這是一個支援大整數計算的擴充。windows自帶而且内置,linux“本類函數僅在 PHP 編譯時配置了 --enable-bcmath 時可用”(引号内是手冊中的話)

注意,有些擴充需要和php的版本保持一緻才可以的.

解壓bcmath包,進入裡面的ext/bcmath目錄,然後執行/usr/local/php/bin/phpize,phpize在php安裝完以後會有這個指令的, 會發現目前目錄下多了一些configure檔案,然後再執行./configure指令即可.

#/usr/local/php/bin/phpize #./configure --with-php-config=/usr/local/php/bin/php-config 注意要先確定/usr/local/php/bin/php-config存在。 (如果你的php安裝路徑不是預設的,請修改為php安裝的路徑)

如果沒有報錯,則make,再make install ,然後它告訴你一個目錄.

#make #make install 你把該目錄下的bcmath.so拷貝到你php.ini中的extension_dir指向的目錄中,

修改php.ini,在最後添加一句

extension=bcmath.so 重新開機WEB服務,再執行phpinfo(),驚喜發現:

到此bcmath擴充已經安裝成功!

另附: Linux下利用phpize安裝php擴充 php有很多擴充功能,我們在初次安裝的時候并沒有安裝某些擴充,可能在使用的過程中,又需要用到這些擴充。php提供了一個phpize工具供我們安裝需要的擴充。 下面我通過安裝socket擴充來介紹phpize的使用: 1.找到自己的php安裝目錄,例如我的目錄是home/vsrank/php,在該目錄下,找到bin/phpize。如果沒有這個工具,則說明沒有安裝該工具,那麼需要安裝php.dev,一般都會有這個工具。 2.要擴充的話,就需要有一個和目前已安裝的php的版本一樣的php的源包,目前php版本可以用過phpinfo()檢視。就是初次安裝後檢視安裝是否成功的那個test.php。 3.打開源包目錄,進入到ext目錄,例如我就進入到:/home/vsrank/php-5.3.10/ext下,ext下有各個php帶有的擴充子產品,進入到ext/sockets中。 4.cd到ext/sockets後,執行下面的指令:

/home/vsrank/php/bin/phpize 即執行phpize工具,執行後,可以看到目錄下生成了對應的configure檔案:

5.現在就可以通過configure來配置,執行下面的指令:

./configure --enable-sockets --with-php-config=/home/vsrank/php/bin/php-config make make install 執行之後,可以看到下面的輸出:

Installing shared extensions:     /home/vsrank/php/lib/php/extensions/no-debug-non-zts-20090626/ Installing header files:          /home/vsrank/php/include/php/ 第一個就是擴充子產品的生成目錄,可以在對應目錄下看到對應的sockets.so檔案。

6.更改php.ini,增加下面的語句: extension="/home/vsrank/php/lib/php/extensions/no-debug-non-zts-20090626/sockets.so" 可以看到和上面的輸出是一緻的。

7.重新開機Apache,接下來就可以看看自己的socket是不是配置好了。。

http://my.oschina.net/junn/blog/158684