gRPC für PHP installieren

Dieser Artikel erfordert mehr Erfahrung im Umgang mit Linux und ist daher nur für fortgeschrittene Benutzer gedacht.

gRPC

Dieses Tutorial beschreibt wie man die PECL-Extension gRPC einfach in PHP integriert.

Dieses Tutorial ist als praktischer Leitfaden gedacht und behandelt keine theoretischen Hintergründe. Diese werden in einer Vielzahl von anderen Dokumenten im Internet behandelt.

Für die Richtigkeit der Inhalte dieses Tutorials gebe ich keinerlei Garantie. Der hier gezeigte Weg ist nicht der einzige um ein solches System aufzusetzen, es ist lediglich, der, den ich bevorzuge.

Was ist gRPC?

gRPC (gRPC Remote Procedure Calls) ist ein Protokoll zum Aufruf von Funktionen in verteilten Computersystemen. Es basiert auf dem Standard HTTP/2 und Protocol Buffers.

Weblinks

https://www.grpc.io/

PECL installieren

sudo apt-get install autoconf zlib1g-dev php-dev php-pear

PHP sollte bei einer aktuellen Version mit dem Flag --with-pear installiert worden sein.

Auszug aus dem Protokoll

Creating config file /etc/php/8.0/cli/php.ini with new version
Setting up php8.0-dev (8.0.2-7+ubuntu18.04.1+deb.sury.org+1) ...
Setting up php-dev (2:8.0+81+ubuntu18.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for php8.0-cli (8.0.2-7+ubuntu18.04.1+deb.sury.org+1) ...

gRPC-Erweiterung installieren

sudo pecl install grpc

Auszug aus dem Protokoll

WARNING: channel “pecl.php.net” has updated its protocols, use “pecl channel-update pecl.php.net” to update
downloading grpc-1.36.0.tgz …
Starting to download grpc-1.36.0.tgz (4,292,034 bytes)

1990 source files, building
running: phpize
Configuring for:
PHP Api Version: 20200930
Zend Module Api No: 20200930
Zend Extension Api No: 420200930
building in /tmp/pear/temp/pear-build-rootPwYZuz/grpc-1.36.0
running: /tmp/pear/temp/grpc/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 pkg-config… /usr/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-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/20200930 -I/usr/include/php/20200930/main -I/usr/include/php/20200930/TSRM -I/usr/include/php/20200930/Zend -I/usr/include/php/20200930/ext -I/usr/include/php/20200930/ext/date/lib
checking for PHP extension directory… /usr/lib/php/20200930
checking for PHP installed headers prefix… /usr/include/php/20200930
checking if debug is enabled… no
checking if zts is enabled… no
checking for gawk… gawk
checking whether to enable grpc support… yes, shared
checking for g++… g++
checking whether we are using the GNU C++ compiler… yes
checking whether g++ accepts -g… yes
checking how to run the C++ preprocessor… g++ -E
checking how to print strings… printf
checking for a sed that does not truncate output… (cached) /bin/sed
checking for fgrep… /bin/grep -F
checking for ld used by cc… /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld… yes
checking for BSD- or MS-compatible name lister (nm)… /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface… BSD nm
checking whether ln -s works… yes
checking the maximum length of command line arguments… 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format… func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format… func_convert_file_noop
checking for /usr/bin/ld option to reload object files… -r
checking for objdump… objdump
checking how to recognize dependent libraries… pass_all
checking for dlltool… no
checking how to associate runtime and link libraries… printf %s\n
checking for ar… ar
checking for archiver @FILE support… @
checking for strip… strip
checking for ranlib… ranlib
checking for gawk… (cached) gawk
checking command to parse /usr/bin/nm -B output from cc object… ok
checking for sysroot… no
checking for a working dd… /bin/dd
checking how to truncate binary pipes… /bin/dd bs=4096 count=1
checking for mt… mt
checking if mt is a manifest tool… no
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 for dlfcn.h… yes
checking for objdir… .libs
checking if cc supports -fno-rtti -fno-exceptions… no
checking for cc option to produce PIC… -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works… yes
checking if cc static flag -static works… yes
checking if cc supports -c -o file.o… yes
checking if cc supports -c -o file.o… (cached) 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
checking how to run the C++ preprocessor… g++ -E
checking for ld used by g++… /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld… yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries… yes
checking for g++ option to produce PIC… -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works… yes
checking if g++ static flag -static works… yes
checking if g++ supports -c -o file.o… yes
checking if g++ supports -c -o file.o… (cached) yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries… yes
checking dynamic linker characteristics… (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs… immediate
configure: patching config.h.in
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands
running: make

Erfolg der Installation nach wenigen Minuten

Build process completed successfully
Installing '/usr/..../grpc.so'
install ok: channel://..../grpc-xx
configuration option "php_ini" is not set to php.ini location
You should add "extension=grpc.so" to php.ini

Wenn, wie erwartet, alles ohne Fehler durchgelaufen ist, fügen wir die Erweiterung unserer Konfiguration hinzu:

echo "extension=grpc.so" >> /path/to/php.ini

Restart PHP / Apache

For php-fpm:

service php7-fpm restart

service apache2 restart

PHP Info

Anmerkung

Man kann das Paket auch über das PPA ppa:ondrej/php erhalten

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don’t ask for end-of-life PHP versions or Ubuntu release, they won’t be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The -backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:

  1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
  2. If you are using apache2, you are advised to add ppa:ondrej/apache2
  3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
       or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

sudo apt install php7.4-gRPC