When looking for a more stable hosting solution for my Magic: The Gathering Api, I stumbled across Google’s App Engine. If you have not read up on this solution, I would recommend it.
To get Laravel’s Lumen running correctly on Google App Engine, there are a few questions that we need to answer. Lets take these one at a time.
How does the application know it is running on Google App Engine?
One of the easiest ways to figure out if the application is running on Google App Engine is to check if the .env file is present. This is because the deployment “ignore” RegEx includes any files that start with a . (AKA Unix hidden files). This function will come in handy when answering the next questions.
|  |  | 
How do we make logs work with the Google console?
After researching how to get logs working with Google, a lot of people recommend editing vendor files. Do not do this, let me repeat, do not edit the vendor files. Let’s take the more stable approach and override the function that is causing the issue.
The first thing we need to do is create the class that will override the logging function. We can do this by making a new class that extends Laravel\Lumen\Application. Then we can override the logging function.
file: app/Bootstrap/GoogleApp.php
|  |  | 
How do we setup environment variables?
Just like normal, the .env file will be used for local environment variables. However, the .env file is not deployed to Google App Engine, so we need to find a work around. The good news is app.yaml allows for environment variables. And even better news, Google App Engine plays nice with these environment variables.
The most important thing to keep in mind is security. Just as .env is ignored in our git repo, we want to ignore app.yaml as well. With this file being excluded from version control, we want to make sure there is a template that users can follow. We can make app.yaml.example that contains the structure with no credentials. Then we can make app.yaml which is a copy of app.yaml.example with the correct credentials.
file: app.yaml
application: mtgapi-service
version: 1
runtime: php55
api_version: 1
handlers:
- url: /favicon\.ico
  static_files: public/favicon.ico
  upload: public/favicon\.ico
- url: /.*
  script: public/index.php
env_variables:
  APP_ENV: production
  APP_DEBUG: false
  APP_KEY: SomeRandomStringHere!!!
  APP_LOCALE: en
  APP_FALLBACK_LOCALE: en
  DB_CONNECTION: mysql
  DB_HOST: localhost
  DB_PORT: 3306
  DB_DATABASE0: homestead
  DB_USERNAME: homestead
  DB_PASSWORD: secret
  CACHE_DRIVER: file
  SESSION_DRIVER: file
  QUEUE_DRIVER: syncHow do we set php.ini settings?
This one is very simple. We just need to create a file in our application root called php.ini. Here, you can set php.ini settings that will be used. There are two settings I want to bring to your attention.
- google_app_engine.enable_functionsneeds to be set to- php_sapi_namefor Lumen to work properly on Google App Engine.
- google_app_engine.enable_curl_liteneeds to be set to- 1if you are using curl in any way.
file: php.ini
|  |  | 
How does everything fit together?
Now that we have a function that lets us know when we are running on Google App Engine; have a new Google Application class; set our php.ini settings correctly; and have our environment variables set, all we need to do is put the puzzle pieces together.
To do this, we need to edit the file bootstrap/app.php.
- Load the new Google application class if we are running on Google App Engine.
- Don’t load the default .envfile if we are running on Google App Engine.
file: bootstrap/app.php
|  |  | 
Note: This is the top of the file bootstrap/app.php. The lines that state $app->withFacades(); and $app->withEloquent(); should immediately follow.
How do we deploy?
Awesome! Now that we have everything ready to deploy, lets get this new Lumen project running on Google App Engine. Keep in mind the following steps assume you are running Mac OSX. You may have to do things a little differently if you are running Windows or Linux.
- Download the Google App Engine Launcher
- Open GoogleAppEngineLauncher-*.*.*.dmgand moveGoogleAppEngineLauncher.appinto yourApplicationsfolder
- Open GoogleAppEngineLauncherand accept (click ‘yes’/‘accept’) and pop-ups and enter your system password
From this point, we have to make a decision: do we want to deploy from GoogleAppEngineLauncher or the terminal?
Deploy From GoogleAppEngineLauncher
Pro(s)
- One click deploy
- Deploy multiple apps from a single application
Con(s)
- Application root folder must have the same name as the Google App Engine project ID
- Do not have the ability to use the full functionality of appcfg.py(the deployment command)
If you’re going this route, please make sure the name of your application root folder is the same name as your Google App Engine project ID. Now, let’s get started:
- Click the +button in the bottom left corner of the app
- Enter your Application Id
- Select the folder where your application root resides
- Select Runtime as PHP
- Click ‘Create’
- Highlight the newly created application from the list
- Click the deploy button in the top right
- Auth with Google (a webpage will pop up)
Deploy From Terminal
Pro(s)
- Ability to use full functionality of appcfg.py(the deployment command)
Con(s)
- You have to remember the command or at least make a bash script to run so you don’t have to remember it
Congratulations! It looks like this isn’t your first rodeo. This is by far the most flexible way to deploy your application.
- Open terminal
- run appcfg.py -A <application-id> update <application-root-path>
- Auth with Google (a webpage will pop up)
Conclusion
We did it! Everything is running smoothly and you can access your new Lumen project at http://app-id.appspot.com
As we are now using Lumen on Google App Engine, there are a few other things that may need to be done to connect to your CloudSql Instance, use queuing, etc… Stay tuned for more posts on topics like these.