Laravel is an open source web framework for PHP developers that encourages the use of the model-view-controller (MVC) pattern.
You can check out PHP on Google Cloud Platform (GCP) to get an overview of PHP and learn ways to run PHP apps on GCP.
- Create a project in the Google Cloud Platform Console.
- Enable billing for your project.
- Install the Google Cloud SDK.
Follow the official documentation for installing Laravel from
laravel.com. This version was tested to work with laravel/laravel-framework:^5.6.
-
Run the app with the following command:
php artisan serve -
Visit http://localhost:8000 to see the Laravel Welcome page.
-
Create an
app.yamlfile with the following contents:runtime: php72 env_variables: # Put production environment variables here. APP_LOG: errorlog APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp -
Copy the
bootstrap/app.phpandconfig/view.phpfiles included in this sample into the corresponding directories of your Laravel application. These two files ensure your Laravel application writes to/tmpfor caching in production.
If you are using an existing Laravel application, just copy the
google-app-engine-deploymentblocks from these files.
-
Replace
YOUR_APP_KEYinapp.yamlwith an application key you generate with the following command:php artisan key:generate --showIf you're on Linux or macOS, the following command will automatically update your
app.yaml:sed -i '' "s#YOUR_APP_KEY#$(php artisan key:generate --show --no-ansi)#" app.yaml -
Run the following command to deploy your app:
gcloud app deploy -
Visit
http://YOUR_PROJECT_ID.appspot.comto see the Laravel welcome page. ReplaceYOUR_PROJECT_IDwith the ID of your GCP project.
Note: This section only works with Laravel 5.4.16. To use earlier versions of
Laravel, you need to manually add the DB_SOCKET value to
config/database.php (see #4178)
-
Follow the instructions to set up a Google Cloud SQL Second Generation instance for MySQL. Keep track of your instance name and password, as they will be used below.
-
Follow the instructions to install the Cloud SQL proxy client on your local machine. The Cloud SQL proxy is used to connect to your Cloud SQL instance when running locally.
-
Use the Google Cloud SDK from the command line to run the following command. Copy the
connectionNamevalue for the next step. ReplaceYOUR_INSTANCE_NAMEwith the name of your instance:gcloud sql instances describe YOUR_INSTANCE_NAME | grep connectionName -
Start the Cloud SQL proxy and replace
YOUR_CONNECTION_NAMEwith the connection name you retrieved in the previous step.cloud_sql_proxy -instances=YOUR_CONNECTION_NAME=tcp:3306 -
Use
gcloudto create a database for the application.gcloud sql databases create laravel --instance=YOUR_INSTANCE_NAME
-
-
Run the database migrations for Laravel. This can be done locally by setting your parameters in
.envor by passing them in as environment variables. Be sure to replaceYOUR_DB_PASSWORDbelow with the root password you configured:# create a migration for the session table php artisan session:table export DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD php artisan migrate --force -
Modify your
app.yamlfile with the following contents:runtime: php72 env_variables: # Put production environment variables here. APP_LOG: errorlog APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp CACHE_DRIVER: database SESSION_DRIVER: database ## Set these environment variables according to your CloudSQL configuration. DB_DATABASE: laravel DB_USERNAME: root DB_PASSWORD: YOUR_DB_PASSWORD DB_SOCKET: "/cloudsql/YOUR_CONNECTION_NAME" -
Replace each instance of
YOUR_DB_PASSWORDandYOUR_CONNECTION_NAMEwith the values you created for your Cloud SQL instance above.

