This tutorial will briefly show everyone on how to set up Laravel version 5.6 on Heroku. As we all have known, Laravel is fantastic PHP Framework for building dynamic web application. One thing that’s hard is when it comes to bringing it live aka into production and share it to the world.
Taylor Otwell; the creator of Laravel has made Laravel Forge which allow users to “Provision and deploy unlimited PHP applications on DigitalOcean, Linode, AWS, and more”. However, it comes with a cost and not everyone can afford to pay a monthly subscription. In this post, I will guide you to set up Heroku which is a Cloud Application Platform for your Laravel project through a step by step instructions.
Like any other online services, what we have to do is register for an account first. Once you have registered, make sure to validate your e-mail address and then log in to the dashboard. The dashboard will look like this, unlike in the past, this new dashboard looks more intuitive and user friendly.
Press the new button on the top right side of the page and you will be redirected to the “Create New App” page. Name the application anything you would like and choose a region; in this case, I will name it “laravel-tut” and Europe as the region.
Press the “Create App” button and you will be redirected to the Heroku app dashboard that you have just created.
From this page, you can manage each individual application setting. This will be unique for each application that you create and they have a very easy configuration that you can experiment on your own.
Because, this tutorial will only be showing you how to set up Laravel on Heroku, I will publish the default Laravel application that’s generated after the installation by using 💻 laravel new command. **Please note that this tutorial won’t cover setting up online database and will just cover the installation part.
To deploy the application, there are several ways/deployment methods that we can use. Each of these deployment methods essentially will have the same outcome but in this tutorial I will assume that everyone is using Git and GitHub as their version control system and git hosting.
This step will require you to connect to your GitHub account with Heroku. To do this, make sure to press the GitHub tab and link your account to Heroku. If they prompt the permission modal, do accept it and you will be able to see all of your GitHub repository from Heroku dashboard. I assume everyone have basic knowledge of Git and GitHub and if you don’t, do read this guide and come back here after you’re done.
In this case, I will create a new repository called laravel-tut and I will push the laravel website that I have just generated to this repository. If you have any existing repository, you can just skip this step.
Once you have your Laravel project on GitHub, head back to Heroku and let’s associate the Heroku application with the project that we have just pushed to GitHub. Here, you will have to search for your repository name and select the project.
Upon connecting the repository, you will see a slight change in the Heroku application dashboard. This will reflect your setting and now you will see that you have associate the Heroku with the application that will be deployed.
The only thing that you should do now is to push it online by pressing the Deploy Branch button. This won’t suffice though because there’s few more setting/configuration that we should perform. The first one is the environment variables because Laravel is using .env as it’s environment variable, we’ll need to explicitly specify it on Heroku. To do so, open the settings from the dashboard and here you will be able to add and remove any configuration variables.
On this page, you will need to set the Laravel key and other keys if your application requires one. In this situation, I will only need the APP_KEY variable, this app key can be the same as your local Laravel Key.
One thing before you pressed the Deploy Branch button, we’ll need to add additional Heroku config which is called Procfile and one adjustment for the post-install-cmd script on our Laravel composer package file. This naturally tells Heroku what type of web application we are building, so it can fetch the dependencies for us.
Add the following settings and save it as Procfile without any extensions on the root level of the Laravel application and the following script in the scripts section of your composer.json file. This will ensure the public folder is accessible by everyone.
web: vendor/bin/heroku-php-apache2 public/
"post-install-cmd": [ "php artisan clear-compiled", "chmod -R 777 public/" ]
Now push this changes to GitHub and then deploy the application. Upon pressing the Deploy button you will see all of the the logs of what Heroku is actually doing. Wait for a few seconds until it’s finished and then you will eventually see a message “Your app was successfully deployed”.
Now, view your application on the browser. Here, it will be laravel-tut.herokuapp.com. Do note that I will terminate the link upon completing this tutorial, so you won’t be able to visit the link.
If you have reached this far, well done 🏆 here’s a trophy for you! and if there’s anything that you guys would like to ask, do comment down below and I will see if there’s any solutions I can do ASAP.
In conclusion, setting up Laravel Application online is not as hard as it used to be. Laravel Forge is great service that’s directly provided by the creator of Laravel but for early/learning stage, it’s somewhat not worth it.
Once again, thanks for checking out this tutorial and hopefully you guys have learned something new! Do make sure to share this post with your friend and hit the FB like button. Happy Deploying!