在本文中,我们将详细介绍如何在共享托管环境下手动安装php扩展。通常,人工安装php扩展共分为以下四个步骤。
确定你需要安装的扩展首先,你需要确定你需要安装的扩展。可以通过在终端中键入以下命令来获取当前运行的php版本的扩展列表:php -m。这会返回一个扩展列表,其中包含当前php版本所支持的所有扩展。
下载并解压扩展包一旦你确定了需要安装的扩展,就需要下载相应的扩展包。你可以从pecl(php extension community library,php扩展社区库)或github等地方获取扩展包。下载扩展包后,你需要使用终端进入该目录并执行以下命令来解压缩它:tar -xzvf <filename>.tar.gz。
运行configure文件在解压缩之后,你需要运行configure文件。这个文件的作用是为扩展准备一些必要的元素。如果configure文件需要进行配置,请先查看你的php信息,然后在命令中添加相应的选项和参数。例如,如果你需要为php 7.3配置redis扩展,你应该执行以下命令:
cd redis-4.1.1
phpize
./configure --with-php-config=/usr/bin/php-config7.3
在执行这个命令之后,会看到一些输出。如果所有必要的依赖库都被找到,并且configure脚本成功运行,则输出应该类似于以下内容:
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -e
checking for a sed that does not truncate output... /bin/sed
configure: warning:
configure: warning: support for running as a daemon, on linux with systemd target type, is experimental at best, and often broken.
configure: warning: don't rely on systemd support, until it is declared stable.
configure: warning:
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
编译和安装扩展最后一步是编译和安装扩展。为编译扩展,你需要在终端中执行以下命令:make。这将从源代码构建扩展。如果执行此命令成功,则可以继续安装扩展。在安装扩展之前,你可以将其复制到php扩展目录(通常在/usr/lib/php/extensions)中。然后,在你的php.ini文件(通常在/etc/php/7.x/cli/php.ini)中添加一个新的扩展引用,如下所示:extension=<extension-name>.so。
在完成上述步骤之后,你需要重新启动apache或php fastcgi。然后,你可以运行php -m命令来验证该扩展是否已成功安装。
总结
在使用共享托管服务时手动安装php扩展也是一个可行的选择。你可以通过确定需要安装的扩展、下载和解压扩展包、运行configure文件以及编译和安装扩展这四个步骤,来手动安装php扩展。这种方法可能比直接从共享托管服务提供商那里获取php扩展更有挑战性,但有时也是必要的。如何去选择,很大程度上取决于你的具体需求和环境。
以上就是php手动安装扩展的详细内容。
