MAC Lion 下安裝php擴展 for XAMPP 之Redis


      mac lion下自帶的apache,擴展很少,另外我自己嘗試用php源碼編譯未通過,因為決定用一個集成的mamp,那就是xampp(一定要安裝那個xampp開發包,不然編譯php擴展的時候會出錯)。在windows和linux 下安裝一些程序或者php的擴展資料很多,而關於mac最新版10.7.4的資料極少。順便提下mac的好用的安裝工具,一個是MacPort,一個是Homebrew.macport可能被牆,安裝時可能會卡住。

     下面進入正題,例如今天先安裝redis和php-redis。關於redis for mac的資料在https://github.com/antirez/redis ,順便說下讓redis開機自動運行的設置:http://naleid.com/blog/2011/03/05/running-redis-as-a-user-daemon-on-osx-with-launchd/ 說的很清楚。我還是引用過來作為參考,注意我加粗加紅的地方。

 

If you’re developing on the mac using redis and want it to start automatically on boot, you’ll want to leverage the OSX launchd system to run it as a User Daemon. A User Daemon is a non-gui program that runs in the background as part of the system. It isn’t associated with your user account. If you only want redis to launch when a particular user logs in, you’ll want to make a User Agent instead.

From the command line, create a plist file as root in the /Library/LaunchDaemons directory with your favorite text editor:

sudo vim /Library/LaunchDaemons/io.redis.redis-server.plist

Paste in the following contents and modify it to point it to wherever you’ve got redis-server installed and optionally pass the location of a config file to it (delete the redis.conf line if you’re not using one):

<?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>io.redis.redis-server</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/bin/redis-server</string>
		<string>/usr/local/etc/redis.conf</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

Make sure that you actually have a redis.conf file at the location above. If you’ve installed it withhomebrew that should be the correct location.

You’ll then need to load the file (one time) into launchd with launchctl:

sudo launchctl load /Library/LaunchDaemons/io.redis.redis-server.plist

Redis will now automatically be started after every boot. You can manually start it without rebooting with:

sudo launchctl start io.redis.redis-server

You can also shut down the server with

sudo launchctl stop io.redis.redis-server

Or you could add these aliases to your bash/zsh rc file:

alias redisstart='sudo launchctl start io.redis.redis-server'
alias redisstop='sudo launchctl stop io.redis.redis-server'

If you’re having some sort of error (or just want to watch the logs), you can just fire up Console.app to watch the redis logs to see what’s going on.

說了這么多,還沒有開始安裝php-redis.下面開始:

首先用git從https://github.com/nicolasff/phpredis下載源碼。然后依次執行以下命令:

sudo /Applications/XAMPP/xamppfiles/bin/phpize
sudo MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch i386 -arch x86_64 -bind_at_load" ./configure --enable-redis --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config
sudo make && sudo make install

然后修改php.ini(增加:extension=redis.so),重啟xampp服務就可以了。

 

示例代碼

 

<?php

echo 'phpredis sample:<br />';

 

error_reporting(E_ALL);

ini_set('display_errors','ON');

 

$redis = new Redis();

$redis->connect('127.0.0.1',6379);

$redis->set('first_key_phpredis', 'Hello world');

 

);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM