Skip to content

Commit c4da994

Browse files
pattishinbshafferyash30201JulienBreuxgrayside
authored
feat: initial commit of run mc php nginx sample (#1901)
* feat: initial commit of run mc php nginx sample * chore: upgrade texttospeech samples to new surface (#1880) * Revert "chore: upgrade texttospeech samples to new surface (#1880)" (#1902) This reverts commit 18c6872. * test: update php mc test and check deployment logs * test: updating test and clean up * chore: clean up * refactor: rename test * Update run/multi-container/hello-php-nginx-sample/nginx/nginx.conf Co-authored-by: Julien Breux <[email protected]> * Update run/multi-container/hello-php-nginx-sample/Makefile Co-authored-by: Julien Breux <[email protected]> * fix: flip container deps * Update run/multi-container/hello-php-nginx-sample/php-app/Dockerfile Co-authored-by: Adam Ross <[email protected]> * Update run/multi-container/hello-php-nginx-sample/php-app/opcache.ini Co-authored-by: Adam Ross <[email protected]> * Update run/multi-container/hello-php-nginx-sample/README.md Co-authored-by: Adam Ross <[email protected]> * refactor: updating with suggestions (updating readme, Cloud Run memory limit updates, rm makefile, add composer) * fix: resolve snippet-bot failures about lack of product name * refactor: update .ini and readme with concurrency/memory info * chore: clean up readme * refactor: updating comment about dockerfile context --------- Co-authored-by: Brent Shaffer <[email protected]> Co-authored-by: Yash Sahu <[email protected]> Co-authored-by: Julien Breux <[email protected]> Co-authored-by: Adam Ross <[email protected]>
1 parent 3438bdb commit c4da994

File tree

12 files changed

+545
-0
lines changed

12 files changed

+545
-0
lines changed

run/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| --------------------------------------- | ------------------------ | ------------- |
1111
|[Hello World][helloworld] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_helloworld] |
1212
|[Laravel][laravel] | Deploy Laravel on Cloud Run | -|
13+
|[Multi-container][multicontainer] | Multi-container samples (i.e nginx) | -|
1314

1415

1516
For more Cloud Run samples beyond PHP, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
@@ -90,4 +91,5 @@ for more information.
9091
[run_deploy]: https://cloud.google.com/run/docs/deploying
9192
[helloworld]: helloworld/
9293
[laravel]: laravel/
94+
[multicontainer]: multi-container/
9395
[run_button_helloworld]: https://deploy.cloud.run/?dir=run/helloworld
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Deploy simple nginx multi-container service NGINX/PHP-FPM
2+
3+
A Google Cloud Project is required in order to run the sample.
4+
5+
## Enable required APIs
6+
7+
The project should have the following API's enabled:
8+
9+
* Cloud Run
10+
* Artifact Registry
11+
12+
```bash
13+
gcloud services enable run.googleapis.com artifactregistry.googleapis.com
14+
```
15+
16+
## Getting started
17+
18+
Declare the following environment variables.
19+
```bash
20+
# References your Google Cloud Project
21+
export PROJECT_ID=<your-project-id>
22+
# References your Artifact Registry repo name
23+
export REPO_NAME="default"
24+
# References your resource location
25+
export REGION="us-west1"
26+
# References final Cloud Run multi-contaienr service name
27+
export MC_SERVICE_NAME="mc-php-example"
28+
```
29+
30+
1. Authenticate in [gcloud cli](https://cloud.google.com/sdk/gcloud).
31+
32+
```bash
33+
gcloud auth login
34+
```
35+
36+
2. Create a repository within [Artifact Registry](https://cloud.google.com/artifact-registry).
37+
38+
```bash
39+
gcloud artifacts repositories create ${REPO_NAME} --repository-format=docker
40+
```
41+
42+
3. Build the `nginx` and `hellophp` container images for our multi-container service.
43+
44+
```bash
45+
# Creating image from the Dockerfile within nginx/ dir.
46+
gcloud builds submit --tag=${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/nginx ./nginx
47+
48+
# Creating image from the Dockerfile within php-app/ dir.
49+
gcloud builds submit --tag=${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/php ./php-app
50+
```
51+
52+
4. Configure the service with the appropriate memory limit.
53+
54+
You will see as you read through `service.yaml`, that the memory limit has been explicitly
55+
set to `320Mi` due to container concurrency of `1` with a single `fpm` worker.
56+
See how we got [here](https://cloud.google.com/run/docs/configuring/services/memory-limits#optimizing) and read more about how to [optimize for concurrency](https://cloud.google.com/run/docs/tips/general#optimize_concurrency).
57+
58+
5. Deploy the multi-container service.
59+
60+
From within `service.yaml`, customize the `service.yaml` file with your own project values by replacing
61+
the following `PROJECT_ID`, `MC_SERVICE_NAME`, `REGION`, and `REPO_NAME`.
62+
63+
Once you've replaced the values, you can deploy from root directory (`hello-php-nginx-sample/`).
64+
65+
```sh
66+
gcloud run services replace service.yaml
67+
```
68+
69+
By default, the above command will deploy the following containers into a single service:
70+
71+
* `nginx`: `serving` ingress container (entrypoint)
72+
* `hellophp`: `application` container
73+
74+
The Cloud Run Multi-container service will default access to port `8080`,
75+
where `nginx` container will be listening and proxy request over to `hellophp` container at port `9000`.
76+
77+
Verify by using curl to send an authenticated request:
78+
79+
```bash
80+
curl --header "Authorization: Bearer $(gcloud auth print-identity-token)" <cloud-run-mc-service-url>
81+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-run": "^0.6.0"
4+
}
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudrun_hello_mc_nginx_dockerfile]
16+
17+
# Context: ./
18+
# Read more about context: https://docs.docker.com/build/building/context/
19+
# We assume here that required file paths are getting copied
20+
# from the perspective of this directory.
21+
22+
FROM nginx:1-alpine
23+
24+
COPY ./nginx.conf /etc/nginx/conf.d
25+
26+
COPY index.php /var/www/html/index.php
27+
28+
# [END cloudrun_hello_mc_nginx_dockerfile]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
<?php
16+
// [START cloudrun_hello_mc_nginx_app_serving_index]
17+
echo "This is nginx. Let's start processing requests!";
18+
19+
// [END cloudrun_hello_mc_nginx_app_serving_index]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudrun_hello_mc_nginx_serving_conf]
16+
server {
17+
# Server at localhost
18+
server_name _;
19+
# Listen at Cloud Run default port 8080 (https://cloud.google.com/run/docs/configuring/services/containers#configure-port)
20+
listen 8080;
21+
# Defining root dir for server
22+
root /var/www/html;
23+
24+
# The index directive searches within the local filesystem for
25+
# index files and does a redirect to `/var/www/html/index.php`.
26+
location / {
27+
index index.php;
28+
}
29+
30+
# The above request ends up here and is passed over to
31+
# FastCGI. As this FastCGI server is a remote one and nginx
32+
# does not have access to its filesystem, we explicity declare `fastcgi_index`.
33+
# This will map the value to relevant directories to complete the path.
34+
location ~ \.php$ {
35+
# Include the fastcgi_param settings
36+
include fastcgi_params;
37+
38+
# SCRIPT_FILENAME is used for PHP FPM (FastCGI process manager) determining the script name
39+
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
40+
fastcgi_index index.php;
41+
42+
# Instructs nginx to use CGI script using FastCGI protocol
43+
# This should reference unix socket path
44+
# if your project uses php-fpm socket this will be changed to:
45+
# fastcgi_pass unix:/var/run/php-fpm.sock
46+
fastcgi_pass 127.0.0.1:9000;
47+
}
48+
}
49+
# [END cloudrun_hello_mc_nginx_serving_conf]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudrun_hello_mc_nginx_app_dockerfile]
16+
17+
# Context: ../
18+
19+
# Read more about context: https://docs.docker.com/build/building/context/
20+
# We assume here that required file paths are getting copied
21+
# from the perspective of this directory's parent.
22+
23+
FROM php:8-fpm-alpine
24+
25+
# Use the default production configuration
26+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
27+
28+
RUN docker-php-ext-install opcache
29+
30+
# COPY php.ini /usr/local/etc/php
31+
COPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini
32+
33+
COPY . /var/www/html/
34+
35+
# [END cloudrun_hello_mc_nginx_app_dockerfile]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
<?php
16+
// [START cloudrun_hello_mc_nginx_index]
17+
18+
echo 'This is main php app. Hello PHP World!';
19+
20+
phpinfo();
21+
22+
// [END cloudrun_hello_mc_nginx_index]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; Configure Cloud Run memory
2+
memory_limit = -1
3+
4+
; Configure Opcache for containers
5+
[opcache]
6+
opcache.enable=1
7+
opcache.validate_timestamps=0
8+
opcache.max_accelerated_files=10000
9+
opcache.memory_consumption=192
10+
opcache.max_wasted_percentage=10
11+
opcache.interned_strings_buffer=16
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2023 Google LLC
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../../../testing/bootstrap.php" convertWarningsToExceptions="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">./php-app</directory>
18+
<directory suffix=".php">./nginx</directory>
19+
</include>
20+
<exclude>
21+
<directory>./vendor</directory>
22+
</exclude>
23+
<report>
24+
<clover outputFile="build/logs/clover.xml"/>
25+
</report>
26+
</coverage>
27+
<testsuites>
28+
<testsuite name="Cloud Run MC Hello PHP Nginx tests">
29+
<directory>test</directory>
30+
</testsuite>
31+
</testsuites>
32+
<logging/>
33+
<php>
34+
<env name="APP_ENV" value="testing"/>
35+
</php>
36+
</phpunit>

0 commit comments

Comments
 (0)