<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joel Okoromi</title>
    <description>The latest articles on Forem by Joel Okoromi (@okmarq).</description>
    <link>https://forem.com/okmarq</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F538966%2F8380114d-a232-41c4-81fd-79641236dd17.jpeg</url>
      <title>Forem: Joel Okoromi</title>
      <link>https://forem.com/okmarq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/okmarq"/>
    <language>en</language>
    <item>
      <title>Deploying a Laravel application to Heroku with a MySQL database</title>
      <dc:creator>Joel Okoromi</dc:creator>
      <pubDate>Mon, 18 Jan 2021 14:46:22 +0000</pubDate>
      <link>https://forem.com/okmarq/deploying-a-laravel-application-to-heroku-with-a-mysql-database-1gi3</link>
      <guid>https://forem.com/okmarq/deploying-a-laravel-application-to-heroku-with-a-mysql-database-1gi3</guid>
      <description>&lt;h1&gt;
  
  
  Deploying a Laravel application to Heroku with a MySQL database
&lt;/h1&gt;

&lt;p&gt;This tutorial will enable you to deploy your Laravel 8 application with a MySQL database to GitHub and Heroku, saving you the time to deal with subsequent errors you might encounter if you were to follow their usual tutorials. as they skip over some little however important detail. that decides the success of your deployment.&lt;/p&gt;

&lt;p&gt;I will assume you already have the Laravel installer and composer installed on your machine as well as a web server like Xampp, then you should also have a GitHub account and git installed, it comes with the bash CLI which I recommend you use for this tutorial.&lt;br&gt;
Finally, have a Heroku account then install the Heroku CLI. they are the prerequisites for this given tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Laravel section
&lt;/h2&gt;

&lt;p&gt;1   in your command line, run "laravel new apoku --jet" and follow the command prompt.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ laravel new apoku --jet

a   choose your desired templating engine e.g livewire or inertia
b   choose if you will be using a team or no team
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;2   if you chose inertia then npm install &amp;amp;&amp;amp; npm run dev will be done automatically for you so you can skip it else you run the command below one after the other has been completed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install
$ npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3 next create a database with a name in the phpmyadmin panel that corresponds to your app name in my case it will be "apoku" then run the command below, this will migrate all your base database information. without a database laravel applications won't work which is one of the main reasons laravel applications are difficult to deploy and set up on Heroku&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;by now you should see your laravel application active on your local development environment.&lt;br&gt;
now it is time to deploy your project on GitHub then subsequently on Heroku.&lt;/p&gt;

&lt;h2&gt;
  
  
  The GitHub section
&lt;/h2&gt;

&lt;p&gt;4 the first step is to initialize your project environment with the command below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;5 next add all your files using the following command "git add ." or "git add file.ext" if an individual file {read best practices on how to use git commands}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;6 you need to commit your changes next with the command below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git commit -m "first commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;7 add a branch to your local project&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git branch -M main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;8 if you haven't already created a repository on GitHub then now would be the best time [actually the last time] to do so to move forward, when you're done you copy the repo link then run the command like below [note: I use ssh so your link might be different. my apologies, I won't be covering the differences from here]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git remote add origin git@github.com:okmarq/apoku.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;9 then upload your code to GitHub with the command below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push -u origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;at this point, we are ready to initialize our Heroku environment for deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Heroku Section
&lt;/h2&gt;

&lt;p&gt;10 the first step here is to sign in to Heroku via your CLI with the command below, follow the given instructions in the command prompt&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;11 next you create a Heroku app with your project name like below or leave it blank for Heroku to give you a randomly generated project name [your project name should be unique or it will be rejected]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku create apoku
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;12 your next step is to push your code to Heroku where the build pack will be automatically detected and installed for you. in our case php&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;opening the app on Heroku at this stage will give us a forbidden output as we don't have a Procfile.&lt;/p&gt;

&lt;p&gt;13 in this case you create add a Procfile with no extensions then add the following line for an apache server "web: vendor/bin/heroku-php-apache2 public/" to the file as the content then stage, commit and push the changes to Heroku. &lt;br&gt;
now you should see an "HTTP 500 Internal Server Error".&lt;br&gt;
you should take note of the "public" in the Procfile, Heroku might give you "web" which is just a generic indication, this will lead you to see an application error which from here on out, any other error will now begin to look painful to fix which is the very reason for this tutorial itself so follow it closely.&lt;/p&gt;

&lt;p&gt;14 add a database from your Heroku dashboard. specifically, use a JawsDB MySQL addon. the reason for this is so you can have easy access to all the separate parameters we will be needing to connect to the database, the Host, Username, Password, Port, and Database.&lt;/p&gt;

&lt;p&gt;15 your next step is to use your CLI to add the following parameters to your Heroku config vars. run the following commands below one after the other has completed&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku config:set APP_NAME=apoku

$ heroku config:set APP_KEY=base64:138V/prcrPWnpMdyl9kPmRPug4s133odCrWuFl/KOas=

$ heroku config:set APP_DEBUG=true

$ heroku config:set APP_URL=http://apoku.test

$ heroku config:set DB_CONNECTION=mysql

$ heroku config:set DB_HOST=j21q532mu148i8ms.cbetxkdyhwsb.us-east-1.rds.amazonaws.com

$ heroku config:set DB_PORT=3306

$ heroku config:set DB_DATABASE=kvqb74eik8x9hi68

$ heroku config:set DB_USERNAME=hksxxqiwg639dry3

$ heroku config:set DB_PASSWORD=oczq10yi11woi5e9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;you're almost there.&lt;br&gt;
16 your next step is to migrate your database. while I'll be using the CLI to do this step, you can do this directly from your heroku dashboard.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku run bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;upon running the above command, a bash interface will be activated after which you will run the following command in it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;very nice, running a bash interface from a bash cli. cool huh?&lt;/p&gt;

&lt;p&gt;well, congratulations on making it this far, you should pat yourself on the back, your application should be up and running right now, assuming you followed the instructions. &lt;/p&gt;

&lt;p&gt;If you still have an error and you are using a laravel application, then read through again, to ensure you used "public" and not "web" in your Procfile.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>mysql</category>
      <category>heroku</category>
      <category>deploy</category>
    </item>
  </channel>
</rss>
