Finishing a thing you started makes it worth the effort. — Anonymous
In this article am going to show you how you can deploy your Laravel application on Heroku the easier and fastest way. We all can attest to the fact that sometimes deploying your application to production can be a hassle which at the end may result to wasted hours. But before we get started let’s first understand some key terminologies and concepts relating to deployment.
WHAT IS DEPLOYMENT?
Deployment involves the processes and activities in getting your web application up and running in a production environment (server). In other words we can simply say deployment is the process of migrating your application from development environment (local server) to a production environment (hosted server), which allows everyone to have access to your application.
Its very important to know that without the hosting servers the deployment process cannot be complete because the whole idea of deployment is centered around these dedicated hosting servers. For clarity purposes let’s find out what hosting servers are:
A hosting server is dedicated to hosting a service or services for users or companies which can be websites, web applications, microservices, APIs, etc. Hosting server is a key component of a Web Hosting Service. It is most often built, delivered and managed by a hosting service provider. This is where Heroku comes in.
WHAT IS HEROKU?
Heroku is a cloud platform that lets companies build, deliver (deploy), monitor and scale apps. Heroku has been around since 2007, consistently helping developers and companies deploy apps faster without worrying about infrastructure.
“Heroku is the quickest way for a company to become an apps company. Heroku is a service that enables companies to spend their time developing and deploying apps that immediately start producing value.”
Personally I love using Heroku to deploy my apps and APIs but sometimes deploying to Heroku can be a pain in the ass most especially if you have no experience around it and this is the focal point of the this article; we will focus on how to deploy Laravel application to Heroku.
So let’s get started right away shall we?
DEPLOYING YOUR LARAVEL APP TO HEROKU
The first step in all of this is to first create an account on Heroku and creating an account on Heroku is one of the simplest thing to do. Click Here to create a free account on Heroku. You can skip this step if you already have an account with Heroku.
In this step we are going to create our application by giving it a unique name. To do this, login to your newly created Heroku account. From your account dashboard, from the upper right click the ‘New’ button and select ‘Create new app’, which will take you to a new page as shown in the image below:
From this page you are expected to enter a unique name as the app name though Heroku is intelligent enough to know if the name you entered is valid; you should be able to see this when typing the app name.
In this step we will be doing some basic but important settings on Heroku that will make our application work. To start with let’s first link our application repo hosted on GitHub with Heroku. This is important because we want Heroku to automatically deploy our application each time we perform a push to the branch we set on Heroku.
In order to achieve this, click on the newly created app on your account dashboard, which will take you to the app overview page like the one displayed in the image below:
Click on the ‘Deploy’ link from the navigation menu, under the ‘Deployment method’ section of the page and select ‘Github (Connect to Github)’ as shown in the image below:
Under the ‘Connect to Github’ section there is an input form field that allows you to search for a repo name on your Github account. In most cases after you register on Heroku, Heroku automatically check Github with the username you registered with if it has an account and then integrates it with your Heroku account. Now head on to the input field and type in your application repo name and then click on the ‘Search’ button as shown in the image below:
If Heroku finds the repo that matches the one you searched for then you should see a similar result like the one displayed in the image below:
Click on the ‘Connect’ button to link the repo to Heroku. That been done, let’s now tell Heroku to automatically deploy our application each time we make a push to the branch we specify. To do this, scroll to the ‘Automatic deploys’ section, choose a branch you want Heroku to automatically deploy from each time there is a push, then click the ‘Enable Automatic Deploys’ button as shown in the image below:
In this next step, we are going to add an add-on to our app to make it connect to a database. This add-on is called ‘ClearDB MySQL’ because our application is using the MySQL database technology. To achieve this, click on the ‘Resources’ link on the navigation menu; then under the ‘Add-ons’ section type in ‘ClearDB MySQL’ on the search input field as shown in the image below:
Click on the search result and a modal will pop up just like the one shown below:
Then click on the ‘Provision’ button to add the ClearDB MySQL add-on to your application.
Please note that Heroku will require your credit card details to complete this process if you are doing it for the first time. Click on this link for more details. Heroku Billing
The last thing for us to do before will push our application to Heroku is to add a Procfile.
Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. You can use a Procfile to declare a variety of process types, including:
- Your app’s web server
- Multiple types of worker processes
- A singleton process, such as a clock
- Tasks to run before a new release is deployed
In our own case we will use the Procfile to specify our app’s web server. The Procfile is always a simple text file that is named Procfile without a file extension. For example, Procfile.txt is not valid. The Procfile must live in your app’s root directory. It does not function if placed anywhere else.
Go to your application’s repo on Github and create a new file called ‘Procfile’ and paste the code shown below inside the file:
Then commit your changes and merge it to the branch registered on Heroku. After merging Heroku will automatically build and deploy our app to production.
To confirm this, click on the ‘Activity’ link on the navigation menu on Heroku and you should see the most recent activity performed and the status either successful or failed as shown in the image below:
If the deployment was successful then you can see your newly deployed app by clicking on the ‘Open app’ button.
This is the last step in the deployment process though it is not a compulsory one; its all depends on your application. In this step I will show you how you can make Laravel to connect to your Heroku database using the .env file in your Laravel application.
Remember we added an add-on called ClearDB MySQL, which automatically created a database for our application; but we need the database connection details like the name, password, host, and username. So how can we get these details?
Click on the ‘Settings’ tab on the navigation menu on Heroku, click on the ‘Reveal Vars’ button, a link will be displayed like the one displayed below without the colors:
The text cover with red color is the database username, while the text covered with black color is the database password, while the text covered with yellow color is the database host, and the text covered with green color is the database name.
You can now go ahead and replace the database connection details on your .env file to match with the one gotten from the Heroku database as shown in the image below:
Commit and push the changes to your repo and Heroku will automatically deploy your application when a merge occur.
Please note that when you want to work on the application locally, you can switch back to your localhost database details.
So with this, we have come to the end of this tutorial on “how to deploy your Laravel application to Heroku the easier way” and I hope this helps someone who is still struggling with deploying Laravel applications to Heroku most especially young and upcoming developers.
Thank you for taking your time to read this article.