Introduction

In this tutorial, I am going to show you how to set up Laravel 5.6 on Google App Engine. Before we start, there are 2 types of environments which Google Cloud provides, the Flexible Environment and Standard Environment, I recommend you to use the Flexible Environment as it provides more functionality and flexibility compared to the Standard Environment.

If you are new to Google App Engine, you might want to read out the documentation first to learn some basic information to avoid some caveats that might occur along the way.

Google App Engine Homepage
Google App Engine Homepage

Here’s a summary of both Environments:

Flexible Environment

  • Support for more programming languages.
  • Runs in a docker container that include source code written in other programming languages.
  • Depends on other software, including operating system packages such as ImageMagick, FFmpeg, libgit2, or others through apt-get.
  • Uses or depends on frameworks that include native code.
  • and more

Standard Environment

  • Only support source code that’s written in a specific version of the supported programming languages.
  • It’s intended to run for free or at very low cost
  • and more

As you can see from the summary, there’s a big advantage of choosing Flexible Environment over the Standard Environment. To read more, check out the App Engine Environment comparison.

Setting Up

Let’s start with Laravel! You will need to have Laravel Installer or either clone Laravel GitHub repository (which requires additional steps such as generating a key) to get it up and running. For the sake of this tutorial, I am going to use the plain Laravel (freshly installed).

Once you have installed it, run php artisan serve and open the link on your browser. If you see this page, then it’s code green.

Laravel running on Localhost
Laravel running on Localhost

For the App Engine Part, you have to download and install Google Cloud SDK. This SDK contains all the command to operate the Google Cloud platform. Once that’s ready, you have to create a new file on the root of the Laravel application called app.yaml and copy the following settings:

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

The above settings are to specify the runtime of our application, environment for our App Engine, runtime configuration and some other important settings such as the variables.

Change the YOUR_APP_KEY with the Laravel key that you have generated, or run the command below and copy it to the env_variables:

php artisan key:generate --show

After that, you need to add this command to your composer.json file under the scripts tag.

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

What this will do is change the cache directory permission recursively to 755 (rwxrw-rw-) and clear any existing cache to ensure any files that’s being deployed is fresh.

Composer.json file
Composer.json file

Deployment

To deploy the Laravel application to the cloud we can run gcloud app deploy on your terminal / command prompt and wait several minutes for it to set up.

Choose GAE Region
Choose GAE Region

Make sure to choose the region closest to your location and remember the instance details.

GAE Instance Details
GAE Instance Details

Once you see successful message on your terminal, you can visit:

http://YOUR_PROJECT_ID.appspot.com

Change YOUR_PROJECT_ID to your project name that you have set up in your account.

Laravel on Google App Engine Flexible Environment
Laravel on Google App Engine Flexible Environment

If there’s no error or whatsoever, you will see the exact same page as the one on your local machine. Please note that the next time you try to access the link on the example, it might not be available because I might have terminated it.

Conclusion

Setting up Laravel on Google App Engine is certainly easy and requires less configuration. With the advantage of ease of scalability that Google App Engine provides, you don’t have to worry over managing it for larger application and high volume of incoming traffics as Google App Engine will take care everything for you.

If you found this tutorial helpful, do share it with your friends and if you have any further questions, make sure to comment down below.

Part 2: Setting up Laravel 5.6 Database on Google App Engine

Worth Reading 🎺