php-docs-samples/appengine/php72/laravel-framework at php72-scripts · johnulist/php-docs-samples · GitHub
Skip to content

Latest commit

 

History

History
 
 

README.md

Laravel on App Engine for PHP 7.2

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. This version was tested to work with laravel/laravel-framework:^5.6.

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: php72
    
     env_variables:
       # Put production environment variables here.
       APP_LOG: errorlog
       APP_KEY: YOUR_APP_KEY
       APP_STORAGE: /tmp
    
  2. Copy the bootstrap/app.php and config/view.php files included in this sample into the corresponding directories of your Laravel application. These two files ensure your Laravel application writes to /tmp for caching in production.

If you are using an existing Laravel application, just copy the google-app-engine-deployment blocks from these files.

  1. 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
    
  2. Run the following command to deploy your app:

     gcloud app deploy
    
  3. 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. Keep track of your instance name and password, as they will be used below.

  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.

    • Use the Google 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 | grep connectionName
      
    • Start the Cloud SQL proxy and replace YOUR_CONNECTION_NAME with the connection name you retrieved in the previous step.

        cloud_sql_proxy -instances=YOUR_CONNECTION_NAME=tcp:3306
      
    • Use gcloud to create a database for the application.

        gcloud sql databases create laravel --instance=YOUR_INSTANCE_NAME
      
  3. 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
     export DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD
     php artisan migrate --force
    
  4. Modify your app.yaml file 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"
    
  5. Replace each instance of YOUR_DB_PASSWORD and YOUR_CONNECTION_NAME with the values you created for your Cloud SQL instance above.