|
| 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 |
0 commit comments