Deploying Laravel application on Google App Engine flexible environment.
I really liked Google App Engine(GAE) as its simple to deploy the application onto it and GCPs user interface is also less chaotic. I hardly had to make any changes in my application to make it compatible with GAE, except it should be cloud ready.
There was one particular issue which I faced while deployment that I have mentioned at the end.
The Prerequisites :
- Google Cloud Platform account.
- Google Cloud SDK.
So let's get started :
- Firstly, we create a new project from the GCP console, Click on Select a project -> New Project
- You need to type in the Project Name(for me its Demo Project) and can keep the Location as default.
- Now we would be creating a SQL instance, Select SQL from the navigation pane.
- Select Create instance.
- Choose a database engine, in my case its MySQL.
- Make sure that the correct project is selected while creating the engine.
- We would be creating a 2nd generation MySQL instance.
- In the next step, specify the Instance ID (demo-instance for me), Root Password, Region (asia-south-1 for me).
- Once the MySQL engine is created, we can select it and create a new database for our application.
- Once the database is created, you can use the import functionality to import your schema(MySQL dump file) into your database (testdb in my case).
- Next, we need to enable the Cloud SQL Admin API for our project, select APIs and Services from the navigation pane, and enable the API.
- We need to add the app.yaml file into our project, it contains the following details:
- You can get the cloud_sql_instance id from the overview tab in the instance details.
- Add the following under scripts in the composer.json
“post-install-cmd”: [
“chmod -R 755 bootstrap\/cache”,
“php artisan cache:clear”
]
- We start with the deployment now, we would be using the Cloud SDK for that. Run all the commands from the project folder.
- We first Login into GCP (Running this command opens a tab on your default browser asking for your account details)as :
gcloud auth login
- Next, we set our default project (You can get the project id on clicking on the project).
gcloud config set {yourprojectid}
- We can deploy the project as,
gcloud app deploy
- It would ask you to specify the region in which you wish to deploy the app.
That’s it, this would deploy your app on GAE.
Issue :
I had specified the Gate rule to implement Authorization in the AuthServiceProvider as :
Since the Providers get called before the connection is made with the database, my deployment failed.
So I had to move the Gate rule to Middleware and call that middleware on the specific routes which needed authorization.
Please get in touch in case of any queries.
Thanks.