Edit on GitHub
Report issue
Page history

Submitted by @{[ TutorialCtrl.tutorial.github_username ]} {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Run Laravel on Google App Engine Flexible Environment description: Learn how to deploy a Laravel app to Google App Engine flexible environment. author: bshaffer tags: App Engine, Laravel, PHP date_published: 2017-03-15


Laravel

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.

Prerequisites

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.

Prepare

Follow the official documentation for installing Laravel from laravel.com.

Run

  1. Run the app with the following command:

    php artisan serve
    
  2. Visit http://localhost:8000 to see the Laravel Welcome page.

Deploy

  1. Create an app.yaml file with the following contents:

    runtime: php
    env: flex
    
    runtime_config:
      document_root: public
    
    # Ensure we skip ".env", which is only for local development
    skip_files:
      - .env
    
    env_variables:
      # Put production environment variables here.
      APP_LOG: errorlog
      APP_KEY: YOUR_APP_KEY
      STORAGE_DIR: /tmp
    
  2. Replace YOUR_APP_KEY in app.yaml with an application key you generate with the following command:

    php artisan key:generate --show
    

    If 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
    
  3. Add the following under scripts in composer.json:

    "post-install-cmd": [
        "chmod -R 755 bootstrap\/cache",
        "php artisan cache:clear"
    ]
    

    In the context of Laravel's composer.json file, it will look like this:

    Add post-install-cmd scripts to composer.json

  4. Run the following command to deploy your app:

    gcloud app deploy
    
  5. Visit http://YOUR_PROJECT_ID.appspot.com to see the Laravel welcome page. Replace YOUR_PROJECT_ID with the ID of your GCP project.

    Laravel welcome page

Set up Database Sessions

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)

  1. Follow the instructions to set up a Google Cloud SQL Second Generation instance for MySQL.

  2. 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.

  3. Use the Cloud SDK from the command line to run the following command. Copy the connectionName value for the next step. Replace YOUR_INSTANCE_NAME with the name of your instance:

    gcloud sql instances describe YOUR_INSTANCE_NAME
    
  4. Start the Cloud SQL proxy and replace YOUR_INSTANCE_CONNECTION_NAME with the connection name you retrieved in the previous step:

    cloud_sql_proxy -instances=YOUR_INSTANCE_CONNECTION_NAME=tcp:3306
    
  5. Use the MySQL client, or a similar program, to connect to your instance and create a database for the application. When prompted, use the root password you configured.

    mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE laravel;"
    
  6. Run the database migrations for Laravel. This can be done locally by setting your parameters in .env or by passing them in as environment variables. Be sure to replace YOUR_DB_PASSWORD below with the root password you configured:

    # create a migration for the session table
    php artisan session:table
    DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD php artisan migrate --force
    
  7. Modify your app.yaml file with the following contents:

    runtime: php
    env: flex
    
    runtime_config:
      document_root: public
    
    # Ensure we skip ".env", which is only for local development
    skip_files:
      - .env
    
    env_variables:
      # Put production environment variables here.
      APP_LOG: errorlog
      APP_KEY: YOUR_APP_KEY
      APP_LOG: errorlog
      STORAGE_DIR: /tmp
      CACHE_DRIVER: database
      SESSION_DRIVER: database
      ## Set these environment variables according to your CloudSQL configuration.
      DB_HOST: localhost
      DB_DATABASE: laravel
      DB_USERNAME: root
      DB_PASSWORD: YOUR_DB_PASSWORD
      DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME"
    
    beta_settings:
        # for Cloud SQL, set this value to the Cloud SQL connection name,
        # e.g. "project:region:cloudsql-instance"
        cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"
    
  8. Replace each instance of YOUR_DB_PASSWORD and YOUR_CLOUDSQL_CONNECTION_NAME with the values you created for your Cloud SQL instance above.

See more by @{[ TutorialCtrl.tutorial.github_username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.