How to deploy a Laravel application on Google app engine (GAE) in Google cloud platform (GCP)
Exploring deployment of Laravel application on cloud platform using Google app engine
In this article, we'll be using Google cloud app engine to deploy a Laravel application on the cloud.
- App Engine
Build monolithic server-side rendered websites. App Engine supports popular development languages with a range of developer tools.
New customers get $300 in free credits to spend on App Engine. All customers get 28 instances in a standard environment free per day, not charged against your credits.
Deploy a website using App Engine with this quickstart
Free up your developers with zero server management and zero configuration deployments
Stay agile with support for popular development languages and a range of developer tools
Learn how to run your applications in App Engine using a flexible environment, standard environment, or both.
See how the App Engine standard environment makes it easy to build and deploy an application that runs reliably even under heavy load and with large amounts of data.
Find out how App Engine allows developers to focus on what they do best: writing code.
- Create a Google cloud platform (GCP) Account
If you don't have an account you can create one or login here
- Prepare your project
After creating the account you should get this screen
- Next, search for app engine on the console dashboard
- Click on create application
To create the app engine for deployment
- Select the location for the application and click next
- Select PHP as the resource
- When you click on the "download SDK" you'll be redirected to this page
Find the Google cloud CLI installer for your operating system here
The complete guide for exploring more about Google Cloud CLI
Download the SDK and install it as an administrator
Next, follow the installation setup instructions (pretty simple)
at the end of it, a window will pop asking you to confirm if you want to start the gcloud init.
It'll prompt the browser to log in or select the account to use and when you close the window it'll automatically continue on the shell
The setup approach is similar to the command line interface installation for Heroku. The installation automatically sets the environment variable
- After successful installation and setup this window appears
We'll host the Laravel application used in demonstrating google cloud features integration in Laravel, you can find the repository here if you intend to try out how it works following this tutorial.
- Next, create the app.yaml file in the Laravel root directory.
Since Laravel uses the env file to load the application we will use the same env variables and values in app.yaml file to load the application on the app engine.
Look up the documentation here for more information about app yaml file configuration.
NB: in the .gitignore file add this to ignore the app.yaml file from been committed to the repository
app.yaml
- Let's update the app.yaml file to reflect the env variables needed
The deployment is for - Laravel version 9 and PHP version 8.1
runtime: php81 # version of php
env: standard # set app engine env to standard
runtime_config:
document_root: .
# for detecting static files in the public folder
handlers:
- url: /(.*\.(gif|png|jpg|css|js))$
static_files: public/\1
upload: public/.*\.(gif|png|jpg|css|js)$
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
env_variables:
APP_KEY: base64:_________________
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: root
DB_USERNAME: root
DB_PASSWORD: ""
APP_NAME: "Google cloud vision"
APP_ENV: production
APP_DEBUG: true
APP_URL: ""
ASSET_URL: ""
SESSION_DRIVER: cookie
QUEUE_CONNECTION: sync
MAIL_MAILER: smtp
MAIL_HOST: ""
MAIL_PORT: 465
MAIL_USERNAME: ""
MAIL_PASSWORD: ""
MAIL_ENCRYPTION: ssl
MAIL_FROM_ADDRESS:
MAIL_FROM_NAME: "Google cloud vision"
PUSHER_APP_ID:
PUSHER_APP_KEY:
PUSHER_APP_SECRET:
PUSHER_APP_CLUSTER: mt1
//the tmp folder is used for caching on GCP as opposed to the default storage folder
APP_SERVICES_CACHE: /tmp/services.php
APP_PACKAGES_CACHE: /tmp/packages.php
APP_CONFIG_CACHE: /tmp/config.php
APP_ROUTES_CACHE: /tmp/routes.php
- To view the credential accounts
gcloud auth list
Credentialed Accounts
- If you've multiple authenticated accounts - you can use the command below to set the active account:
gcloud config set account `ACCOUNT`
- To delete cached files using the command below
php artisan optimize:clear
- To check if there are multiple projects on your account
gcloud projects list
**NB you can use the config set to set a particular project **
gcloud config set project project-id
if the project is successfully configured it returns this output
Updated property [core/project].
- Let's deploy the app using the gcloud deploy command
gcloud app deploy
or you can use the command below to deploy a particular project.
gcloud config set project project-id
NB: if you encounter this error
Kindly go to the .gcloudignore file and ignore the node_modules and storage log file.
# PHP Composer dependencies:
/vendor/
/storage/logs/laravel.log
/node_modules/
- Deployment progress
Do you want to continue? Type y and press enter
y
- To stream logs from the command line run:
gcloud app logs tail -s default
- To view the application on a web browser run:
gcloud app browse
- To take a quick anonymous survey, run:
gcloud survey
- To install available updates for Google Cloud CLI components, please run the command below as an administrator preferably on the Google cloud CLI:
gcloud components update
Head over to the app engine dashboard to view the instances, versions etc... of the application deployment
- In the settings section, you can set custom domains
App Engine lets you serve your application through a custom domain. If you use a custom domain, Google will provide a managed auto-renewing SSL certificate for security.
For file uploads and management you can use Google cloud storage which is covered here: How to upload files to Google cloud storage from a Laravel application.
In the next article, we'll cover how to set up a database on the Google cloud platform for the Laravel application.
Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.