How to Deploy a Laravel App to Heroku with Database Setup?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello guys, welcome back to our channel. Today in this video we are going to be looking at deploying Laravel application to Heroku platform. Heroku is a cloud platform as a service, supporting several programming languages. It allows us to create an app and you can customize exactly as you want. Heroku allows you to create free sites, which is really great if you starting out. Today in this video, we are going to push Laravel code to Heroku. We can either do this through GitHub or we can directly push to Heroku. So, Before moving head, If you are new to this channel, Hit the red subscribe button and press the bell icon. So you could never miss upcoming videos. The first thing that you're gonna want to do is obviously go ahead and register for a Heroku account. I have already created my account and I am inside Heroku Dashboard. Here you'll see something like this. You can see currently we have no apps. Most of what you do with Heroku, you can control within your dashboard, but we're actually going to be doing a lot of work on the command line. So, for that we need to install the Heroku Toolbelt. Click on this download and install link. As you can see, this works for all operating systems. I am using Ubuntu and I have already installed it, but if you are using Windows or Mac, this is going to be pretty much the same. Here on this page, it is very well documented steps to install Heroku toolbelt on all Operating system. You can download Heroku toolbelt from here and install it on your system. Once you have installed Heroku toolbelt, let's move to next steps. In the terminal, Now the first step is to run Heroku login. It will ask you to enter your Heroku email and password to login in your Heroku account. I am already logged in my Heroku account. So, I can skip this step. Now I am going to create fresh Laravel application. Lets name it Laravel57 because current stable version is 5 dot 7. cd Laravel57. Next, we want to push this Laravel app to Heroku. So, before that we will create a Procfile. We can either do this by just saying touch procfile on the command line, or you can just go ahead and create a new file. But here, there's no extension to this at all. It just looks like this. Inside of this procfile, we will define what our application runs on dynos. Dynos are basically just a little container within Heroku. So what I'm going to do is say, well I need some kind of web server and inside of this I want to say vendor bin Heroku PHP Apache two. Or you can say nginx if you want nginx server. But for now I am going to use apache2. This is just a predefined environment in Heroku. And then we say, it should run from the public directory. Now we need to push this up to Heroku. For that first we need to run git init, to initiazlise empty git repository. And this is very important, because the way we works with heroku. We commit changes with git and push this up to Heroku. And it will deploy our application. So, let's commit all of our changes. git add . git commit -m "initial release" Now how we gonna push this to Heroku. If we see on the Heroku dashboard, here we don't have any apps. So, our next step is to create a new APP in Heorku. So, for that in terminal, inside our project. We gonna run Heroku create. This will create a new APP for us. And you can see, this just gives us a random name. If we go into the dashboard and refresh, you can see that we now have this application in there. Click on this. Here we can manage it as we need. Heroku create command also adds git remote URL. We can verify it by running, git remote -v. Here it is. So, to push this Laravel app to Heroku. we can run git push heroku master. Here you can see a lot of output. Basically Heroku doing all of these things just to get our app set up and you can see here that the remote install of all our composer dependencies is happening. So we just wait a couple more seconds. Now we've actually deployed our code to Heroku. Let's take a look at the Heroku dashboard, just to see what the difference is now. Refresh. And we should see under dyno formation, just here that we have the web. Now, if we go and open our APP. We got 500 error. This is because we don't have any of environment variable set up on Heroku for Laravel app for example app key. You'll find these normally in your settings, and you can click reveal config vars. So this is where you would add config variables. You have key and value. So, first I will add, APP_DEBUG true. Now we come back and refresh this page. Now we can see, No application encryption key has been specified error. so, let's add app key. Refresh this page. It's done. Now it is working. Let's add other environment variables as well. We can also add environment variables directly from the command line using Heroku config add and then give the key and the value. In our case, APP_NAME Larave. APP_ENV, I will say it production. Next APP_URL, Our App url is this one. Next, variables are for the database connection. But we have not configured database in Heroku yet. So, lets configured database. So, in the Heroku dashboard, We will go to resources. To add a new database, we gonna use this addon here. There are so many add-ons you can explore. Here we gonna search for Postgres. You can see we have Heroku Postgres. Click on this. you can see here that we have plan Hobby dev free. If you want you can upgrade of course. For now, we are working with free stuff. So, lets go ahead provision this. In a few moments, it will be ready for us to use. Next, You can click on here to get the Postgres credentials. Or on the command line you can run, heroku pg:credentials:url And here we get Database name, host, port, username and password. And we will set up this in environment config variables. Now, we will add DB connection. Heroku config:add db_connection and that is pgsql. Because we are using a Postgres SQL. Next, we will add DB host, copy this from here. Similarly, we gonna add DB port, this one. next DB database next, we need db user name and last we will add db password. In Heroku setting page, here we can reveal config variables. And you can see configuration we added from the command line is here. If you need to modify any of these, then you can modify it from here. Now we have all database credentials set up. Next, we need to run artisan migrate command to create tables in Heroku Postgres database. There are a couple of ways to do this. we can say, heroku run and give the full command that we want to run. In our case, we can say php artisan migrate. Or we can say, Heroku run bash What this will do. It will put us into the command line for our Heroku server. So, when I list here, you can see our Larave application files. If you are running a lot of commands, then you can run Heroku run bash. But of just one command, It is very easy. We just to go and run Heroku runs whatever you want to run. and here we will say Heroku run php artisan migrate. It has created tables in database. Now to test it with Laravel app, let's run php artisan make:auth to generate general scaffolding for authentication system in the Laravel application. Authentication scaffolding generated. git status, This will tell us the status of general untrack files. Next let's commit this files. git add, git commit -m "authentication" Now let's push this to Heroku. git push heroku master. Now Laravel application is deployed. So, let's test it out. in browser, refresh this page. Here we got login and registration links. So, Let's start to register a new account and see what happens. Harish, Harish at example dot com password It just works as normal. And that is pretty much it. So, we now know, How to deploy Laravel application, setup database and adding configuration variables in Heroku. And we also know how to run commands using Heroku. If you like the tutorial hit the like button, share this video and don't forget to subscribe us. See you soon in the next upcoming video.
Info
Channel: QiroLab
Views: 75,033
Rating: undefined out of 5
Keywords: laravel 5, web development, heroku login, heroku procfile php, heroku laravel postgres, laravel free hosting, heroku cli download, deploying a laravel application, heroku build pack laravel, heroku run php artisan migrate, heroku php, heroku composer, deploy laravel to heroku with database, heroku deploy laravel application, laravel 5.7 tutorial, laravel 5.7
Id: 639Pe0PpVLQ
Channel Id: undefined
Length: 12min 11sec (731 seconds)
Published: Tue Jan 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.