Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
UPDIVISION builds unicorns. Our developers have been through a lot. These are their stories. This one is told by Indra, one of our senior app-crafters.
Indra: I am an experienced software engineer and team lead. I know my projects inside outâââI have to. Iâm the one making sure all the pieces fit together, the servers are up and running, and the hardest tasks get done. When Iâm not doing that, I teach and do research at the university. Iâm eager to upgrade my skills all the time and pass on to others what IÂ know.
This week we had a client who wanted his Laravel web app deployed in Google App Engine. There were multiple reasons for this:
- cost (reported to be cheaper than Amazon)
- multi region
- autoscaling
Plus, we already use a lot of Google APIs in this particular app, so it looked like a pretty easy job to do.
What do you know? It wasnât easy. But I made it work in the end.
Going through this experience, I found the hard way that thereâs hardly any information on this topic. And the only tutorial I was able to find was posted on the Google community and it was missing a lot of details.
Before starting:I recommend before starting your journey of deploying a Laravel app with Google App Engine to read this entire article. Get everything ready. And only then do the deploy. Otherwise you might end up like me and lose a lot of time and stress yourself out.
Disclaimer:While I didnât do any modifications to the nginx setup I will talk about it in a future tutorial. Iâm considering doing a tutorial about adding a custom domain and the issues I encountered. But there are already some very good tutorials on YouTube on this topic.
Also, I used standard MySQL for this tutorial, but using big data is possible as well.Because I didnât want to make this blogpost hard to read, in some sections, I added links and YouTube videos instead of images.
Some of the documentation on Google regarding PHP deployments is lacking, so I substituted it in some places with the documentation from Python. If you plan on deploying a Laravel app with Google App Engine just by following the Google documentation I highly recommend having a look at both.
Prerequisite reading (you can skip to Preparing for the deploy)
2. PHP on Google Cloud Platform
Weâll be using Flexible environment so read about that as well.
3. (Optional) Documentation for Python
Preparing for the deploy
1. Create a Google cloud account: here or more directly here.
At the moment, you get a 365 day or a $300 credit free trial (whichever comes first). Unless you are a very old user (beta tester) you will also be required to add your credit card, but you will not be charged.
2. Create a new project (quick video here if needed)
3. Create an app engine instance (quik video here if needed)
4. We will use Google Cloud Shell to deploy
To be able to use it you need to download the cloud SDK for your operating system.
For some of the settings, you can use the cloud shell directly from your browser as seeen here. I do not use it, but read the docs to find out how. But you should really install it for ease of use.
If you run Windows you need to open the installer with administrator rights. Follow the quickstart tutorial from google for the other OSs.
5. Before you continue with Google Cloud Shell, I recommend reading this about the PHP environment.
6. Login in the console using command gcloud auth login. You will be logged in through the browser. If you want to see all the available commands write gloud help in the command line and press enter.
7. Write gcloud config set project project_id in the command line, where project_ id is the project id you just gave to your newly created project. It can be found on Google console on the first page after you select the project under Project info widget.
8. To see a list of all your projects type gcloud projects list.
This is it for the prerequisites. We will go through the database setup and downloads needed as we move along.
Preparing your project
For this part I used this tutorial as an inspiration. You can read it or skip it completely since I will be covering all the steps needed for deploy, except for the file storage part. For that I will give you some info, but not a full blown tutorial.
For this tutorial I will be using as an exemple an application I am building right now. A simple blog having Backpack for Laravel as admin panel. While weâre here, I have to let you know that Vue.js is compatible with Google App Engine. You just need to run npm install and npm run prod and that will do the trick. In this tutorial we will not be using Vue.js.
1. Change the directory to you application folder.
For ease of use you can set things up like this:
TUTORIALDIR= C:/Code/updivision-laravel-demo/
git clone https://github.com/Updivision/laravel-demo-google-cloud.git $TUTORIALDIR
cd $TUTORIALDIR
git checkout phase0-helloworld
We will need to create an app.yaml file. Just create it manually at the same level as composer.json.
2. Now that we have the file we need to setup the database and project details.
3. To setup the project details we will not be using the .env file from Laravel, but instead this app.yaml. Please follow the comments added in my sample of app.yaml to understand how I wrote it.
It needs to look like this:
runtime: php # language of the appenv: flex # let app engine know we use flexible environment
runtime_config: document_root: public #folder where index.php is
# Ensure we skip ".env", which is only for local developmentskip_files: - .env #we want to skip this to make sure we donât mess stuff up on the server
env_variables: # Put production environment variables here. APP_ENV: local # or production APP_DEBUG : true # or false APP_KEY: base64:FqwXmLtL6szdWqIVydb7nKs2eyvHib4lf6d5l6A/XW4= #go to generate app key paragraf in this tutorial
CACHE_DRIVER: database
# instead of putting the cache in the database I recommend using redis SESSION_DRIVER: database #or file since both work
APP_LOG: daily APP_TIMEZONE: UTC #your timezone of choice
# follow the part of the tutorial on setting up your SQL database DB_CONNECTION: mysql DB_HOST: localhost DB_DATABASE: instance_id DB_USERNAME: database_user_name DB_PASSWORD: database_password DB_SOCKET: /cloudsql/instance_name
QUEUE_DRIVER: database #in case you execute queued jobs
MAIL_DRIVER: smtp MAIL_HOST: smtp.sparkpostmail.com MAIL_PORT: 587 MAIL_USERNAME: sparkpost_username MAIL_PASSWORD: ****************
LOG_DELETE: true # this is a parameter added by us in the project .env file. You can add here any setting you would add to your .env file
GOOGLE_VISION_PROJECT_ID : project_id
#we need this for the flex environmentbeta_settings: # for Cloud SQL, set this value to the Cloud SQL connection name, # e.g. "project:region:cloudsql-instance" cloud_sql_instances: project:cloudsql-instance
4. Generate the app key.
To generate the app key for the Laravel installation we need to run:
php artisan key:generate --show
And then copy paste that in app.yaml.
5. Update composer.json to match Google App Engine requirements.
In scripts, add script as below. In post-install-cmd script there is a recommendation to add php artisan cache:clear however from my experience with Google deploy this does not work. Thatâs why I recommend leaving it out. I only added the minimum Laravel packages in this composer.json example.
{ "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "type": "project", "require": { "php": "7.1.*", "fideloper/proxy": "~3.3", "laravel/framework": "5.5.*", "laravel/tinker": "~1.0" }, "require-dev": { "filp/whoops": "~2.0", "fzaninotto/faker": "~1.4", "mockery/mockery": "~1.0", "phpunit/phpunit": "~6.0", "symfony/thanks": "^1.0" }, "autoload": { "classmap": [ "database/seeds", "database/factories" ], "psr-4": { "App\\": "app/" } }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "extra": { "laravel": { "dont-discover": [ ] } }, "scripts": {
"post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "@php artisan key:generate" ], "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover" ], "post-install-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postInstall", "@php artisan optimize", "chmod -R 755 bootstrap\/cache" ], "post-update-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postUpdate", "@php artisan optimize" ], "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover" ] }, "config": { "preferred-install": "dist", "sort-packages": true, "optimize-autoloader": true }}
7. Run gcloud app deploy in the google sdk command line (you have to be in the application folder) and when prompted answer Y.
descriptor: [\blog\app.yaml]
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.