Google App Engine is a Fully managed serverless application platform for developing and hosting web applications. App Engine offers automatic scaling for web applications. App engine support most of the familiar languages like Java, PHP, Node.js, Python, C#, .Net, Ruby and Go in both standard and flexible environment or you can use your own language runtimes and frameworks using flexible environment. Standard environment is a sandbox and pre defined runtime by google that gives you freedom from managing server and runtime and provide rapid scaling for your application. Using the flexible environment you can bring your own runtime and run any version or dependencies of the supported languages. you can read more about the difference between the two environment Here
Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller architectural pattern and based on Symfony. (Wikipedia)
We can deploy Laravel to both app engine standard and Flexible environment. In this tutorial we will go the standard environment. Standard environment provide easy deployment and provide free runtime at the start so you can easy deploy and test your application without worrying about charges.
Before starting your deployment Create a projectt in Google Cloud Console and Install and Initialize The Google Cloud SDK
Create a laravel Project and Test it locally Official Documentation
Visit http://localhost:8000 to see the Laravel Welcome page.
Now prepare your application for Deployment to app engine, for this purpose first of all you need to create an app.yaml file
# app.yaml runtime: php72 env_variables: ## Put production environment variables here. APP_KEY: APP_KEY APP_STORAGE: /tmp VIEW_COMPILED_PATH: /tmp SESSION_DRIVER: cookie
Replace APP_KEY in app.yaml with an application key you generate with the following command:
php artisan key:generate --show
In this app.yaml file we are defining our runtime as php7.2 and then we have laravel environment variable declaration. Laravel .env file doesn't work in app engine so you have to declare all your laravel environment variable here. Laravel use PROJECT_ROOT/storage for storing for storing runtime files like cache, session, blade view cache but in app engine environment that directory isn't writeable so it will give an error. The only writeable directory in app engine is /tmp we have to change the APP_STORAGE to /tmp. In current app engine environment laravel is giving error with File base session driver so we are using cookie based driver for storing laravel sessions.
Next modify bootstrap/app.php file to use the storage path defined in environment variable instead of PROJECT_ROOT/storage by adding the following lines of code.
/* |-------------------------------------------------------------------------- | Set Storage Path |-------------------------------------------------------------------------- | | This script allows you to override the default storage location used by | the application. You may set the APP_STORAGE environment variable | in your .env file, if not set the default location will be used | */ $app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));
this will check for environment variable APP_STORAGE if found it will use that if not then it will use the default storage path.
One last step before deploying your app remove the beyondcode/laravel-dump-server composer dependency. This is a fix for an error which happens as a result of Laravel's caching in bootstrap/cache/services.php.
composer remove --dev beyondcode/laravel-dump-server
Finally now you can deploy your project to app engine using
gcloud app deploy
App Engine will create a default URL for your project that is http://YOUR_PROJECT_ID.appspot.com , replace YOUR_PROJECT_ID with your project id from console and visit the url you will be able to see your home page.
At this point you will be able to load your page with the content that Laravel has returned, but you won't be access any static content like images, css, js file that are in your public folder. This is because when you deploy an app app engine will look for index.php or public/index.php and will forward all the request to it. To change this you have to add handlers to your app.yaml file.
Add the following to your app.yaml file before env_variables:
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
This will tell app engine to serves files with extension of gif, png, or jpg as static resources and reponsd to all other urls using auto behavior. secure:alway will return non-secure urls to https with a response code of 301.
After adding the above lines redeploy your app using the same gcloud app deploy it will create an updated version for your app and now you will be able to load your images, css and js files. Any other kind of files like font, txt or xml that you want to load as a static content you have to add its extension to url and upload of handler.
If you want to connect your application to Cloud SQL you can create a Cloud SQL instance and database using cloud console or using gcloud sdk and then add your db configuration to app.yaml environment variable.
## Set these environment variables according to your CloudSQL configuration. DB_DATABASE: YOUR_DB_DATABASE DB_USERNAME: YOUR_DB_USERNAME DB_PASSWORD: YOUR_DB_PASSWORD DB_SOCKET: "/cloudsql/YOUR_CONNECTION_NAME"
Then you will be able to connect to SQL from app Engine. to connect to Cloud SQL instance from your local or development environment outside google cloud you need to use cloud_sql_proxy. To run laravel migration you have to use cloud_sql_proxy.
Hope You enjoy this, If you have any question don't hesitate to reach out at twitter
You can follow me on twitter @ilyash00 where I share some cool stuff about programming and occasionally create some zines