|
1 | 1 | # Laravel Framework on App Engine Standard for PHP 7.2 |
2 | 2 |
|
3 | | -[Laravel][laravel] is an open source web framework for PHP developers that encourages the use of the model-view-controller (MVC) pattern. |
| 3 | +To run this sample, read the [Run Laravel on App Engine Standard][tutorial] tutorial. |
4 | 4 |
|
5 | | -You can check out [PHP on Google Cloud Platform][php-gcp] (GCP) to get an |
6 | | -overview of PHP and learn ways to run PHP apps on GCP. |
7 | | - |
8 | | -## Prerequisites |
9 | | - |
10 | | -1. Create a project in the [Google Cloud Platform Console](https://console.cloud.google.com/project). |
11 | | -1. Enable billing for your project. |
12 | | -1. Install the [Google Cloud SDK][cloud_sdk]. |
13 | | - |
14 | | -## Prepare |
15 | | - |
16 | | -Follow the official documentation for [installing Laravel][laravel-install] from |
17 | | -laravel.com. This version was tested to work with `laravel/laravel-framework:^5.6`. |
18 | | - |
19 | | -## Run |
20 | | - |
21 | | -1. Run the app with the following command: |
22 | | - |
23 | | - php artisan serve |
24 | | - |
25 | | -1. Visit [http://localhost:8000](http://localhost:8000) to see the Laravel |
26 | | - Welcome page. |
27 | | - |
28 | | -## Deploy |
29 | | - |
30 | | -1. Create an `app.yaml` file with the following contents: |
31 | | - |
32 | | - runtime: php72 |
33 | | - |
34 | | - env_variables: |
35 | | - # Put production environment variables here. |
36 | | - APP_KEY: YOUR_APP_KEY |
37 | | - APP_STORAGE: /tmp |
38 | | - VIEW_COMPILED_PATH: /tmp |
39 | | - |
40 | | -1. Copy the [`bootstrap/app.php`](bootstrap/app.php) file included in this sample |
41 | | - into the `bootstrap` directory of your Laravel application. This file ensures |
42 | | - your Laravel application writes to `/tmp` for caching in production. |
43 | | - |
44 | | - > If you are using an existing Laravel application, just copy the |
45 | | - `google-app-engine-deployment` block from this file. |
46 | | - |
47 | | -1. Replace `YOUR_APP_KEY` in `app.yaml` with an application key you generate |
48 | | - with the following command: |
49 | | - |
50 | | - php artisan key:generate --show |
51 | | - |
52 | | - If you're on Linux or macOS, the following command will automatically |
53 | | - update your `app.yaml`: |
54 | | - |
55 | | - sed -i '' "s#YOUR_APP_KEY#$(php artisan key:generate --show --no-ansi)#" app.yaml |
56 | | - |
57 | | -1. Run the following command to deploy your app: |
58 | | - |
59 | | - gcloud app deploy |
60 | | - |
61 | | -1. Visit `http://YOUR_PROJECT_ID.appspot.com` to see the Laravel welcome page. Replace `YOUR_PROJECT_ID` |
62 | | - with the ID of your GCP project. |
63 | | - |
64 | | - ![Laravel welcome page][laravel-welcome] |
65 | | - |
66 | | -## Set up Database Sessions with Cloud SQL |
67 | | - |
68 | | -**Note**: This section only works with Laravel 5.4.16 and above. To use earlier versions of |
69 | | -Laravel, you need to manually add the `DB_SOCKET` value to |
70 | | -`config/database.php` (see [#4178](https://github.com/laravel/laravel/pull/4179/files)) |
71 | | - |
72 | | -1. Follow the instructions to set up a |
73 | | - [Google Cloud SQL Second Generation instance for MySQL][cloudsql-create]. |
74 | | - Keep track of your instance name and password, as they |
75 | | - will be used below. |
76 | | - |
77 | | -1. Follow the instructions to |
78 | | - [install the Cloud SQL proxy client on your local machine][cloudsql-install]. |
79 | | - The Cloud SQL proxy is used to connect to your Cloud SQL instance when running |
80 | | - locally. |
81 | | - |
82 | | - * Use the [Google Cloud SDK][cloud_sdk] from the command line to run the following command. Copy the `connectionName` value for the next step. Replace `YOUR_INSTANCE_NAME` with the name of your instance: |
83 | | - |
84 | | - gcloud sql instances describe YOUR_INSTANCE_NAME | grep connectionName |
85 | | - |
86 | | - * Start the Cloud SQL proxy and replace `YOUR_CONNECTION_NAME` with the connection name you retrieved in the previous step. |
87 | | - |
88 | | - cloud_sql_proxy -instances=YOUR_CONNECTION_NAME=tcp:3306 |
89 | | - |
90 | | - * Use `gcloud` to create a database for the application. |
91 | | - |
92 | | - gcloud sql databases create laravel --instance=YOUR_INSTANCE_NAME |
93 | | - |
94 | | -1. Run the database migrations for Laravel. This can be done locally by setting |
95 | | - your parameters in `.env` or by passing them in as environment variables. Be |
96 | | - sure to replace `YOUR_DB_PASSWORD` below with the root password you |
97 | | - configured: |
98 | | - |
99 | | - # create a migration for the session table |
100 | | - php artisan session:table |
101 | | - export DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD |
102 | | - php artisan migrate --force |
103 | | - |
104 | | -1. Modify your `app.yaml` file with contents from [`app-dbsessions.yaml`](app-dbsessions.yaml): |
105 | | - |
106 | | - runtime: php72 |
107 | | - |
108 | | - env_variables: |
109 | | - # Put production environment variables here. |
110 | | - APP_KEY: YOUR_APP_KEY |
111 | | - APP_STORAGE: /tmp |
112 | | - VIEW_COMPILED_PATH: /tmp |
113 | | - CACHE_DRIVER: database |
114 | | - SESSION_DRIVER: database |
115 | | - ## Set these environment variables according to your CloudSQL configuration. |
116 | | - DB_DATABASE: laravel |
117 | | - DB_USERNAME: root |
118 | | - DB_PASSWORD: YOUR_DB_PASSWORD |
119 | | - DB_SOCKET: "/cloudsql/YOUR_CONNECTION_NAME" |
120 | | - |
121 | | -1. Replace each instance of `YOUR_DB_PASSWORD` and `YOUR_CONNECTION_NAME` |
122 | | - with the values you created for your Cloud SQL instance above. |
123 | | - |
124 | | -## Set up Stackdriver Logging and Error Reporting |
125 | | - |
126 | | -Before we begin, install both of the Google Cloud client libraries for Stackdriver |
127 | | -Logging and Error Reporting: |
128 | | - |
129 | | - composer require google/cloud-logging google/cloud-error-reporting |
130 | | - |
131 | | -### Stackdriver Logging |
132 | | - |
133 | | -You can write logs to Stackdriver Logging from PHP applications by using the Stackdriver Logging library for PHP directly. |
134 | | - |
135 | | -1. First, create a custom logger in `app/Logging/CreateStackdriverLogger.php`: |
136 | | - ```php |
137 | | - namespace App\Logging; |
138 | | - |
139 | | - use Google\Cloud\Logging\LoggingClient; |
140 | | - use Monolog\Handler\PsrHandler; |
141 | | - use Monolog\Logger; |
142 | | - |
143 | | - class CreateStackdriverLogger |
144 | | - { |
145 | | - /** |
146 | | - * Create a custom Monolog instance. |
147 | | - * |
148 | | - * @param array $config |
149 | | - * @return \Monolog\Logger |
150 | | - */ |
151 | | - public function __invoke(array $config) |
152 | | - { |
153 | | - $logName = isset($config['logName']) ? $config['logName'] : 'app'; |
154 | | - $psrLogger = LoggingClient::psrBatchLogger($logName); |
155 | | - $handler = new PsrHandler($psrLogger); |
156 | | - $logger = new Logger($logName, [$handler]); |
157 | | - return $logger; |
158 | | - } |
159 | | - } |
160 | | - ``` |
161 | | - |
162 | | -1. Next, you'll need to add our new custom logger to `config/logging.php`: |
163 | | - |
164 | | - ```php |
165 | | - 'channels' => [ |
166 | | - |
167 | | - // Add the following lines to integrate with Stackdriver: |
168 | | - 'stackdriver' => [ |
169 | | - 'driver' => 'custom', |
170 | | - 'via' => App\Logging\CreateStackdriverLogger::class, |
171 | | - 'level' => 'debug', |
172 | | - ], |
173 | | - ``` |
174 | | - |
175 | | -1. Finally, set the environment variable `LOG_CHANNEL` in `app.yaml` to |
176 | | - `stackdriver` to use the Stackdriver logger you created: |
177 | | - |
178 | | - ```yaml |
179 | | - runtime: php72 |
180 | | - |
181 | | - env_variables: |
182 | | - # Put production environment variables here. |
183 | | - LOG_CHANNEL: stackdriver |
184 | | - #... |
185 | | - ``` |
186 | | - |
187 | | -1. Now you can log to Stackdriver logging anywhere in your application! |
188 | | - |
189 | | - ```php |
190 | | - Log::info("Hello Stackdriver! This will show up as log level INFO!"); |
191 | | - ``` |
192 | | - |
193 | | -### Stackdriver Error Reporting |
194 | | - |
195 | | -You can send error reports to Stackdriver Error Reporting from PHP applications by using the |
196 | | -[Stackdriver Error Reporting library for PHP](http://googleapis.github.io/google-cloud-php/#/docs/cloud-error-reporting/v0.12.3/errorreporting/readme). |
197 | | - |
198 | | - |
199 | | -1. Add the following `use` statement at the beginning of the file `app/Exceptions/Handler.php`: |
200 | | - ```php |
201 | | - use Google\Cloud\ErrorReporting\Bootstrap; |
202 | | - ``` |
203 | | - |
204 | | -1. Edit the `report` function in the same file (`app/Exceptions/Handler.php`) as follows: |
205 | | - ```php |
206 | | - public function report(Exception $exception) |
207 | | - { |
208 | | - if (isset($_SERVER['GAE_SERVICE'])) { |
209 | | - Bootstrap::init(); |
210 | | - Bootstrap::exceptionHandler($exception); |
211 | | - } else { |
212 | | - parent::report($exception); |
213 | | - } |
214 | | - } |
215 | | - ``` |
216 | | - |
217 | | -1. Now any PHP Exception will be logged to Stackdriver Error Reporting! |
218 | | - ```php |
219 | | - throw new \Exception('PHEW! We will see this in Stackdriver Error Reporting!'); |
220 | | - ``` |
221 | | - |
222 | | -[php-gcp]: https://cloud.google.com/php |
223 | | -[laravel]: http://laravel.com |
224 | | -[laravel-install]: https://laravel.com/docs/5.4/installation |
225 | | -[laravel-welcome]: https://storage.googleapis.com/gcp-community/tutorials/run-laravel-on-appengine-flexible/welcome-page.png |
226 | | -[cloud_sdk]: https://cloud.google.com/sdk/ |
227 | | -[cloudsql-create]: https://cloud.google.com/sql/docs/mysql/create-instance |
228 | | -[cloudsql-install]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install |
| 5 | +[tutorial]: https://cloud.google.com/community/tutorials/run-laravel-on-appengine-standard |
0 commit comments