Deploying Laravel to Google Cloud Engine Part 1

Ai: African Intelligence
2 min readNov 10, 2020
Laravel + Google App Engine

Over the past months I decided I wanted a better hosting platform for my Laravel Application. I have used several hosting options like Dreamhost and Forge. Apart from the cost concerns and ease of deployment, I wanted to implement a complete CI/CD for my app.

Google cloud engine has a lot of benefits. At the moment, you get a 365 day or a $300 credit free trial. Also you have access to a myriad of solutions like Firestore, Storage, Cloud Compute , AI and many others under each project.

Preparing to Deploy

1. Create a Google cloud account: here or more directly here.

2. Create a new project (quick video here if needed)

3. Download and use Google Cloud Shell to deploy

Deploy your Laravel App

1. Create a new Laravel App or use an existing App (version 6x and above)

2. Add the app.yaml file to the root directory of your app

runtime: php73runtime_config:
document_root: public

env_variables:
## Put your environment variables here.
APP_KEY: base64:XeqggOkX2Bhng7Knlj3LMq1JWE1Cs4FPRmm+wp49wsM=
APP_ENV: local
APP_URL: localhost
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp
LOG_CHANNEL: stackdriver
APP_DEBUG: true
CACHE_DRIVER: file
SESSION_DRIVER: cookie
handlers:
- url: /robots.txt
static_files: public/robots.txt
upload: public/robots.txt
- url: /favicon.ico
static_files: public/favicon.ico
upload: public/favicon.ico
- url: /images
static_dir: public/images
- url: /js
static_dir: public/js
- url: /css
static_dir: public/css

3. Add this storage snippet to the bootstrap/app.php file before the return statement

$app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

4. Create and Add .gcloudignore to the root directory of your app

# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.env
.git
.gitignore
# PHP Composer dependencies:
/vendor/
# Node modules:
/node_modules/

5. Add these lines to the composer.json file

"post-create-project-cmd": [
"@php artisan key:generate --ansi",
"chmod -R 755 bootstrap\/cache",
"chmod -R 755 storage\/logs"
]

4. Initialize project using command prompt

gcloud init

5. Deploy

gcloud beta app deploy --no-cache

6. When you have multiple projects you can change deployment target

gcloud config set project <project-id>//thengcloud beta app deploy --no-cache

To add MYSQL and CRONs kindly follow this tutorial or watch out for part 2

--

--

Ai: African Intelligence

African Intelligence seeks to explore technologies that will be disruptive to the African Tech Ecosystem