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