To Deploy Laravel applications to a cloud-based hosting platform can be intuitive and tedious sometimes especially if you are a beginner into the nitty-gritty of cloud technologies and cloud base hosting like Heroku, DigitalOcean, etc.
I have to deploy Laravel countless times, I have learned numerous tips and tricks, best practices in Laravel deployments, that’s why I will be sharing my experiences in deploying your Laravel project using Heroku as your cloud hosting service.
In this article, I will explore everything you need to know to deploy your next Laravel project to any Cloud Based Hosting Provider of your choice.
So if you sure want to learn from my experience and deploy the right way:
You might also want to look at Deploying Laravel to Shared Hosting, you might need it someday, who knows.
Let’s dive right into it:
Create a Laravel Project
Create a Procfile
Initialize Git
Before Deployment
Best Practices
Create an App in Heroku
Set up an Encryption key
Set up .env credentials
Before you dive in, if you’re a backend developer or looking at delving into this career path, join other developers to receive daily articles on backend development that will boost your productivity.
Create a Laravel Project
As always, the first thing you need to do is create a brand new Laravel project either by following this article on Laravel Framework: The Ultimate Guide or simply follow the official documentation.
If you have already created a project, let’s dive into deploying that project to a cloud-based server.
Create a Procfile
By default, Heroku serves the root directory when launching the Apache Server, but with Heroku Procfile, we can specify the file and directory we want Heroku to load our Laravel project from:
Create a Procfile and add the following line of code.
touch Procfile
Add this line to the file:
web: vendor/bin/heroku-php-apache2 public/
Initialize Git
Sometimes, creating a Laravel project might come with Git initialized, but if that’s not the case, we need Git to be initialized and properly set up to aid us to deploy our Laravel project efficiently.
Now, let’s initialize git by running the following command in the root directory of our project:
git init
Make sure to download and install Git from here.
Before Deployment
Before every deployment with Laravel, there are couple of deployment artifacts you need to observe before sending your files to the server.
In this section, I will list out a few and also show you how to do it:
Autoload Optimizer
When deploying a Laravel Project, make sure the Composer’s class autoloader map is optimized properly.
According to the official documentation, this will help Composer to find the appropriate class to load for a given name.
You can easily achieve this with the following command:
composer install --optimize-autoloader --no-dev
Optimizing Configuration Loading
Before deploying your Laravel Project, make sure to clear every configuration caches that Laravel has created during development.
Clearing your cached files during development is very important before deploying your Laravel Project. It can be done with this command.
php artisan config:cache
Optimize Routing
Also, make sure to optimize your routes, if you’re building a very large application with many routes, this command can boost performance by reducing all the route files into a single cached file.
You can optimize your routing system using the following command:
php artisan route:cache
Optimize View
Lastly, you need to also optimize the views of your Laravel Project, again, if you are building a large project, optimizing the view can result in performance gain because the command precompiles all your views so that they are not compiled on demand.
php artisan view:cache
Before we move on, just as a bonus for reading this article, Laravel provides a command to run all the above commands in a single command.
php artisan optimize
//Or
php artisan optimize:clear
Best Practices Before Deployment
Below are lists of best practices you need to observe before you deploy your Laravel Project.
There are many different specific best practices depending on the type of project and project sizes.
But I will list out a few general ones:
Change Log directory
As a best practice when deploying Laravel Project to Heroku, make sure to configure your log driver properly, so that Heroku can display your logs when you run the heroku logs --tail
command.
To achieve this, add the following code to your config/logging.php
file:
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
],
'single' => [
'driver' => 'errorlog',
'level' => 'debug',
],
Take a break and subscribe to get access to our free Laravel tips that will improve your productivity.
Create an App in Heroku
Creating a new Heroku app is simple as logging into the Heroku Dashboard and clicking on the Create a New App icon.
Pass in a unique name and click on Create App:
Heroku will present to you three methods of deployment which I will discuss in a bit.
Types of deployment method
Using Heroku Git
This method is simple and straightforward, with this method of deployment, you don’t need external git repositories like Github or Gitlab.
You simply push your finished project straight to Heroku just like the way you will push to Github and Heroku will handle the rest.
Let’s try this option:
Firstly, download and install Heroku CLI following these steps.
After installing the CLI successfully, you should have access to the heroku
command, use that command to log in to Heroku by typing in the following command.
heroku login
Next, run the following command to add Heroku repository to your local git remote repository.
cd my-project/
heroku git:remote -a <PROJECT_NAME>
where PROJECT_NAME is the unique name you passed in when creating the Heroku app.
Next, commit your project changes and push to the Heroku repository with the following commands.
git add .
git commit -am "Fixed for deployment"
git push heroku master
At this point, that’s all you need to do when deploying your Laravel Project to Heroku using this method.
For subsequent deployments either for bug fixes or new features, just repeat the last step of committing and pushing your changes to Heroku master and the deployment will start automatically.
Using Github Option
This option is my most used option, as it allows me to also use all GitHub features including Github Actions.
First, switch to the Github option and connect your Github account, if you haven’t done that already.
Once your Github account is connected, you should be able to select any repository you wish to deploy by typing in the name of the repository in the box provided.
However, if you don’t already have a repository for the project you want to deploy, you can simply create and add the remote URL to your local repository as Origin.
Next, we will search for and connect to the right repository to deploy in Heroku Dashboard under the Github option.
After connecting to the repository, we will choose the branch we want to auto-deploy, in my case, it will be master
or you can select main
if your project uses that as the master branch.
If you set up Auto Testing with Github Actions, you need to check the box for “Wait for CI to pass before deploy”. This will allow your Github actions scripts to pass before you deploy your Laravel project.
Next, click on Enable Automatic Deployment.
Move down and select the branch you want to deploy now and click on the Deploy Branch button.
Heroku will handle the deployment process for you and if you’re successful with your setting and configuration, your project will be live on the URL presented.
You might notice some errors if you try to visit the URL, which may be caused because we haven’t added any .env
credentials yet.
Set up .env credentials
Adding our .env
credentials to Heroku is very simple and can be done in two ways:
Using CLI
This is the shortest path as it involves using the terminal and typing in the following command:
heroku config:set CREDENTIAL_KEY=CREDENTIAL_VALUE
// e.g.
heroku config:set DB_PORT=3306
Repeat the command until you finish all your credentials.
Set up Encryption key
Setting up Laravel Encryption Key is very important and must be present before Laravel can even work.
We can easily set this up using the CLI like below:
heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)
Using Heroku Dashboard
This option involves logging into Heroku Dashboard and navigating to the Setting page of your specific project.
This option is great for our non-terminal freaks
Scroll down to the Review Config Vars button and start adding your credentials one after the other.
You can set up your Encryption Key by copying the APP_KEY value and pasting it in the APP_KEY column respectively.
That’s all you need to do:
Conclusion
There you have it, we explored how to deploy Laravel to Heroku, the best practices, and what to do before deploying Laravel.
If you insert the correct credentials and follow the steps appropriately, you should have your Laravel Project deployed successfully.
You might also want to look at Deploying Laravel to Shared Hosting, you might need it someday, who knows.
If you find any step difficult or you need more explanation, let me know in the comment section below.