Debian Codegraph @

src/php/README.md

raw
1
2 # Overview
3
4 This directory contains source code for PHP implementation of gRPC layered on
5 shared C library. The same installation guides with more examples and
6 tutorials can be seen at [grpc.io](https://grpc.io/docs/languages/php/quickstart).
7 gRPC PHP installation instructions for Google Cloud Platform is in
8 [cloud.google.com](https://cloud.google.com/php/grpc).
9
10 ## Environment
11
12 ### Prerequisites
13
14 * `php`: version 7.0 or above (PHP 5.x support is deprecated from Sep 2020).
15 * `pecl`
16 * `composer`
17 * `phpunit` (optional)
18
19
20 ## Install the _grpc_ extension
21
22 There are two ways to install the `grpc` extension.
23 * Via `pecl`
24 * Build from source
25
26 ### Install from PECL
27
28 ```sh
29 $ [sudo] pecl install grpc
30 ```
31
32 or specific version
33
34 ```sh
35 $ [sudo] pecl install grpc-1.30.0
36 ```
37
38 Please make sure your `gcc` version satisfies the minimum requirement as
39 specified [here](https://grpc.io/docs/languages/#official-support).
40
41
42 ### Install on Windows
43
44 You can download the pre-compiled `grpc.dll` extension from the PECL
45 [website](https://pecl.php.net/package/grpc).
46
47 ### Build from source
48
49 Clone this repository at the [latest stable release tag](https://github.com/grpc/grpc/releases).
50
51 ```sh
52 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
53 $ cd grpc
54 ```
55
56 #### Build the gRPC C core library
57
58 ```sh
59 $ git submodule update --init
60 $ EXTRA_DEFINES=GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK make
61 ```
62
63 #### Build and install the `grpc` extension
64
65 Compile the `grpc` extension from source
66
67 ```sh
68 $ grpc_root="$(pwd)"
69 $ cd src/php/ext/grpc
70 $ phpize
71 $ GRPC_LIB_SUBDIR=libs/opt ./configure --enable-grpc="${grpc_root}"
72 $ make
73 $ [sudo] make install
74 ```
75
76 This will compile and install the `grpc` extension into the
77 standard PHP extension directory. You should be able to run
78 the [unit tests](#unit-tests), with the `grpc` extension installed.
79
80
81 ### Update php.ini
82
83 After installing the `grpc` extension, make sure you add this line to your
84 `php.ini` file, depending on where your PHP installation is, to enable the
85 `grpc` extension.
86
87 ```sh
88 extension=grpc.so
89 ```
90
91 ## Composer package
92
93 In addition to the `grpc` extension, you will need to install the `grpc/grpc`
94 composer package as well. Add this to your project's `composer.json` file.
95
96 ```json
97 "require": {
98 "grpc/grpc": "~1.30.0"
99 }
100 ```
101
102 To run tests with generated stub code from `.proto` files, you will also
103 need the `composer` and `protoc` binaries. You can find out how to get these
104 below.
105
106 ## Protocol Buffers
107
108 gRPC PHP supports
109 [protocol buffers](https://developers.google.com/protocol-buffers)
110 out-of-the-box. You will need the following things to get started:
111
112 * `protoc`: the protobuf compiler binary to generate PHP classes for your
113 messages and service definition.
114 * `grpc_php_plugin`: a plugin for `protoc` to generate the service stub
115 classes.
116 * `protobuf.so`: the `protobuf` extension runtime library.
117
118 ### `protoc` compiler
119
120 If you don't have it already, you need to install the protobuf compiler
121 `protoc`, version 3.5.0+ (the newer the better) for the current gRPC version.
122 If you installed already, make the protobuf version is compatible to the
123 grpc version you installed. If you build grpc.so from the souce, you can check
124 the version of grpc inside package.xml file.
125
126 The compatibility between the grpc and protobuf version is listed as table
127 below:
128
129 grpc | protobuf | grpc | protobuf | grpc | protobuf
130 --- | --- | --- | --- | --- | ---
131 v1.0.0 | 3.0.0(GA) | v1.12.0 | 3.5.2 | v1.22.0 | 3.8.0
132 v1.0.1 | 3.0.2 | v1.13.1 | 3.5.2 | v1.23.1 | 3.8.0
133 v1.1.0 | 3.1.0 | v1.14.2 | 3.5.2 | v1.24.0 | 3.8.0
134 v1.2.0 | 3.2.0 | v1.15.1 | 3.6.1 | v1.25.0 | 3.8.0
135 v1.2.0 | 3.2.0 | v1.16.1 | 3.6.1 | v1.26.0 | 3.8.0
136 v1.3.4 | 3.3.0 | v1.17.2 | 3.6.1 | v1.27.3 | 3.11.2
137 v1.3.5 | 3.2.0 | v1.18.0 | 3.6.1 | v1.28.1 | 3.11.2
138 v1.4.0 | 3.3.0 | v1.19.1 | 3.6.1 | v1.29.0 | 3.11.2
139 v1.6.0 | 3.4.0 | v1.20.1 | 3.7.0 | v1.30.0 | 3.12.2
140 v1.8.0 | 3.5.0 | v1.21.3 | 3.7.0
141
142 If `protoc` hasn't been installed, you can download the `protoc` binary from
143 the protocol buffers
144 [Github repository](https://github.com/protocolbuffers/protobuf/releases).
145 Then unzip this file and update the environment variable `PATH` to include the
146 path to the protoc binary file.
147
148 If you really must compile `protoc` from source, you can run the following
149 commands, but this is risky because there is no easy way to uninstall /
150 upgrade to a newer release.
151
152 ```sh
153 $ cd grpc/third_party/protobuf
154 $ ./autogen.sh && ./configure && make
155 $ [sudo] make install
156 ```
157
158 ### `grpc_php_plugin` protoc plugin
159
160 You need the `grpc_php_plugin` to generate the PHP client stub classes. This
161 plugin works with the main `protoc` binary to generate classes that you can
162 import into your project.
163
164 You can build `grpc_php_plugin` with `cmake`:
165
166 ```sh
167 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
168 $ cd grpc
169 $ git submodule update --init
170 $ mkdir -p cmake/build
171 $ cd cmake/build
172 $ cmake ../..
173 $ make protoc grpc_php_plugin
174 ```
175
176 The commands above will make `protoc` and `grpc_php_plugin` available
177 in `cmake/build/third_party/protobuf/protoc` and `cmake/build/grpc_php_plugin`.
178
179 Alternatively, you can also build the `grpc_php_plugin` with `bazel`:
180
181 ```sh
182 $ bazel build @com_google_protobuf//:protoc
183 $ bazel build src/compiler:grpc_php_plugin
184 ```
185
186 The `protoc` binary will be found in
187 `bazel-bin/external/com_google_protobuf/protoc`.
188 The `grpc_php_plugin` binary will be found in
189 `bazel-bin/src/compiler/grpc_php_plugin`.
190
191 Plugin may use the new feature of the new protobuf version, thus please also
192 make sure that the protobuf version installed is compatible with the grpc
193 version you build this plugin.
194
195 ### `protobuf` runtime library
196
197 There are two `protobuf` runtime libraries to choose from. They are identical
198 in terms of APIs offered. The C implementation provides better performance,
199 while the native implementation is easier to install.
200
201 #### C implementation (for better performance)
202
203 Install the `protobuf` extension from PECL:
204
205 ``` sh
206 $ [sudo] pecl install protobuf
207 ```
208 or specific version
209
210 ``` sh
211 $ [sudo] pecl install protobuf-3.12.2
212 ```
213
214 And add this to your `php.ini` file:
215
216 ```sh
217 extension=protobuf.so
218 ```
219
220 #### PHP implementation (for easier installation)
221
222 Or require the `google/protobuf` composer package. Add this to your
223 `composer.json` file:
224
225 ```json
226 "require": {
227 "google/protobuf": "~v3.12.2"
228 }
229 ```
230
231 ### Generate PHP classes from your service definition
232
233 With all the above done, now you can define your message and service definition
234 in a `.proto` file and generate the corresponding PHP classes, which you can
235 import into your project, with a command similar to the following:
236
237 ```
238 $ protoc -I=. echo.proto --php_out=. --grpc_out=. \
239 --plugin=protoc-gen-grpc=<path to grpc_php_plugin>
240 ```
241
242 ## Unit Tests
243
244 You will need the source code to run tests
245
246 ```sh
247 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
248 $ cd grpc
249 $ git submodule update --init
250 ```
251
252 Run unit tests
253
254 ```sh
255 $ cd grpc/src/php
256 $ ./bin/run_tests.sh
257 ```
258
259 ## Generated Code Tests
260
261 This section specifies the prerequisites for running the generated code tests,
262 as well as how to run the tests themselves.
263
264 ### Composer
265
266 Install the runtime dependencies via `composer install`.
267
268 ```sh
269 $ cd grpc/src/php
270 $ composer install
271 ```
272
273
274 ### Client Stub
275
276 The generate client stub classes have already been generated from `.proto` files
277 by the `./bin/generate_proto_php.sh` script.
278
279 ### Run test server
280
281 Run a local server serving the `Math`
282 [service](https://github.com/grpc/grpc/blob/master/src/proto/math/math.proto#L42).
283
284 ```sh
285 $ cd grpc/src/php/tests/generated_code
286 $ npm install
287 $ node math_server.js
288 ```
289
290 ### Run test client
291
292 Run the generated code tests
293
294 ```sh
295 $ cd grpc/src/php
296 $ ./bin/run_gen_code_test.sh
297 ```
298
299 ## Apache, PHP-FPM and Nginx
300
301 For more information on how you can run the `grpc` library with Apache,
302 PHP-FPM and Nginx, you can check out
303 [this guide](https://github.com/grpc/grpc/tree/master/examples/php/echo).
304 There you will find a series of Docker images where you can quickly run an
305 end-to-end example.
306
307 ## Misc Config Options
308
309 ### SSL credentials
310
311 Here's how you can specify SSL credentials when creating your PHP client:
312
313 ```php
314 $client = new Helloworld\GreeterClient('localhost:50051', [
315 'credentials' => Grpc\ChannelCredentials::createSsl(
316 file_get_contents('<path to certificate>'))
317 ]);
318 ```
319
320 ### pcntl_fork() support
321
322 To make sure the `grpc` extension works with `pcntl_fork()` and related
323 functions, add the following lines to your `php.ini` file:
324
325 ```
326 grpc.enable_fork_support = 1
327 grpc.poll_strategy = epoll1
328 ```
329
330 ### Tracing and Logging
331
332 To turn on gRPC tracing, add the following lines to your `php.ini` file. For
333 all possible values of the `grpc.grpc.trace` option, please check
334 [this doc](https://github.com/grpc/grpc/blob/master/doc/environment_variables.md).
335
336 ```
337 grpc.grpc_verbosity=debug
338 grpc.grpc_trace=all,-polling,-polling_api,-pollable_refcount,-timer,-timer_check
339 grpc.log_filename=/var/log/grpc.log
340 ```
341
342 > Make sure the log file above is writable, by doing the following:
343 > ```
344 > $ sudo touch /var/log/grpc.log
345 > $ sudo chmod 666 /var/log/grpc.log
346 > ```
347 > Note: The log file does grow pretty quickly depending on how much logs are
348 > being printed out. Make sure you have other mechanisms (perhaps another
349 > cronjob) to zero out the log file from time to time,
350 > e.g. `cp /dev/null /var/log/grpc.log`, or turn these off when logs or tracing
351 > are not necessary for debugging purposes.
352
353 ### User agent string
354
355 You can customize the user agent string for your gRPC PHP client by specifying
356 this `grpc.primary_user_agent` option when constructing your PHP client:
357
358 ```php
359 $client = new Helloworld\GreeterClient('localhost:50051', [
360 'credentials' => Grpc\ChannelCredentials::createInsecure(),
361 'grpc.primary_user_agent' => 'my-user-agent-identifier',
362 ]);
363 ```
364
365 ### Maximum message size
366
367 To change the default maximum message size, specify this
368 `grpc.max_receive_message_length` option when constructing your PHP client:
369
370 ```php
371 $client = new Helloworld\GreeterClient('localhost:50051', [
372 'credentials' => Grpc\ChannelCredentials::createInsecure(),
373 'grpc.max_receive_message_length' => 8*1024*1024,
374 ]);
375 ```
376
377 ### Compression
378
379 You can customize the compression behavior on the client side, by specifying the following options when constructing your PHP client.
380
381 ```php
382 $client = new Helloworld\GreeterClient('localhost:50051', [
383 'credentials' => Grpc\ChannelCredentials::createInsecure(),
384 'grpc.default_compression_algorithm' => 2,
385 'grpc.default_compression_level' => 2,
386 ]);
387 ```
388
389 Possible values for `grpc.default_compression_algorithm`:
390
391 ```
392 0: No compression
393 1: Compress with DEFLATE algorithm
394 2: Compress with GZIP algorithm
395 3: Stream compression with GZIP algorithm
396 ```
397
398 Possible values for `grpc.default_compression_level`:
399
400 ```
401 0: None
402 1: Low level
403 2: Medium level
404 3: High level
405 ```