Skip to content

Commit 4b6eb2b

Browse files
authored
Laravel on App Engine PHP 7.2 example (GoogleCloudPlatform#667)
1 parent 0f97485 commit 4b6eb2b

File tree

12 files changed

+1453
-0
lines changed

12 files changed

+1453
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
Laravel on App Engine for PHP 7.2
2+
=================================
3+
4+
[Laravel][laravel] is an open source web framework for PHP developers that encourages the use of the model-view-controller (MVC) pattern.
5+
6+
You can check out [PHP on Google Cloud Platform][php-gcp] (GCP) to get an
7+
overview of PHP and learn ways to run PHP apps on GCP.
8+
9+
## Prerequisites
10+
11+
1. Create a project in the [Google Cloud Platform Console](https://console.cloud.google.com/project).
12+
1. Enable billing for your project.
13+
1. Install the [Google Cloud SDK][cloud_sdk].
14+
15+
## Prepare
16+
17+
Follow the official documentation for [installing Laravel][laravel-install] from
18+
laravel.com. This version was tested to work with `laravel/laravel-framework:^5.6`.
19+
20+
## Run
21+
22+
1. Run the app with the following command:
23+
24+
php artisan serve
25+
26+
1. Visit [http://localhost:8000](http://localhost:8000) to see the Laravel
27+
Welcome page.
28+
29+
## Deploy
30+
31+
1. Create an `app.yaml` file with the following contents:
32+
33+
runtime: php72
34+
35+
env_variables:
36+
# Put production environment variables here.
37+
APP_LOG: errorlog
38+
APP_KEY: YOUR_APP_KEY
39+
APP_STORAGE: /tmp
40+
41+
1. Copy the [`bootstrap/app.php`](bootstrap/app.php) and
42+
[`config/view.php`](config/view.php) files included in this sample into the
43+
corresponding directories of your Laravel application. These two files ensure
44+
your Laravel application writes to `/tmp` for caching in production.
45+
46+
> If you are using an existing Laravel application, just copy the
47+
`google-app-engine-deployment` blocks from these files.
48+
49+
1. Replace `YOUR_APP_KEY` in `app.yaml` with an application key you generate
50+
with the following command:
51+
52+
php artisan key:generate --show
53+
54+
If you're on Linux or macOS, the following command will automatically
55+
update your `app.yaml`:
56+
57+
sed -i '' "s#YOUR_APP_KEY#$(php artisan key:generate --show --no-ansi)#" app.yaml
58+
59+
1. Run the following command to deploy your app:
60+
61+
gcloud app deploy
62+
63+
1. Visit `http://YOUR_PROJECT_ID.appspot.com` to see the Laravel welcome page. Replace `YOUR_PROJECT_ID`
64+
with the ID of your GCP project.
65+
66+
![Laravel welcome page][laravel-welcome]
67+
68+
## Set up Database Sessions
69+
70+
**Note**: This section only works with Laravel 5.4.16. To use earlier versions of
71+
Laravel, you need to manually add the `DB_SOCKET` value to
72+
`config/database.php` (see [#4178](https://github.com/laravel/laravel/pull/4179/files))
73+
74+
1. Follow the instructions to set up a
75+
[Google Cloud SQL Second Generation instance for MySQL][cloudsql-create].
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+
1. Use the [Google Cloud SDK][cloud_sdk] from the command line to run the following command. Copy
83+
the `connectionName` value for the next step. Replace `YOUR_INSTANCE_NAME` with the name
84+
of your instance:
85+
86+
gcloud sql instances describe YOUR_INSTANCE_NAME
87+
88+
1. Start the Cloud SQL proxy and replace `YOUR_INSTANCE_CONNECTION_NAME` with
89+
the connection name you retrieved in the previous step:
90+
91+
cloud_sql_proxy -instances=YOUR_INSTANCE_CONNECTION_NAME=tcp:3306
92+
93+
1. Use `gcloud` to create a database for the application.
94+
95+
gcloud sql databases create laravel --instance=YOUR_INSTANCE_NAME
96+
97+
1. Run the database migrations for Laravel. This can be done locally by setting
98+
your parameters in `.env` or by passing them in as environment variables. Be
99+
sure to replace `YOUR_DB_PASSWORD` below with the root password you
100+
configured:
101+
102+
# create a migration for the session table
103+
php artisan session:table
104+
DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD php artisan migrate --force
105+
106+
1. Modify your `app.yaml` file with the following contents:
107+
108+
runtime: php72
109+
110+
env_variables:
111+
# Put production environment variables here.
112+
APP_LOG: errorlog
113+
APP_KEY: YOUR_APP_KEY
114+
APP_STORAGE: /tmp
115+
CACHE_DRIVER: database
116+
SESSION_DRIVER: database
117+
## Set these environment variables according to your CloudSQL configuration.
118+
DB_DATABASE: laravel
119+
DB_USERNAME: root
120+
DB_PASSWORD: YOUR_DB_PASSWORD
121+
DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME"
122+
123+
1. Replace each instance of `YOUR_DB_PASSWORD` and `YOUR_CLOUDSQL_CONNECTION_NAME`
124+
with the values you created for your Cloud SQL instance above.
125+
126+
[php-gcp]: https://cloud.google.com/php
127+
[laravel]: http://laravel.com
128+
[laravel-install]: https://laravel.com/docs/5.4/installation
129+
[laravel-welcome]: https://storage.googleapis.com/gcp-community/tutorials/run-laravel-on-appengine-flexible/welcome-page.png
130+
[cloud_sdk]: https://cloud.google.com/sdk/
131+
[cloudsql-create]: https://cloud.google.com/sql/docs/mysql/create-instance
132+
[cloudsql-install]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install
133+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
runtime: php72
2+
3+
env_variables:
4+
# Put production environment variables here.
5+
APP_LOG: errorlog
6+
APP_KEY: YOUR_APP_KEY
7+
APP_STORAGE: /tmp
8+
CACHE_DRIVER: database
9+
SESSION_DRIVER: database
10+
## Set these environment variables according to your CloudSQL configuration.
11+
DB_DATABASE: YOUR_DB_DATABASE
12+
DB_USERNAME: YOUR_DB_USERNAME
13+
DB_PASSWORD: YOUR_DB_PASSWORD
14+
DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: php72
2+
3+
env_variables:
4+
# Put production environment variables here.
5+
LOG_CHANNEL: stackdriver
6+
APP_KEY: YOUR_APP_KEY
7+
APP_STORAGE: /tmp
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Create The Application
6+
|--------------------------------------------------------------------------
7+
|
8+
| The first thing we will do is create a new Laravel application instance
9+
| which serves as the "glue" for all the components of Laravel, and is
10+
| the IoC container for the system binding all of the various parts.
11+
|
12+
*/
13+
14+
$app = new Illuminate\Foundation\Application(
15+
realpath(__DIR__ . '/../')
16+
);
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Bind Important Interfaces
21+
|--------------------------------------------------------------------------
22+
|
23+
| Next, we need to bind some important interfaces into the container so
24+
| we will be able to resolve them when needed. The kernels serve the
25+
| incoming requests to this application from both the web and CLI.
26+
|
27+
*/
28+
29+
$app->singleton(
30+
Illuminate\Contracts\Http\Kernel::class,
31+
App\Http\Kernel::class
32+
);
33+
34+
$app->singleton(
35+
Illuminate\Contracts\Console\Kernel::class,
36+
App\Console\Kernel::class
37+
);
38+
39+
$app->singleton(
40+
Illuminate\Contracts\Debug\ExceptionHandler::class,
41+
App\Exceptions\Handler::class
42+
);
43+
44+
# [START google-app-engine-deployment]
45+
/*
46+
|--------------------------------------------------------------------------
47+
| Set Storage Path
48+
|--------------------------------------------------------------------------
49+
|
50+
| This script allows us to override the default storage location used by
51+
| the application. You may set the APP_STORAGE environment variable
52+
| in your .env file, if not set the default location will be used
53+
|
54+
*/
55+
56+
$app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));
57+
# [END google-app-engine-deployment]
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Return The Application
62+
|--------------------------------------------------------------------------
63+
|
64+
| This script returns the application instance. The instance is given to
65+
| the calling script so we can separate the building of the instances
66+
| from the actual running of the application and sending responses.
67+
|
68+
*/
69+
70+
return $app;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"require-dev": {
3+
"google/cloud-tools": "^0.8.1",
4+
"symfony\/process": "~2.8|~3.0",
5+
"monolog\/monolog": "^1.19"
6+
}
7+
}

0 commit comments

Comments
 (0)