运维日常:五分钟搞定PHP的redis问题

栏目: PHP · 发布时间: 6年前

内容简介:2.解决LNMP环境中的PHP缺少redis扩展包,导致访问页面报错500,处理问题,安装PHP扩展功能redis。下面记录整个过程,如果不想看,而只想在最短的时间内搞定这个问题,请直接翻到最后,使用脚本。

一、前言

1.需求

运维日常:五分钟搞定 <a href='https://www.codercto.com/topics/18749.html'>PHP</a> 的 <a href='https://www.codercto.com/topics/18994.html'>redis</a> 问题

2.解决

LNMP环境中的PHP缺少redis扩展包,导致访问页面报错500,处理问题,安装PHP扩展功能redis。

下面记录整个过程,如果不想看,而只想在最短的时间内搞定这个问题,请直接翻到最后,使用脚本。

二、安装

1.下载扩展包

[root@lp 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检测安装

[root@lp source]# cd phpredis/
[root@lp phpredis]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012

3.预编译

[root@lp 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.编译安装

[root@lp 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文件**
[root@lp phpredis]# vim /usr/local/php/etc/php.ini
[root@lp phpredis]# tail -n 1 /usr/local/php/etc/php.ini
extension="redis.so"

**6.重启LNMP环境**
[root@lp 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扩展**
[root@lp 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!!!"

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python学习手册(第4版)

Python学习手册(第4版)

[美] Mark Lutz / 李军、刘红伟 / 机械工业出版社 / 2011-4 / 119.00元

Google和YouTube由于Python的高可适应性、易于维护以及适合于快速开发而采用它。如果你想要编写高质量、高效的并且易于与其他语言和工具集成的代码,《Python学习手册:第4 版》将帮助你使用Python快速实现这一点,不管你是编程新手还是Python初学者。本书是易于掌握和自学的教程,根据作者Python专家Mark Lutz的著名培训课程编写而成。 《Python学习手册:第......一起来看看 《Python学习手册(第4版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换