Skip to content

Commit 7f16b65

Browse files
authored
Adds laravel example (GoogleCloudPlatform#139)
1 parent 18fa6de commit 7f16b65

File tree

7 files changed

+2286
-0
lines changed

7 files changed

+2286
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Laravel on App Engine Flexible Environment
2+
==========================================
3+
4+
## Overview
5+
6+
This guide will help you deploy Laravel on [App Engine Flexible Environment][1]
7+
8+
## Prerequisites
9+
10+
Before setting up Laravel on App Engine, you will need to complete the following:
11+
12+
1. Create a project in the [Google Cloud console][2]. Note your **Project ID**, as you will need it
13+
later.
14+
15+
## Install Laravel
16+
17+
1. Use composer to download Laravel and its dependencies
18+
```sh
19+
composer create-project laravel/laravel
20+
```
21+
22+
1. cd laravel
23+
1. composer install
24+
1. php artisan key:generate
25+
26+
## Set up the Database
27+
28+
Laravel on App Engine Flexible uses a database for sessions and cache. This is
29+
to allow the cache and session to persist across instances.
30+
31+
1. Follow the instructions to set up a [CloudSQL Second Generation instance][3]
32+
33+
1. Follow the instructions to
34+
[install the Cloud SQL Proxy client on your local machine][4]. The Cloud SQL
35+
Proxy is used to connect to your Cloud SQL instance when running locally.
36+
37+
1. Use the Cloud SDK from command-line to run the following command. Copy the
38+
connectionName value for the next step.
39+
```sh
40+
gcloud beta sql instances describe [YOUR_INSTANCE_NAME]
41+
```
42+
43+
1. Start the Cloud SQL Proxy using the connection name from the previous step:
44+
```sh
45+
cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
46+
```
47+
48+
1. Use the MySQL client or similar program to connect to your instance and
49+
create a database for the application. When prompted, use the root password
50+
you configured:
51+
```sh
52+
mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE laravel;"
53+
```
54+
55+
1. Run the database migrations for Laravel. This can be done by setting your
56+
parameters in `.env` or by passing them in as environemnt variables:
57+
```sh
58+
# create a migration for the session table
59+
php artisan session:table
60+
DB_DATABASE=laravel \
61+
DB_USERNAME=root \
62+
DB_PASSWORD=supersecretpassword \
63+
php artisan migrate --force
64+
```
65+
1. Update `app.yaml` with the values for your database configuration.
66+
67+
1. Finally, edit `config/database.php` and add a line for "unix_socket" to the
68+
'mysql' connection configuration:
69+
```php
70+
'mysql' => [
71+
// ...
72+
'unix_socket' => env('DB_SOCKET', ''),
73+
```
74+
75+
76+
## Copy over App Engine's `app.yaml` File
77+
78+
For your app to deploy on App Engine Flexible, you will need to copy over
79+
`app.yaml`:
80+
81+
```sh
82+
# clone this repo somewhere
83+
git clone https://github.com/GoogleCloudPlatform/php-docs-samples /path/to/php-docs-samples
84+
85+
# copy the file below to the root directory of your Laravel project
86+
cp /path/to/php-docs-samples/appengine/flexible/laravel/app.yaml /path/to/laravel
87+
```
88+
89+
`app.yaml` contains production environemnt variables and App Engine
90+
configuration for your project.
91+
92+
## Add deploy commands to composer
93+
94+
Finally, you need to have scripts run after your application deploys. Add the
95+
following scripts to your project's composer.json:
96+
97+
```json
98+
{
99+
"scripts": {
100+
"post-deploy-cmd": [
101+
"chmod -R 755 bootstrap\/cache",
102+
"php artisan cache:clear"
103+
]
104+
}
105+
}
106+
```
107+
108+
[1]: https://cloud.google.com/appengine/docs/flexible/
109+
[2]: https://console.cloud.google.com
110+
[3]: https://cloud.google.com/sql/docs/create-instance
111+
[4]: https://cloud.google.com/sql/docs/external#install
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
runtime: php
2+
env: flex
3+
4+
runtime_config:
5+
document_root: public
6+
7+
env_variables:
8+
# The values here will override those in ".env". This is useful for
9+
# production-specific configuration. However, Feel free to set these values
10+
# in ".env" instead if you prefer.
11+
APP_LOG: errorlog
12+
CACHE_DRIVER: database
13+
SESSION_DRIVER: database
14+
STORAGE_DIR: /tmp
15+
## Set these environment variables according to your CloudSQL configuration.
16+
DB_HOST: localhost
17+
DB_DATABASE: YOUR_DB_DATABASE
18+
DB_USERNAME: YOUR_DB_USERNAME
19+
DB_PASSWORD: YOUR_DB_PASSWORD
20+
DB_SOCKET: /cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME
21+
22+
beta_settings:
23+
# for Cloud SQL, uncomment and set this value to the Cloud SQL
24+
# connection name, e.g.
25+
# "project:region:cloudsql-instance"
26+
cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"require-dev": {
3+
"symfony\/process": "~2.8|~3.0",
4+
"monolog\/monolog": "^1.19",
5+
"guzzlehttp\/guzzle": "^6.2",
6+
"google/cloud-tools": "^0.5",
7+
"paragonie/random_compat": " ^2.0"
8+
}
9+
}

0 commit comments

Comments
 (0)