Jayant Kolhe | 300748e | 2015-02-19 11:30:40 -0800 | [diff] [blame] | 1 | |
| 2 | #Overview |
| 3 | |
| 4 | This directory contains source code for PHP implementation of gRPC layered on shared C library. |
| 5 | |
| 6 | #Status |
| 7 | |
Stanley Cheung | 86bfc74 | 2015-09-22 10:56:54 -0700 | [diff] [blame] | 8 | Beta |
Jayant Kolhe | 300748e | 2015-02-19 11:30:40 -0800 | [diff] [blame] | 9 | |
Stanley Cheung | dbeb1cd | 2015-08-19 08:20:06 -0700 | [diff] [blame] | 10 | ## Environment |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 11 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 12 | Prerequisite: `php` >=5.5, `phpize`, `pecl`, `phpunit` |
Stanley Cheung | 76ed0cc | 2015-06-25 15:38:59 -0700 | [diff] [blame] | 13 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 14 | **Linux (Debian):** |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 15 | |
Stanley Cheung | cb14eab | 2015-06-16 17:33:34 -0700 | [diff] [blame] | 16 | ```sh |
Stanley Cheung | 75457ff | 2015-10-20 16:27:55 -0700 | [diff] [blame] | 17 | $ sudo apt-get install php5 php5-dev php-pear |
Stanley Cheung | 76ed0cc | 2015-06-25 15:38:59 -0700 | [diff] [blame] | 18 | ``` |
| 19 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 20 | **Linux (CentOS):** |
| 21 | |
| 22 | ```sh |
| 23 | $ yum install php55w |
| 24 | $ yum --enablerepo=remi,remi-php55 install php-devel php-pear |
| 25 | ``` |
| 26 | |
Stanley Cheung | dbeb1cd | 2015-08-19 08:20:06 -0700 | [diff] [blame] | 27 | **Mac OS X:** |
Stanley Cheung | 76ed0cc | 2015-06-25 15:38:59 -0700 | [diff] [blame] | 28 | |
| 29 | ```sh |
Stanley Cheung | 75457ff | 2015-10-20 16:27:55 -0700 | [diff] [blame] | 30 | $ curl -O http://pear.php.net/go-pear.phar |
| 31 | $ sudo php -d detect_unicode=0 go-pear.phar |
| 32 | ``` |
| 33 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 34 | **PHPUnit:** |
Stanley Cheung | 75457ff | 2015-10-20 16:27:55 -0700 | [diff] [blame] | 35 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 36 | $ wget https://phar.phpunit.de/phpunit-old.phar |
| 37 | $ chmod +x phpunit-old.phar |
| 38 | $ sudo mv phpunit-old.phar /usr/bin/phpunit |
Stanley Cheung | cb14eab | 2015-06-16 17:33:34 -0700 | [diff] [blame] | 39 | ``` |
murgatroid99 | 975a07b | 2015-02-02 14:10:42 -0800 | [diff] [blame] | 40 | |
Stanley Cheung | dbeb1cd | 2015-08-19 08:20:06 -0700 | [diff] [blame] | 41 | ## Quick Install |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 42 | |
Stanley Cheung | dbeb1cd | 2015-08-19 08:20:06 -0700 | [diff] [blame] | 43 | Install the gRPC PHP extension |
| 44 | |
| 45 | ```sh |
Stanley Cheung | 86bfc74 | 2015-09-22 10:56:54 -0700 | [diff] [blame] | 46 | sudo pecl install grpc-beta |
Stanley Cheung | dbeb1cd | 2015-08-19 08:20:06 -0700 | [diff] [blame] | 47 | ``` |
| 48 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 49 | This 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 Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 51 | To 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 Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 52 | |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 53 | ## Build from Source |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 54 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 55 | |
| 56 | ### gRPC C core library |
| 57 | |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 58 | Clone this repository |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 59 | |
Stanley Cheung | 7b773e9 | 2015-08-28 09:53:16 -0700 | [diff] [blame] | 60 | ```sh |
David Garcia Quintas | 4281272 | 2016-06-29 17:39:07 -0700 | [diff] [blame] | 61 | $ git clone -b $(curl -L http://grpc.io/release) https://github.com/grpc/grpc |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 62 | ``` |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 63 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 64 | Build and install the gRPC C core library |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 65 | |
| 66 | ```sh |
| 67 | $ cd grpc |
Stanley Cheung | 7b773e9 | 2015-08-28 09:53:16 -0700 | [diff] [blame] | 68 | $ git pull --recurse-submodules && git submodule update --init --recursive |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 69 | $ make |
| 70 | $ sudo make install |
| 71 | ``` |
| 72 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 73 | ### gRPC PHP extension |
Stanley Cheung | 7b773e9 | 2015-08-28 09:53:16 -0700 | [diff] [blame] | 74 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 75 | Install the gRPC PHP extension from PECL |
Stanley Cheung | cb14eab | 2015-06-16 17:33:34 -0700 | [diff] [blame] | 76 | |
| 77 | ```sh |
Stanley Cheung | 86bfc74 | 2015-09-22 10:56:54 -0700 | [diff] [blame] | 78 | $ sudo pecl install grpc-beta |
Stanley Cheung | cb14eab | 2015-06-16 17:33:34 -0700 | [diff] [blame] | 79 | ``` |
| 80 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 81 | Or, compile from source |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 82 | |
| 83 | ```sh |
| 84 | $ cd grpc/src/php/ext/grpc |
| 85 | $ phpize |
| 86 | $ ./configure |
| 87 | $ make |
| 88 | $ sudo make install |
| 89 | ``` |
| 90 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 91 | ### Update php.ini |
| 92 | |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 93 | Add this line to your `php.ini` file, e.g. `/etc/php5/cli/php.ini` |
| 94 | |
| 95 | ```sh |
| 96 | extension=grpc.so |
| 97 | ``` |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 98 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 99 | ## Unit Tests |
| 100 | |
| 101 | You will need the source code to run tests |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 102 | |
| 103 | ```sh |
David Garcia Quintas | 4281272 | 2016-06-29 17:39:07 -0700 | [diff] [blame] | 104 | $ git clone -b $(curl -L http://grpc.io/release) https://github.com/grpc/grpc |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 105 | $ cd grpc |
| 106 | $ git pull --recurse-submodules && git submodule update --init --recursive |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 107 | ``` |
| 108 | |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 109 | Run unit tests |
| 110 | |
| 111 | ```sh |
| 112 | $ cd grpc/src/php |
| 113 | $ ./bin/run_tests.sh |
| 114 | ``` |
| 115 | |
| 116 | ## Generated Code Tests |
| 117 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 118 | This section specifies the prerequisites for running the generated code tests, as well as how to run the tests themselves. |
| 119 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 120 | ### Composer |
| 121 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 122 | If you don't have it already, install `composer` to pull in some runtime dependencies based on the `composer.json` file. |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 123 | |
| 124 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 125 | $ 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 Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 134 | Again if you don't have it already, you need to install the protobuf compiler `protoc`, version 3.0.0+. |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 135 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 136 | If 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 Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 137 | |
| 138 | ```sh |
| 139 | $ cd grpc/third_party/protobuf |
| 140 | $ sudo make install # 'make' should have been run by core grpc |
| 141 | ``` |
| 142 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 143 | Alternatively, you can download `protoc` binaries from [the protocol buffers Github repository](https://github.com/google/protobuf/releases). |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 144 | |
| 145 | |
| 146 | ### PHP protobuf compiler |
| 147 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 148 | You need to install `protoc-gen-php` to generate stub class `.php` files from service definition `.proto` files. |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 149 | |
| 150 | ```sh |
| 151 | $ cd grpc/src/php/vendor/datto/protobuf-php # if you had run `composer install` in the previous step |
| 152 | |
| 153 | OR |
| 154 | |
| 155 | $ git clone https://github.com/stanley-cheung/Protobuf-PHP # clone from github repo |
| 156 | |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 157 | $ gem install rake ronn |
| 158 | $ rake pear:package version=1.0 |
| 159 | $ sudo pear install Protobuf-1.0.tgz |
| 160 | ``` |
| 161 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 162 | ### Client Stub |
| 163 | |
| 164 | Generate client stub classes from `.proto` files |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 165 | |
| 166 | ```sh |
| 167 | $ cd grpc/src/php |
| 168 | $ ./bin/generate_proto_php.sh |
| 169 | ``` |
| 170 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 171 | ### Run test server |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 172 | |
Stanley Cheung | a2c8b20 | 2016-03-08 08:21:56 -0800 | [diff] [blame] | 173 | Run a local server serving the math services. Please see [Node][] for how to run an example server. |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 174 | |
| 175 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 176 | $ cd grpc |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 177 | $ npm install |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 178 | $ nodejs src/node/test/math/math_server.js |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 179 | ``` |
| 180 | |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 181 | ### Run test client |
| 182 | |
Stanley Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 183 | Run the generated code tests |
| 184 | |
| 185 | ```sh |
| 186 | $ cd grpc/src/php |
| 187 | $ ./bin/run_gen_code_test.sh |
| 188 | ``` |
| 189 | |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 190 | ## Use the gRPC PHP extension with Apache |
| 191 | |
| 192 | Install `apache2`, in addition to `php5` above |
| 193 | |
| 194 | ```sh |
| 195 | $ sudo apt-get install apache2 |
| 196 | ``` |
| 197 | |
| 198 | Add this line to your `php.ini` file, e.g. `/etc/php5/apache2/php.ini` |
| 199 | |
| 200 | ```sh |
| 201 | extension=grpc.so |
| 202 | ``` |
| 203 | |
| 204 | Restart apache |
| 205 | |
| 206 | ```sh |
| 207 | $ sudo service apache2 restart |
| 208 | ``` |
| 209 | |
| 210 | Make sure the Node math server is still running, as above. |
| 211 | |
| 212 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 213 | $ cd grpc |
| 214 | $ npm install |
| 215 | $ nodejs src/node/test/math/math_server.js |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 216 | ``` |
| 217 | |
| 218 | Make sure you have run `composer install` to generate the `vendor/autoload.php` file |
| 219 | |
| 220 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 221 | $ cd grpc/src/php |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 222 | $ composer install |
| 223 | ``` |
| 224 | |
| 225 | Make sure you have generated the client stub `math.php` |
| 226 | |
| 227 | ```sh |
| 228 | $ ./bin/generate_proto_php.sh |
| 229 | ``` |
| 230 | |
| 231 | Copy 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 | |
| 237 | You may have to fix the first two lines to point the includes to your installation: |
| 238 | |
| 239 | ```php |
| 240 | include 'vendor/autoload.php'; |
| 241 | include 'tests/generated_code/math.php'; |
| 242 | ``` |
| 243 | |
| 244 | Connect 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 | |
| 252 | Install `nginx` and `php5-fpm`, in addition to `php5` above |
| 253 | |
| 254 | ```sh |
| 255 | $ sudo apt-get install nginx php5-fpm |
| 256 | ``` |
| 257 | |
| 258 | Add this line to your `php.ini` file, e.g. `/etc/php5/fpm/php.ini` |
| 259 | |
| 260 | ```sh |
| 261 | extension=grpc.so |
| 262 | ``` |
| 263 | |
| 264 | Uncomment the following lines in your `/etc/nginx/sites-available/default` file: |
| 265 | |
| 266 | ``` |
| 267 | location ~ \.php$ { |
| 268 | include snippets/fastcgi-php.conf; |
| 269 | fastcgi_pass unix:/var/run/php5-fpm.sock; |
| 270 | } |
| 271 | ``` |
| 272 | |
| 273 | Restart nginx and php-fpm |
| 274 | |
| 275 | ```sh |
| 276 | $ sudo service nginx restart |
| 277 | $ sudo service php5-fpm restart |
| 278 | ``` |
| 279 | |
| 280 | Make sure the Node math server is still running, as above. |
| 281 | |
| 282 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 283 | $ cd grpc |
| 284 | $ npm install |
| 285 | $ nodejs src/node/test/math/math_server.js |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 286 | ``` |
| 287 | |
| 288 | Make sure you have run `composer install` to generate the `vendor/autoload.php` file |
| 289 | |
| 290 | ```sh |
Stanley Cheung | a41a677 | 2016-03-07 17:10:07 -0800 | [diff] [blame] | 291 | $ cd grpc/src/php |
Stanley Cheung | e4c4738 | 2015-09-21 16:28:59 -0700 | [diff] [blame] | 292 | $ composer install |
| 293 | ``` |
| 294 | |
| 295 | Make sure you have generated the client stub `math.php` |
| 296 | |
| 297 | ```sh |
| 298 | $ ./bin/generate_proto_php.sh |
| 299 | ``` |
| 300 | |
| 301 | Copy 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 | |
| 307 | You may have to fix the first two lines to point the includes to your installation: |
| 308 | |
| 309 | ```php |
| 310 | include 'vendor/autoload.php'; |
| 311 | include 'tests/generated_code/math.php'; |
| 312 | ``` |
| 313 | |
| 314 | Connect 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 Cheung | 3fa51a3 | 2015-06-09 16:16:42 -0700 | [diff] [blame] | 320 | [Node]:https://github.com/grpc/grpc/tree/master/src/node/examples |