linux centos7 lnmp 安裝redis及php開啟redis擴充套件 自測
最近專案需要把memcache換成redis,所以就安裝redis,大概費了三四個小時把,才把各種坑填滿,現在來了解一下:
首先,你的linux環境能用,php也可以正常訪問,就是說lnmp環境沒問題,那麼我們來一起看一下,由於是昨天安裝的,所以圖片之類的,就是找的別人跟我出一樣錯誤的部落格上找的,但內容是自己的。
一、伺服器安裝redis
1.先去網址下載redis
wget http://download.redis.io/releases/redis-2.8.13.tar.gz
然後解壓
tar zxvf redis-2.8.13.tar.gz cd redis-2.8.13
然後
make PREFIX=/usr/local/redis install
#PREFIX=/usr/local/redis可以省略,省略情況下redis會預設安裝到/usr/local/bin目錄下
可能會報錯如下圖:
cd src && make install make[1]: Entering directory `/usr/export/software/redis-2.8.13/src' Hint: To run 'make test' is a good idea ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/usr/export/software/redis-2.8.13/src'
根據提示執行:cd src && make install
[[email protected] redis-2.8.13]# cd src && make install
Hint: To run 'make test' is a good idea ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
[[email protected] src]#
ok沒有報錯,當然你也可以測試一下,回到上個目錄,執行 make test
[[email protected] redis-2.8.13]# make test
Cleanup: may take some time... OK
Starting test server at port 11111
底下會顯示很多ok,然後等會就可以了,我測試的是到36
然後,再次執行 make install
執行完之後,算是安裝了一大半,接著改一下配置檔案
--- 建立redis日誌目錄,和資料檔案目錄
[[email protected] src]#cd /usr/local/redis-2.8.13
[[email protected] redis-2.8.13]# cp redis.conf /usr/local/redis/
[[email protected] redis-2.8.13]# mkdir -p /usr/local/redis/log
[[email protected] redis-2.8.13]# mkdir -p /usr/local/redis/db
修改redis.conf配置項
[[email protected] redis-2.8.13]# cd /usr/local/redis/
[[email protected] redis]# vi redis.conf
#修改下面內容
daemonize yes #確保守護程序開啟
pidfile /usr/local/redis/redis.pid
logfile /usr/local/redis/log
dir /usr/local/redis/db
#解釋一下
#pidfile 當redis作為守護程序執行的時候,它會把 pid 預設寫到 /var/run/redis.pid 檔案裡面,
# logfile 就是redis日誌
# dir 工作目錄
# 例如上面的 dbfilename 只指定了檔名,
# 但是它會寫入到這個目錄下。這個配置項一定是個目錄,而不能是檔名。
# daemonize redis採用的是單程序多執行緒的模式。當redis.conf中選項daemonize設定成yes時,代表開啟
# 守護程序模式。在該模式下,redis會在後臺執行,並將程序pid號寫入至redis.conf選項pidfile設定的檔案中,
# 此時redis將一直執行,除非手動kill該程序。
啟動redis
[[email protected] redis]#cd /usr/local/redis/bin/
[[email protected] bin]# ./redis-server
OK,開啟成功,當然有種錯誤是這個
表示redis已經啟用,關閉重新開啟就是了
然後就是設定開機自啟
vi /etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=/usr/local/redis/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
echo "start SUCCESS"
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
新增許可權
chmod 777 /etc/init.d/redis
開機自啟
chkconfig redis on
下面你就可以用命令測試了
service redis start
service redis stop
好的,linux安裝redis已經完成,現在要做的是,php開啟redis擴充套件,讓php可以操作redis
二、php開啟redis擴充套件
在上面redis已經安裝成功,並且php沒有問題情況下,可以讓php開啟redis擴充套件
1.下載資源
[[email protected] local]# wget https://github.com/phpredis/phpredis/archive/2.2.8.tar.gz
2.解壓安裝,並進入redis目錄[[email protected] local]#tar -zxvf 2.2.8.tar.gz
[[email protected] local]#cd phpredis-2.2.8/3.檢視你的phpize在哪個目錄下面,找到它,下面是我php的路徑
[[email protected]localhost phpredis-2.2.8]#/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
記得用root使用者
[[email protected] phpredis-2.2.8]# ./configure --with-php-config=/usr/local/php/bin/php-config
[[email protected] phpredis-2.2.8]# make
[[email protected] phpredis-2.2.8]# make install
4.修改你的php.ini檔案,新增redis擴充套件
extension=redis.so
重啟php-fpm
然後
php -m
命令可以看到redis擴充套件已經開啟,當然了我安裝的也沒那麼順利,不過關機之後再次開機,沒問題的話就都好了,所以強烈建議,當所有東西都設定完畢之後,關機,重啟是萬能的