We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Deploy Laravel on Google App Engine – Standard

Deploy Laravel on Google App Engine – Standard and connect with Google Cloud SQL with Cloud SQL Proxy.

This guide demonstrates how to set up deploy your first Laravel app in App Engine Standard Environment with Cloud SQL.

Not using App Engine? Choose a different Category:

App Engine – Std

Prerequisites

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install and initialize the Google Cloud SDK.
  4. Create a Cloud SQL Second Generation Instance.
  5. Composer installed on your local computer.

Local Laravel Set Up

Create a directory in your local computer and install the Laravel installer.

mkdir my-project
composer global require laravel/installer

Create a new Laravel application using the laravel new command.

cd my-project
laravel new my-app

Now Laravel will be installed inside the my-app directory.

You can verify the installation with the following command.

cd my-app
php artisan serve

Visit http://localhost:8000 on your web browser to see the Laravel Welcome page.

Set up Laravel for App Engine

01. Generate application key with the following command.

php artisan key:generate --show

02. Create an app.yaml file with the following contents:

Replace YOUR_APP_KEY with the application key you have generated in the above step.

runtime: php72
env_variables:
APP_KEY: YOUR_APP_KEY
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp

03. Set the storage path to /tmp for caching in production.

Edit the bootstrap/app.php and add the below code before the return statement.

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

So your app.php will look like this.

$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;

04. Now remove the laravel-dump-server repository to prevent the Laravel’s caching error.

composer remove --dev beyondcode/laravel-dump-server

Now your application is ready to be deployed to App Engine.

Enable APIs and Create New Service account

Go to APIs and Services and click Enable APIs and Services and enable Cloud SQL API and Cloud SQL Admin API

Go to IAM & Admin >> Service accounts and click Create service account

In step 1

Enter Service account Name

Click Create

In step 2

Select Role

Cloud SQL >> Cloud SQL Client
Project >> Editor

Click Continue

In step 3

Click Create Key

Choose Key type as JSON

Click Create

Install Cloud SQL Proxy in Local Machine

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.

Start the Cloud SQL proxy and replace CLOUDSQL_CONNECTION_NAME with the connection name of your Cloud SQL Instance.

You can get the connection name from your Cloud SQL Dashboard.

Cloud SQL Connection Name
./cloud_sql_proxy -instances=CLOUDSQL_CONNECTION_NAME=tcp:3306

If you are using windows machine you need to update the ./cloud_sql_proxy with the path of the downloaded proxy.

Run Migrations for Laravel

Once the Proxy is started you will something similar to this.

2019/03/06 15:48:17 Listening on 127.0.0.1:3306 for CLOUDSQL_CONNECTION_NAME
2019/03/06 15:48:17 Ready for new connections

Now edit your .env file and update the database details.

DB_DATABASE=CLOUD_SQL_DATABASE_NAME
DB_USERNAME=CLOUD_SQL_USERNAME
DB_PASSWORD=CLOUD_SQL_PASSWORD

Important: Open a new command prompt and navigate to your Laravel’s root directory and run the database migrations for Laravel.

php artisan session:table
php artisan migrate --force

If the migrations are completed successfully, you will see the output similar to the one below.

Output
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_03_06_101455_create_sessions_table
Migrated: 2019_03_06_101455_create_sessions_table

Great! Now you can deploy your Laravel application to App Engine.

Deploy Laravel to App Engine

Edit your app.yaml and update the Cloud SQL details.

runtime: php72

env_variables:
APP_KEY: YOUR_APP_KEY
APP_STORAGE: /tmp
CACHE_DRIVER: database
SESSION_DRIVER: database
DB_DATABASE: CLOUD_SQL_DATABASE_NAME
DB_USERNAME: CLOUD_SQL_USERNAME
DB_PASSWORD: CLOUD_SQL_PASSWORD
DB_SOCKET: "/cloudsql/CLOUDSQL_CONNECTION_NAME"

Once done, run the below command to deploy the app on App Engine.

gcloud app deploy

Once the deployment is completed check this url (http://YOUR_PROJECT_ID.appspot.com) to see the Laravel Welcome page.

Laravel Welcome Page

Conclusion

Now you have learned how to deploy your Laravel application on Google App Engine – Standard Environment.

Thanks for your time. If you face any problem or any feedback, just leave a comment below.

Cart

Your Cart is Empty

Back To Shop