Heroku is a cloud platform as a service(PaaS) supporting several programming languages. Heroku, one of the first cloud platforms, has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go.
There are many benefits of choosing Heroku platform to deploy your web applications over other traditional hosting platforms. Some of them being: more developer friendly, free for start, extremely easy to start: you don’t need to deal with the infrastructure, scaling is straightforward – just specify the number of processing units (dynos) you need and you’re set and so on.
- A working Laravel Web application (may or may not be making use of a database). If you haven’t already setup an app, you can follow this post to setup a basic Hello World app.
- Git installed on your machine and Github account. If you have not done this, have a read here
Please make sure you have the above and lets get started with Heroku now.
Setting up Heroku
Please go to Heroku and sign up for a free account. You will be asked to confirm your email, so make sure you have an accessible email account filled in there. After you have signed up, log in to your Heroku account. You will be redirected to their dashboard which is fairly easy to use.
The next step would be to install the heroku CLI (Command Line Interface) which we will be using later on. You can install the Heroku CLI on Windows, Mac or Linux. I have a windows machine, so I will be talking about that but if you have a Linux or Mac, you can read here to install the heroku CLI.
For windows users, you can directly download the 32 or 64 bit executable installer from here and install the CLI.
Once you install the Heroku CLI, you will be able to execute Heroku commands like :
which is also our next step. So open you command prompt on windows, bash or terminal on your mac or linux and run the above command. You can open cmd on Windows by pressing Windows+R and typing cmd and pressing enter.
Running this command will open a new tab or window on your browser, which will prompt you to log you in, just press login there. You should be able to see something like this:
Now you can either use the Heroku GIT to directly push your laravel web application to Heroku or you can setup a remote repository (Github or Gitlab or Bitbucket repository) to push to the Heroku.
Either one is do-able but I will be talking about the later one. If you have not pushed your Laravel web app to a remote git repository you can follow this post to do so or run the following commands on the root directory of your laravel web app(use git bash on windows):
git init git remote add origin YOUR_GITHUB_REPO_CLONE_ADDRESS git add . git commit -m "YOUR_COMMIT_MESSAGE_HERE" git push origin master
If you did not understand above code, I highly suggest you read our Git and Github post.
Once you have the laravel web app pushed to you Github, you can follow the following steps:
A Procfile is a file that specifies the commands that are executed by the app on startup
Create a new file named “Procfile” without any extension on the root directory of your laravel project. In the procfile write the following line:
web: vendor/bin/heroku-php-apache2 public/
This basically tells the Heroku app to map our Laravel’s public/ folder to vendor/bin/heroku-php-apache2 directory which is the Linux directory where our app will be hosted. So save the procfile and push it to your github repository.
git add Procfile git commit -m "Create profile for heroku" git push origin master
Create app on Heroku and setup
github repository to deploy to Heroku
On your command/terminal type
which will create a heroku app on your heroku account, this will be the place for your web application. You can access your apps from heroku dashboard. Select your app from the dashboard and go to Deploy tab. Under deployment method, select Github and choose the app to connect from App connected to GitHub section. Under Automatic deploys section choose the “master” branch. This will setup the master branch of our github repository to be automatically deployed to Heroku.
Once you have completed the above steps you will be able to see something like below in your Heroku:
And in your Github repository, you will be able to see “1 environment” which is the confirmation that your app is deployed to Heroku:
You also need to setup some environment variables on the heroku app to get your laravel web app running. You can do so by running the following command:
heroku config:set APP_KEY=PASTE_APP_KEY_FROM_ENV_FILE
Replace PASTE_APP_KEY_FROM_ENV_FILE with the key from your .env file in your laravel project
You can also run the following command to fix your APP_URL so that your UI will display alright:
heroku config:set APP_URL=https://YOUR_HEROKU_APP_ID.herokuapp.com/
You can see YOUR_HEROKU_APP_ID from your dashboard and is generally a fun name with some numbers attached to it. Mine is stark-cove-11076
Once these are setup you will be able to access your laravel web application from the Heroku dashboard or you will be able to access your app live from the url:
Your laravel web application is not pushed to github repository whose master branch will be automatically deployed to Heroku. We have not yet setup a database on Heroku and your web app may not function correctly if some pages make use of database, like after the registration form is filled and sign up is clicked.
I will be covering how to setup PostgreSQL on your Heroku. Let me know if you would like me to cover that and do comment or contact me if you got stuck doing this or you did not get any part of this post. I have also manged to run some commands on the Heroku bash to create symlinks and to generate keys for passport, which was particularly fun and exciting. Thanks.