I occasionally work with Laravel. Most of them these are small projects I work on my free time. So, whenever I want to deploy the app to Heroku I go through this painful process which takes much of time.
Since I need to do so many fixes for each deployment, I don’t even remember the process and need to google for every error code I face. This blog is a step by step process ( for my reminder as well ) that you can use to deploy Laravel with ease.
For those who don’t know, Heroku is cloud platform as a service platform where you can deploy web based applications.
One of the main reason I use Heroku is that it allows me to host small projects/prototypes for free. Most of the apps that I use are to show to my friends and get feedback.
Steps for Deployment
Heroku Toolbelt is a tool for creating and managing Heroku applications from command line . Depending upon your operating system, download Heroku Toolbelt. Now, go to heroku.com and create an account.
Then go to CLI/Git/Heroku-Toolbelt and enter the below code to login to your heroku account.
I personally prefer creating app using the web. I can instantly see if that name is available rather just hitting the key to find a good name.
Go to Heroku Dashboard to create a new project.
Heroku, by default will run an Apache server with PHP which points to root directory of the project. Since we are using Laravel, we need to add a Procfile to serve the site from /public directory. Go to command line again and paste this. This will create a Procfile with the proper content.
echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
4. MySQL Setup
I use MySQL with Laravel. Other Databases are also supported but I don’t know them.
For MySQL, there is a free addon on Heroku called ClearDB. Go to your project and on command line enter:
heroku addons:add cleardb
Now, Heroku will add ClearDB addon to the app and gives the database URL as an environment variable. We can then parse that url to get host, username, password and other server credentials. So, go to config/database.php and find ‘mysql’ in connection array. Now change the value:
'mysql' => [ 'driver' => 'mysql', 'host' => parse_url(getenv("CLEARDB_DATABASE_URL"))["host"], 'database' => substr(parse_url(getenv("CLEARDB_DATABASE_URL"))["path"], 1), 'username' => parse_url(getenv("CLEARDB_DATABASE_URL"))["user"], 'password' => parse_url(getenv("CLEARDB_DATABASE_URL"))["pass"], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ],
Since the things are App Key and App URL are set using .env file on local won’t be available on Heroku. We need to set those variables by running below codes:
Generate APP_KEY using “php artisan key:generate”
Copy the generate key and now set the APP_KEY for Heroku app using:
heroku config:set APP_KEY=XXXXX
For APP URL, go to the Heroku dashboard and copy the address assigned for your app. Then set the variable as below:
heroku config:set APP_URL=xyz.herokuapp.com
I’m not going into detail for Git here.
git push heroku master
Now this code will push your app to Heroku repo if you set the remote as per the instruction on your dashboard.
After pushing the code to Heroku, now you need to create database tables. For that, we are using the same php artisan command which needs to be tweaked a little bit to run in Heroku Cli.
Migrate Database on Heroku
heroku run php artisan migrate
Basically, whatever command you want to run add
before it and you are ready to go.