在 PHP 7.1 中安装 libsodium 时出错

Error while installing libsodium in PHP 7.1

我在 PHP 7.1.25 中,mcrypt 是当前使用的扩展,mcrypt 将在 PHP 7.2 中弃用,Libsodium 将添加到 [=21= 中的核心扩展中] 7.2

现在我需要尝试在 PHP 7.1 中安装 libsodium,看看它是如何工作的,按照这个 https://lukasmestan.com/install-libsodium-extension-in-php7 但是安装失败了。也更新了 PECL 包。查看下面的错误!

如有任何帮助,我们将不胜感激。提前致谢!

$ sudo pecl install -f libsodium
downloading libsodium-2.0.20.tgz ...
Starting to download libsodium-2.0.20.tgz (28,504 bytes)
.........done: 28,504 bytes
4 source files, building
running: phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
building in /tmp/pear/temp/pear-build-rootsL5uMO/libsodium-2.0.20
running: /tmp/pear/temp/libsodium/configure --with-php-config=/usr/bin/php-config
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
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-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php/20160303 -I/usr/include/php/20160303/main -I/usr/include/php/20160303/TSRM -I/usr/include/php/20160303/Zend -I/usr/include/php/20160303/ext -I/usr/include/php/20160303/ext/date/lib
checking for PHP extension directory... /usr/lib/php/20160303
checking for PHP installed headers prefix... /usr/include/php/20160303
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 for sodium support... yes, shared
checking for pkg-config... no
checking for libsodium... configure: error: Please install libsodium - See https://github.com/jedisct1/libsodium
ERROR: `/tmp/pear/temp/libsodium/configure --with-php-config=/usr/bin/php-config' failed

@VenkateshLB 您必须先安装 linux 发行版提供的 libsodium。

CentOS 7:

yum install libsodium libsodium-devel

Ubuntu 18.04:

apt install libsodium libsodium-dev

安装完成后,执行PECL命令。

这对我不起作用 (Ubuntu 16.04)(让 $ pecl install -f libsodium 工作):

$ apt install libsodium libsodium-dev

这里我得到了以下错误:

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libsodium

有效的方法是手动下载 libsodium 并编译它:

下载 libsodium 源码并解压

$ wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
$ tar -xzf LATEST.tar.gz

编译 libsodium

$ cd libsodium-stable/
$ ./configure
$ make && make check
$ make install

参见:https://libsodium.gitbook.io/doc/installation

之后通过 PECL 安装 libsodium 成功了:

$ pecl install -f libsodium

疑难解答:

$ pecl install -f libsodium 还是不行

检查是否安装了PECL:

$ pecl version
PEAR Version: ...
PHP Version: ...
Zend Engine Version: ...
Running on: ...

如果出现错误,则必须先安装 PECL:

$ apt install php-pear

检查 phpize 是否可用(为 PHP 编译 libsodium):

$ phpize --version
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

如果出现错误,则必须安装 php7.1-dev:

$ apt install php7.1-dev

如何激活PHP中的钠?

您只需在 PHP 扩展目录(可能在 /etc/php/7.1/mods-available/)中创建一个文件 sodium.ini

内容:

extension=sodium.so

您可以通过以下方式激活模块:

$ phpenmod -v 7.1 sodium

(可以通过 phpdismod -v 7.1 sodium 完成停用。)

phpinfo 现在应该列出模块:

$ php -i | grep sodium
/etc/php/7.1/cli/conf.d/20-sodium.ini,
sodium
sodium support => enabled
sodium compiled version => 2.0.20
libsodium headers version => 1.0.18
libsodium library version => 1.0.18

因为它帮助了很多人,所以将我的评论更改为答案:

我需要 运行 sudo apt install libsodium-dev 然后 sudo pecl install libsodium,然后它告诉我 'You should add "extension=sodium.so" to php.ini'。