さくらのレンタルサーバにPHP-sodium 拡張を入れようと思ったらてこずった話
Q&A
Closed
さくらのレンタルサーバにPHP-sodium 拡張を入れようと思ったらてこずった話
Laravelにて開発中のアプリケーションでJWTを使おうと思ったら、PHP-sodium が入ってないよって怒られた。
Waht's sodium?
Sodium is a modern, easy-to-use software library for encryption, decryption, signatures, password hashing, and more.
Sodium は、暗号化、復号化、署名、パスワード ハッシュなどのための最新の使いやすいソフトウェア ライブラリです。
これをPHPで利用するための拡張が PHP-sodium だそうな。
具体的にはJWTで EdDSA signer
に対応する際に導入されたらしい。
うごかすためには、レンタルサーバに Libsodium
と PHP拡張のコンパイルが必要。
かつて intl
を入れるときにやった手順と同じような感じでできるやろう。と気軽に踏み入れたのは沼でした。
# スペック確認
$ uname -a
FreeBSD www0000.sakura.ne.jp 11.2-RELEASE-p15 FreeBSD 11.2-RELEASE-p15
(中略)/usr/obj/home/pkg/src/sys/GENERIC amd64
$ php -v
PHP 8.0.21 (cli) (built: Jul 27 2022 10:04:58) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.21, Copyright (c) Zend Technologies
with Zend OPcache v8.0.21, Copyright (c), by Zend Technologies
発生している問題・エラー
取り急ぎ、公式サイト見ながら Libsodium
をコンパイル
# 作業用ディレクトリの作成
$ mkdir -p {~/usr/local/src,~/usr/local/php/extension}
# libsodium のダウンロードとコンパイル
$ cd ~/usr/local/src
$ wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz
$ tar xzvf libsodium-1.0.18.tar.gz
$ cd libsodium-1.0.18
$ ./configure --prefix=$HOME/usr/local
$ make && make check
$ make install
$ ls -al ~/usr/local/lib/
total 5392
drwxr-xr-x 3 neko users 512 10月 17 11:48 .
drwxr-xr-x 6 neko users 512 10月 17 11:48 ..
-rw-r--r-- 1 neko users 3324628 10月 17 11:48 libsodium.a
-rwxr-xr-x 1 neko users 955 10月 17 11:48 libsodium.la
lrwxr-xr-x 1 neko users 19 10月 17 11:48 libsodium.so -> libsodium.so.23.3.0
lrwxr-xr-x 1 neko users 19 10月 17 11:48 libsodium.so.23 -> libsodium.so.23.3.0
-rwxr-xr-x 1 neko users 2082456 10月 17 11:48 libsodium.so.23.3.0
drwxr-xr-x 2 neko users 512 10月 17 11:48 pkgconfig
ここまではたぶん良い
# PHP拡張のダウンロード&コンパイル
$ cd ~/usr/local/src
$ wget https://github.com/php/php-src/archive/php-8.0.21.zip
$ unzip php-8.0.21.zip
$ mv php-src-php-8.0.21/ext/sodium ./sodium-php-8.0.21
$ cd sodium-php-8.0.21
$ phpize
$ ./configure \
--with-sodium \
--libdir=$HOME/usr/local/lib \
--with-libdir=$HOME/usr/local/lib/libsodium.so \
--with-php-config=/usr/local/php/8.0/bin/php-config \
LIBSODIUM_CFLAGS=$HOME/usr/local/include \
LIBSODIUM_LIBS=$HOME/usr/local/lib \
LDFLAGS="-Wl,--rpath -Wl,LIBDIR,-L$HOME/usr/local/lib" \
CPPFLAGS="-I$HOME/usr/local/include" \
LD_LIBRARY_PATH="$HOME/usr/local/lib"
$ INSTALL_ROOT=$HOME/usr/local make install
オプションは、なんか試行錯誤を重ねてもりもりにしました。動かないけど。
# 動作確認
$ echo 'extension=$HOME/usr/local/php/extension/sodium.so' >> ~/www/neko.ini
$ php -c ~/www/neko.ini -m
/home/neko/usr/local/php/extension/sodium.so: Undefined symbol "sodium_init"
自分で試したこと
$ ./configure --with-sodium \
--prefix=$HOME/usr/local/php/extension/ \
--with-php-config=/usr/local/php/8.0/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 pkg-config... /usr/local/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
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 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-freebsd11.2
checking host system type... x86_64-unknown-freebsd11.2
checking target system type... x86_64-unknown-freebsd11.2
checking for PHP prefix... /usr/local/php/8.0
checking for PHP includes... -I/usr/local/php/8.0/include/php -I/usr/local/php/8.0/include/php/main -I/usr/local/php/8.0/include/php/TSRM -I/usr/local/php/8.0/include/php/Zend -I/usr/local/php/8.0/include/php/ext -I/usr/local/php/8.0/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/php/8.0/lib/php/extensions/no-debug-non-zts-20200930
checking for PHP installed headers prefix... /usr/local/php/8.0/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for sodium support... yes, shared
checking for libsodium >= 1.0.8... no
configure: error: Package requirements (libsodium >= 1.0.8) were not met:
Package 'libsodium', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables LIBSODIUM_CFLAGS
and LIBSODIUM_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
libsodium がないといわれたので
$ ./configure --with-sodium \
--prefix=$HOME/usr/local/php/extension/ \
--with-php-config=/usr/local/php/8.0/bin/php-config \
LIBSODIUM_CFLAGS=$HOME/usr/local/include \
LIBSODIUM_LIBS=$HOME/usr/local/lib
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 pkg-config... /usr/local/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
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 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-freebsd11.2
checking host system type... x86_64-unknown-freebsd11.2
checking target system type... x86_64-unknown-freebsd11.2
checking for PHP prefix... /usr/local/php/8.0
checking for PHP includes... -I/usr/local/php/8.0/include/php -I/usr/local/php/8.0/include/php/main -I/usr/local/php/8.0/include/php/TSRM -I/usr/local/php/8.0/include/php/Zend -I/usr/local/php/8.0/include/php/ext -I/usr/local/php/8.0/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/php/8.0/lib/php/extensions/no-debug-non-zts-20200930
checking for PHP installed headers prefix... /usr/local/php/8.0/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for sodium support... yes, shared
checking for libsodium >= 1.0.8... yes
checking for a sed that does not truncate output... /usr/bin/sed
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/local/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... 196608
checking command to parse /usr/local/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... yes
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... yes
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... freebsd11.2 ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... no
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: patching config.h.in
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
で、make
$ make
/bin/sh /home/neko/usr/local/src/sodium-php-8.0.21/libtool --mode=install cp ./sodium.la /home/neko/usr/local/src/sodium-php-8.0.21/modules
cp ./.libs/sodium.so /home/neko/usr/local/src/sodium-php-8.0.21/modules/sodium.so
cp ./.libs/sodium.lai /home/neko/usr/local/src/sodium-php-8.0.21/modules/sodium.la
----------------------------------------------------------------------
Libraries have been installed in:
/home/neko/usr/local/src/sodium-php-8.0.21/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
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'.
$ make test
Build complete.
Don't forget to run 'make test'.
*** Error code 1
Stop.
make: stopped in /home/neko/usr/local/src/sodium-php-8.0.21
make test がもうエラー
もちろん、こう
$ php -c ~/www/neko.ini -m
/home/neko/usr/local/php/extension/sodium.so: Undefined symbol "sodium_init"
ぬるま湯につかってきた身としては、コンパイル難しくて勘所さえありませぬ・・・・。
付録
Libsodium の configure のヘルプ
$ ./configure --help
`configure' configures libsodium 1.0.18 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/libsodium]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
Program names:
--program-prefix=PREFIX prepend PREFIX to installed program names
--program-suffix=SUFFIX append SUFFIX to installed program names
--program-transform-name=PROGRAM run sed PROGRAM on installed program names
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-silent-rules less verbose build output (undo: "make V=1")
--disable-silent-rules verbose build output (undo: "make V=0")
--enable-maintainer-mode
enable make rules and dependencies not useful (and
sometimes confusing) to the casual installer
--enable-dependency-tracking
do not reject slow dependency extractors
--disable-dependency-tracking
speeds up one-time build
--disable-ssp Do not compile with -fstack-protector
--disable-asm Do not compile assembly code -- As a side effect,
this disables CPU-specific implementations on
non-Windows platforms. Only for use with targets
such as WebAssembly.
--disable-pie Do not produce position independent executables
--enable-blocking-random
Enable this switch only if /dev/urandom is totally
broken on the target platform
--enable-minimal Only compile the minimum set of functions required
for the high-level API
--enable-retpoline Use return trampolines for indirect calls
--enable-debug For maintainers only - please do not use
--enable-opt Optimize for the native CPU - The resulting library
will be faster but not portable
--enable-valgrind Whether to enable Valgrind on the unit tests
(requires GNU make)
--disable-valgrind-memcheck
Whether to skip memcheck during the Valgrind tests
--disable-valgrind-helgrind
Whether to skip helgrind during the Valgrind tests
--disable-valgrind-drd Whether to skip drd during the Valgrind tests
--disable-valgrind-sgcheck
Whether to skip sgcheck during the Valgrind tests
--enable-soname-versions
enable soname versions (must be disabled for
Android) (default: enabled)
--enable-shared[=PKGS] build shared libraries [default=yes]
--enable-static[=PKGS] build static libraries [default=yes]
--enable-fast-install[=PKGS]
optimize for fast installation [default=yes]
--disable-libtool-lock avoid locking (might break parallel builds)
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-pthreads use pthreads library, or --without-pthreads to
disable threading support.
--with-safecode For maintainers only - please do not use
--with-ctgrind For maintainers only - please do not use
--with-pic[=PKGS] try to use only PIC/non-PIC objects [default=use
both]
--with-aix-soname=aix|svr4|both
shared library versioning (aka "SONAME") variant to
provide on AIX, [default=aix].
--with-gnu-ld assume the C compiler uses GNU ld [default=no]
--with-sysroot[=DIR] Search for dependent libraries within DIR (or the
compiler's sysroot if not specified).
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CPP C preprocessor
SAFECODE_HOME
set to the safecode base directory
CCAS assembler compiler command (defaults to CC)
CCASFLAGS assembler compiler flags (defaults to CFLAGS)
CWFLAGS define to compilation flags for generating extra warnings
LT_SYS_LIBRARY_PATH
User-defined run-time library search path.
AR path to the ar utility
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
Report bugs to <https://github.com/jedisct1/libsodium/issues>.
libsodium home page: <https://github.com/jedisct1/libsodium>.
PHP-sodium の configure のヘルプ
$ ./configure --help
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/PACKAGE]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
--target=TARGET configure for building compilers for TARGET [HOST]
Optional Features and Packages:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-libdir=NAME Look for libraries in .../NAME rather than .../lib
--with-php-config=PATH Path to php-config [php-config]
--with-sodium Include sodium support
--enable-shared=PKGS Build shared libraries default=yes
--enable-static=PKGS Build static libraries default=yes
--enable-fast-install=PKGS
Optimize for fast installation default=yes
--with-gnu-ld Assume the C compiler uses GNU ld default=no
--disable-libtool-lock Avoid locking (might break parallel builds)
--with-pic Try to use only PIC/non-PIC objects default=use both
--with-tags=TAGS Include additional configurations automatic
Some influential environment variables:
PKG_CONFIG path to pkg-config utility
PKG_CONFIG_PATH
directories to add to pkg-config's search path
PKG_CONFIG_LIBDIR
path overriding pkg-config's built-in search path
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CPP C preprocessor
LIBSODIUM_CFLAGS
C compiler flags for LIBSODIUM, overriding pkg-config
LIBSODIUM_LIBS
linker flags for LIBSODIUM, overriding pkg-config
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
Report bugs to the package provider.
0