blob: 6cc1ba4d46264dfaa299b2ad61311cd74d3251dd [file] [log] [blame] [view]
Jayant Kolhe300748e2015-02-19 11:30:40 -08001
2#Overview
3
4This directory contains source code for PHP implementation of gRPC layered on shared C library.
5
6#Status
7
Stanley Cheung86bfc742015-09-22 10:56:54 -07008Beta
Jayant Kolhe300748e2015-02-19 11:30:40 -08009
Stanley Cheungdbeb1cd2015-08-19 08:20:06 -070010## Environment
mlumishb892a272014-12-09 16:28:23 -080011
Stanley Cheunga41a6772016-03-07 17:10:07 -080012Prerequisite: `php` >=5.5, `phpize`, `pecl`, `phpunit`
Stanley Cheung76ed0cc2015-06-25 15:38:59 -070013
Stanley Cheunga41a6772016-03-07 17:10:07 -080014**Linux (Debian):**
mlumishb892a272014-12-09 16:28:23 -080015
Stanley Cheungcb14eab2015-06-16 17:33:34 -070016```sh
Stanley Cheung75457ff2015-10-20 16:27:55 -070017$ sudo apt-get install php5 php5-dev php-pear
Stanley Cheung76ed0cc2015-06-25 15:38:59 -070018```
19
Stanley Cheunga41a6772016-03-07 17:10:07 -080020**Linux (CentOS):**
21
22```sh
23$ yum install php55w
24$ yum --enablerepo=remi,remi-php55 install php-devel php-pear
25```
26
Stanley Cheungdbeb1cd2015-08-19 08:20:06 -070027**Mac OS X:**
Stanley Cheung76ed0cc2015-06-25 15:38:59 -070028
29```sh
Stanley Cheung75457ff2015-10-20 16:27:55 -070030$ curl -O http://pear.php.net/go-pear.phar
31$ sudo php -d detect_unicode=0 go-pear.phar
32```
33
Stanley Cheunga41a6772016-03-07 17:10:07 -080034**PHPUnit:**
Stanley Cheung75457ff2015-10-20 16:27:55 -070035```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -080036$ wget https://phar.phpunit.de/phpunit-old.phar
37$ chmod +x phpunit-old.phar
38$ sudo mv phpunit-old.phar /usr/bin/phpunit
Stanley Cheungcb14eab2015-06-16 17:33:34 -070039```
murgatroid99975a07b2015-02-02 14:10:42 -080040
Stanley Cheungdbeb1cd2015-08-19 08:20:06 -070041## Quick Install
mlumishb892a272014-12-09 16:28:23 -080042
Stanley Cheungdbeb1cd2015-08-19 08:20:06 -070043Install the gRPC PHP extension
44
45```sh
Stanley Cheung86bfc742015-09-22 10:56:54 -070046sudo pecl install grpc-beta
Stanley Cheungdbeb1cd2015-08-19 08:20:06 -070047```
48
Stanley Cheunga41a6772016-03-07 17:10:07 -080049This will compile and install the gRPC PHP extension into the standard PHP extension directory. You should be able to run the [unit tests](#unit-tests), with the PHP extension installed.
50
Stanley Cheunga2c8b202016-03-08 08:21:56 -080051To run tests with generated stub code from `.proto` files, you will also need the `composer`, `protoc` and `protoc-gen-php` binaries. You can find out how to get these [below](#generated-code-tests).
Stanley Cheunga41a6772016-03-07 17:10:07 -080052
Stanley Cheung3fa51a32015-06-09 16:16:42 -070053## Build from Source
mlumishb892a272014-12-09 16:28:23 -080054
Stanley Cheunga41a6772016-03-07 17:10:07 -080055
56### gRPC C core library
57
Stanley Cheung3fa51a32015-06-09 16:16:42 -070058Clone this repository
mlumishb892a272014-12-09 16:28:23 -080059
Stanley Cheung7b773e92015-08-28 09:53:16 -070060```sh
David Garcia Quintas42812722016-06-29 17:39:07 -070061$ git clone -b $(curl -L http://grpc.io/release) https://github.com/grpc/grpc
Stanley Cheung3fa51a32015-06-09 16:16:42 -070062```
mlumishb892a272014-12-09 16:28:23 -080063
Stanley Cheunga41a6772016-03-07 17:10:07 -080064Build and install the gRPC C core library
Stanley Cheung3fa51a32015-06-09 16:16:42 -070065
66```sh
67$ cd grpc
Stanley Cheung7b773e92015-08-28 09:53:16 -070068$ git pull --recurse-submodules && git submodule update --init --recursive
Stanley Cheung3fa51a32015-06-09 16:16:42 -070069$ make
70$ sudo make install
71```
72
Stanley Cheunga41a6772016-03-07 17:10:07 -080073### gRPC PHP extension
Stanley Cheung7b773e92015-08-28 09:53:16 -070074
Stanley Cheunga41a6772016-03-07 17:10:07 -080075Install the gRPC PHP extension from PECL
Stanley Cheungcb14eab2015-06-16 17:33:34 -070076
77```sh
Stanley Cheung86bfc742015-09-22 10:56:54 -070078$ sudo pecl install grpc-beta
Stanley Cheungcb14eab2015-06-16 17:33:34 -070079```
80
Stanley Cheunga41a6772016-03-07 17:10:07 -080081Or, compile from source
Stanley Cheung3fa51a32015-06-09 16:16:42 -070082
83```sh
84$ cd grpc/src/php/ext/grpc
85$ phpize
86$ ./configure
87$ make
88$ sudo make install
89```
90
Stanley Cheunga41a6772016-03-07 17:10:07 -080091### Update php.ini
92
Stanley Cheunge4c47382015-09-21 16:28:59 -070093Add this line to your `php.ini` file, e.g. `/etc/php5/cli/php.ini`
94
95```sh
96extension=grpc.so
97```
Stanley Cheung3fa51a32015-06-09 16:16:42 -070098
Stanley Cheunga41a6772016-03-07 17:10:07 -080099## Unit Tests
100
101You will need the source code to run tests
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700102
103```sh
David Garcia Quintas42812722016-06-29 17:39:07 -0700104$ git clone -b $(curl -L http://grpc.io/release) https://github.com/grpc/grpc
Stanley Cheunga41a6772016-03-07 17:10:07 -0800105$ cd grpc
106$ git pull --recurse-submodules && git submodule update --init --recursive
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700107```
108
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700109Run unit tests
110
111```sh
112$ cd grpc/src/php
113$ ./bin/run_tests.sh
114```
115
116## Generated Code Tests
117
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800118This section specifies the prerequisites for running the generated code tests, as well as how to run the tests themselves.
119
Stanley Cheunga41a6772016-03-07 17:10:07 -0800120### Composer
121
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800122If you don't have it already, install `composer` to pull in some runtime dependencies based on the `composer.json` file.
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700123
124```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800125$ curl -sS https://getcomposer.org/installer | php
126$ sudo mv composer.phar /usr/local/bin/composer
127
128$ cd grpc/src/php
129$ composer install
130```
131
132### Protobuf compiler
133
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800134Again if you don't have it already, you need to install the protobuf compiler `protoc`, version 3.0.0+.
Stanley Cheunga41a6772016-03-07 17:10:07 -0800135
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800136If you compiled the gRPC C core library from source above, the `protoc` binary should have been installed as well. If it hasn't been installed, you can run the following commands to install it.
Stanley Cheunga41a6772016-03-07 17:10:07 -0800137
138```sh
139$ cd grpc/third_party/protobuf
140$ sudo make install # 'make' should have been run by core grpc
141```
142
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800143Alternatively, you can download `protoc` binaries from [the protocol buffers Github repository](https://github.com/google/protobuf/releases).
Stanley Cheunga41a6772016-03-07 17:10:07 -0800144
145
146### PHP protobuf compiler
147
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800148You need to install `protoc-gen-php` to generate stub class `.php` files from service definition `.proto` files.
Stanley Cheunga41a6772016-03-07 17:10:07 -0800149
150```sh
151$ cd grpc/src/php/vendor/datto/protobuf-php # if you had run `composer install` in the previous step
152
153OR
154
155$ git clone https://github.com/stanley-cheung/Protobuf-PHP # clone from github repo
156
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700157$ gem install rake ronn
158$ rake pear:package version=1.0
159$ sudo pear install Protobuf-1.0.tgz
160```
161
Stanley Cheunga41a6772016-03-07 17:10:07 -0800162### Client Stub
163
164Generate client stub classes from `.proto` files
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700165
166```sh
167$ cd grpc/src/php
168$ ./bin/generate_proto_php.sh
169```
170
Stanley Cheunga41a6772016-03-07 17:10:07 -0800171### Run test server
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700172
Stanley Cheunga2c8b202016-03-08 08:21:56 -0800173Run a local server serving the math services. Please see [Node][] for how to run an example server.
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700174
175```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800176$ cd grpc
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700177$ npm install
Stanley Cheunga41a6772016-03-07 17:10:07 -0800178$ nodejs src/node/test/math/math_server.js
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700179```
180
Stanley Cheunga41a6772016-03-07 17:10:07 -0800181### Run test client
182
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700183Run the generated code tests
184
185```sh
186$ cd grpc/src/php
187$ ./bin/run_gen_code_test.sh
188```
189
Stanley Cheunge4c47382015-09-21 16:28:59 -0700190## Use the gRPC PHP extension with Apache
191
192Install `apache2`, in addition to `php5` above
193
194```sh
195$ sudo apt-get install apache2
196```
197
198Add this line to your `php.ini` file, e.g. `/etc/php5/apache2/php.ini`
199
200```sh
201extension=grpc.so
202```
203
204Restart apache
205
206```sh
207$ sudo service apache2 restart
208```
209
210Make sure the Node math server is still running, as above.
211
212```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800213$ cd grpc
214$ npm install
215$ nodejs src/node/test/math/math_server.js
Stanley Cheunge4c47382015-09-21 16:28:59 -0700216```
217
218Make sure you have run `composer install` to generate the `vendor/autoload.php` file
219
220```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800221$ cd grpc/src/php
Stanley Cheunge4c47382015-09-21 16:28:59 -0700222$ composer install
223```
224
225Make sure you have generated the client stub `math.php`
226
227```sh
228$ ./bin/generate_proto_php.sh
229```
230
231Copy the `math_client.php` file into your Apache document root, e.g.
232
233```sh
234$ cp tests/generated_code/math_client.php /var/www/html
235```
236
237You may have to fix the first two lines to point the includes to your installation:
238
239```php
240include 'vendor/autoload.php';
241include 'tests/generated_code/math.php';
242```
243
244Connect to `localhost/math_client.php` in your browser, or run this from command line:
245
246```sh
247$ curl localhost/math_client.php
248```
249
250## Use the gRPC PHP extension with Nginx/PHP-FPM
251
252Install `nginx` and `php5-fpm`, in addition to `php5` above
253
254```sh
255$ sudo apt-get install nginx php5-fpm
256```
257
258Add this line to your `php.ini` file, e.g. `/etc/php5/fpm/php.ini`
259
260```sh
261extension=grpc.so
262```
263
264Uncomment the following lines in your `/etc/nginx/sites-available/default` file:
265
266```
267location ~ \.php$ {
268 include snippets/fastcgi-php.conf;
269 fastcgi_pass unix:/var/run/php5-fpm.sock;
270}
271```
272
273Restart nginx and php-fpm
274
275```sh
276$ sudo service nginx restart
277$ sudo service php5-fpm restart
278```
279
280Make sure the Node math server is still running, as above.
281
282```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800283$ cd grpc
284$ npm install
285$ nodejs src/node/test/math/math_server.js
Stanley Cheunge4c47382015-09-21 16:28:59 -0700286```
287
288Make sure you have run `composer install` to generate the `vendor/autoload.php` file
289
290```sh
Stanley Cheunga41a6772016-03-07 17:10:07 -0800291$ cd grpc/src/php
Stanley Cheunge4c47382015-09-21 16:28:59 -0700292$ composer install
293```
294
295Make sure you have generated the client stub `math.php`
296
297```sh
298$ ./bin/generate_proto_php.sh
299```
300
301Copy the `math_client.php` file into your Nginx document root, e.g.
302
303```sh
304$ cp tests/generated_code/math_client.php /var/www/html
305```
306
307You may have to fix the first two lines to point the includes to your installation:
308
309```php
310include 'vendor/autoload.php';
311include 'tests/generated_code/math.php';
312```
313
314Connect to `localhost/math_client.php` in your browser, or run this from command line:
315
316```sh
317$ curl localhost/math_client.php
318```
319
Stanley Cheung3fa51a32015-06-09 16:16:42 -0700320[Node]:https://github.com/grpc/grpc/tree/master/src/node/examples