A quick & simple way to deploy a Laravel + PHP app with built-in Postgres
This tutorial will take you over deploying a Laravel PHP application on Hasura.
Benefits of using Hasura to deploy and host your Laravel app:
- A Hasura project comes with a pre-configured Postgres that’s ready to be used
- git push hasura master inside your git repo will deploy your application to your server
Basic deployment
Follow the 4 steps below so that you can start off and deploy a Laravel app within minutes to a cloud server.
Note: Following the cloud deployment section, we show you how to deploy and test your app on your local machine.
Step 1a: Get a hasura project and make a note of your credentials
Sign up on http://dashboard.hasura.io and get yourself a Hasura project. Creating a hasura project will give you a domain, something like: project42.hasura-app.io.
You will also get an email with your admin credentials for your project console and your database (search for “hasura credentials” in your inbox if you’re having trouble finding the email).
Specifically, note your project name postgres credentials:
Step 1b: Install hasuractl
Install the command line tool: hasuractl
.
Read the full instructions here.
Once you’re done with that, login and setup hasuractl
:
Make hasuractl
talk to the specific project you created in Step 1a. (this was project42
in the example above)
Step 2: Initialise a Laravel project with an app name
Run the following command to initialise a Laravel app that can be instantly deployed:
This is the file structure that will be setup:
Step 3: Configure your Laravel .env
file and set the Postgres password
Use the postgres password from Step 1a, and set that in your .env
file.
Step 3: Use hasuractl to add your SSH key to the Hasura project
You can’t just start pushing code to a new hasura project without making sure that you are really you! hasuractl
can push your SSH key to your hasura project cluster so that you can start pushing your code.
Step 4: Run the database migrations and generate a new application key
Set up a tunnel to securely connect to the Postgres database on the Hasura project and run the migrations.
Now, in a different terminal:
Note the exact value between the [ and the ]. This is the application key, and we’ll be using it later: base64:xVnT+XfhwcOZ76qaNcbFeb3YXWsoLLylqtKp6rdO5EQ=
Step 5: git push
and you’re done (almost)!
Step 6: Set the database password as an environment variable
Head to the Data > Administration section of the console and copy the database password.
Head to the Git push > my-app > update section of the console and create a new environment variable called DB_PASSWORD and paste the password as the value. Create another env variable called APP_KEY and paste the application key from above.
Note: Make sure you copy the exact string since it is base64 encoded and will error out if you miss any characters.
Step 7: You’re done, view your app
Head to my-app.project42.hasura-app.io to view your app. This is a simple app that uses a single table in a database to store values and fetch values. Remember to replace project42 with your actual project’s name!
Local development
Considering that the Postgres database is already on the Hasura project cluster, when you are developing your application on your local machine, you might want to connect to the database too.
Step 1: Run all the steps above (skip if you already have)
Make sure that you’ve already run all the steps above. Step 5: Git push is only important if you’re deploying the app, so you can skip that.
Step 2: Create a .env file with the remote postgres password
Now open the .env file in your favourite editor and edit the DB_PASSWORD value The same that you used in step 3 above.
Step 2: Run php artisan serve
!
And everything works. :)
Hasura is a Postgres BaaS + Kubernetes PaaS to help you build and deploy backends super fast. Try it out at https://hasura.io
Originally published at docs.hasura.io.