Deploy Laravel on Google App Engine – Flexible and
This guide demonstrates how to set up deploy your first Laravel app in App Engine Flexible Environment with Cloud SQL.
Not using App Engine? Choose a different Category:
Prerequisites
- Create a project in the Google Cloud Platform Console.
- Enable billing for your project.
- Install and initialize the Google Cloud SDK.
- Create a Cloud SQL Second Generation Instance.
- 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: php
env: flex
runtime_config:
document_root: public
skip_files:
- .env
env_variables:
APP_LOG: errorlog
APP_KEY: YOUR_APP_KEY
03. Add the following lines inside scripts in your composer.json
Edit the composer.json
and add the below code before.
"post-install-cmd": [
"chmod -R 755 bootstrap/cache",
"php artisan cache:clear"
]
So your composer.json
scripts json
object will look like this.
"scripts": {
"post-autoload-dump": [
"IlluminateFoundationComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r "file_exists('.env') || copy('.env.example', '.env');""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
],
"post-install-cmd": [
"chmod -R 755 bootstrap/cache",
"php artisan cache:clear"
]
}
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 CLOUDSQL_CONNECTION_NAME
You can get the connection name from your Cloud SQL Dashboard.
./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: php
env: flex
runtime_config:
document_root: public
skip_files:
- .env
env_variables:
APP_LOG: errorlog
APP_KEY: YOUR_APP_KEY
CACHE_DRIVER: database
SESSION_DRIVER: database
DB_HOST: localhost
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.
Conclusion
Now you have learned how to deploy your Laravel application on Google App Engine – Flexible Environment.
Thanks for your time. If you face any problem or any feedback, just leave a comment below.
Leave a Reply
You must be logged in to post a comment.