运维日常:五分钟搞定PHP的redis问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运维日常:五分钟搞定PHP的redis问题相关的知识,希望对你有一定的参考价值。
一、前言
1.需求
2.解决
LNMP环境中的php缺少redis扩展包,导致访问页面报错500,处理问题,安装PHP扩展功能redis。
下面记录整个过程,如果不想看,而只想在最短的时间内搞定这个问题,请直接翻到最后,使用脚本。
二、安装
1.下载扩展包
[[email protected] source]# git clone https://github.com/nicolasff/phpredis
Cloning into ‘phpredis‘...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 9450 (delta 11), reused 15 (delta 7), pack-reused 9421
Receiving objects: 100% (9450/9450), 5.40 MiB | 2.10 MiB/s, done.
Resolving deltas: 100% (6483/6483), done.
2.phpize检测安装
[[email protected] source]# cd phpredis/
[[email protected] phpredis]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20151012
Zend Module Api No: 20151012
Zend Extension Api No: 320151012
3.预编译
[[email protected] phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr/local/php
checking for PHP includes... -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012
checking for PHP installed headers prefix... /usr/local/php/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking whether to enable redis support... yes, shared
checking whether to enable sessions... yes
checking whether to enable igbinary serializer support... no
checking whether to enable lzf compression... no
checking use system liblzf... no
checking for redis igbinary support... disabled
checking for git... yes
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
4.编译安装
[[email protected] phpredis]# make && make install
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis.c -o redis.lo
mkdir .libs
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis.c -fPIC -DPIC -o .libs/redis.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_commands.c -o redis_commands.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_commands.c -fPIC -DPIC -o .libs/redis_commands.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/library.c -o library.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/library.c -fPIC -DPIC -o .libs/library.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_session.c -o redis_session.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_session.c -fPIC -DPIC -o .libs/redis_session.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_array.c -o redis_array.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_array.c -fPIC -DPIC -o .libs/redis_array.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_array_impl.c -o redis_array_impl.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_array_impl.c -fPIC -DPIC -o .libs/redis_array_impl.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_cluster.c -o redis_cluster.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/redis_cluster.c -fPIC -DPIC -o .libs/redis_cluster.o
/bin/sh /server/source/phpredis/libtool --mode=compile cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/cluster_library.c -o cluster_library.lo
cc -I. -I/server/source/phpredis -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /server/source/phpredis/cluster_library.c -fPIC -DPIC -o .libs/cluster_library.o
/bin/sh /server/source/phpredis/libtool --mode=link cc -DPHP_ATOM_INC -I/server/source/phpredis/include -I/server/source/phpredis/main -I/server/source/phpredis -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -o redis.la -export-dynamic -avoid-version -prefer-pic -module -rpath /server/source/phpredis/modules redis.lo redis_commands.lo library.lo redis_session.lo redis_array.lo redis_array_impl.lo redis_cluster.lo cluster_library.lo
cc -shared .libs/redis.o .libs/redis_commands.o .libs/library.o .libs/redis_session.o .libs/redis_array.o .libs/redis_array_impl.o .libs/redis_cluster.o .libs/cluster_library.o -Wl,-soname -Wl,redis.so -o .libs/redis.so
creating redis.la
(cd .libs && rm -f redis.la && ln -s ../redis.la redis.la)
/bin/sh /server/source/phpredis/libtool --mode=install cp ./redis.la /server/source/phpredis/modules
cp ./.libs/redis.so /server/source/phpredis/modules/redis.so
cp ./.libs/redis.lai /server/source/phpredis/modules/redis.la
PATH="$PATH:/sbin" ldconfig -n /server/source/phpredis/modules
----------------------------------------------------------------------
Libraries have been installed in:
/server/source/phpredis/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR‘
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH‘ environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH‘ environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR‘ linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf‘
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don‘t forget to run ‘make test‘.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
```~~
**5.修改php.ini文件**
[[email protected] phpredis]# vim /usr/local/php/etc/php.ini
[[email protected] phpredis]# tail -n 1 /usr/local/php/etc/php.ini
extension="redis.so"
**6.重启LNMP环境**
[[email protected] phpredis]# lnmp restart
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
Stoping LNMP...
Stoping nginx... done
Shutting down mysql. SUCCESS!
Gracefully shutting down php-fpm . done
Starting LNMP...
Starting nginx... done
Starting MySQL. SUCCESS!
Starting php-fpm done
**7.检测是否安装成功redis扩展**
[[email protected] phpredis]# php -m |grep redis
redis
**一键安装**
#!/bin/bash
## 2018-10-15 write by leo;
redis=`php -m |grep redis`
yum install -y wget vim git
mkdir -pv /server/{source,web,data}
cd /server/source
git clone https://github.com/nicolasff/phpredis
cd phpredis/ && /bin/bash /usr/local/php/bin/phpize
/server/source/phpredis/configure --with-php-config=/usr/local/php/bin/php-config
if [ $? -eq 0 ];then
make && make install
else
echo "configure error!!!"
fi
echo "extension=‘redis.so‘" >> /usr/local/php/etc/php.ini
lnmp restart
if [ $? -eq 0 ];then
echo "lnmp restart finish!"
else
echo "restart services error!!!"
fi
echo "$redis ext finish!!!"
以上是关于运维日常:五分钟搞定PHP的redis问题的主要内容,如果未能解决你的问题,请参考以下文章