DEV Community

Cover image for How to Host a Laravel App with MySQL Database on Heroku
Chinedu Orie
Chinedu Orie

Posted on

How to Host a Laravel App with MySQL Database on Heroku

This article outlines a step-by-step approach to hosting a Laravel App on Heroku. It also explains how to make your app on Heroku work with MySQL database since ordinarily, Heroku does not make provision for MySQL. I saw the need to write about this topic after undergoing a series of challenges to host my Laravel test app with MySQL database on Heroku.

Requirements

In this article, I assume that you already have your Laravel app ready for deployment. The following tools will be required:

  • Command prompt for windows or its equivalent for other Operating System users.
  • A Heroku account. If you don’t have one click here to create an account Heroku account. If you don’t have one click here to create an account
  • Heroku command-line interface (CLI), Heroku has its CLI for interacting with it. You can download one here

Intro

Heroku is one of the platforms as a service that offers developers free hosting services. This enables a developer to test the performance of his/her application before moving it to the final production environment.

One of the drawbacks with this platform is that it does not offer MySQL Server by default but of course, there’s a way around it as there are other platforms that also offer free Mysql servers such as db4free (which will be used in this article) or ClearDB addons which complements this shortfall.

Follow along as I explain how you can host your Laravel app on Heroku and using the MySQL Server service from other platforms.

Step 1

Open the command prompt. Navigate to the directory where your Laravel project is located. (Note git bash does not support Heroku CLI). Login to your Heroku account by running;

heroku login

Enter fullscreen mode Exit fullscreen mode

Step 2
Run the following commands:

git init  //to initialize a new repo
git add --all 
git commit -m "my first commit"
Enter fullscreen mode Exit fullscreen mode

Step 3

Create a Procfile. Heroku launches an Apache server with PHP to serve the app from the root of the project which in our case is the public/ folder. A Procfile is required to configure it.

Create a file at the root of your project and name it Procfile.
Note no extension is required and the letter P must be uppercase. Copy the string below and paste in the Procfile you created and save it.

web: vendor/bin/heroku-php-apache2 public/
Enter fullscreen mode Exit fullscreen mode

The Procfile should look like the screenshot below:

Image of procfile

Alternatively, you can run the command below to create the Procfile:

echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile
Enter fullscreen mode Exit fullscreen mode

Then run,

git add Procfile
git commit -m "Heroku Procfile"
Enter fullscreen mode Exit fullscreen mode

Next is to create the new Heroku app.

Step 4
To create your heroku app run;

heroku create <myappname>
Enter fullscreen mode Exit fullscreen mode

Note <myappname> above serves as your domain name. If you don’t add it, Heroku will generate an app name for you.

Next is to specify the language of the app as PHP.

Step 5
To create a buildpack that will specify PHP as the app language run;

heroku buildpacks:set heroku/php
Enter fullscreen mode Exit fullscreen mode

Next is to generate an app key for the Laravel app on Heroku environment.

Step 6
Run php artisan key:generate --show

Copy the generated key and run the command below setting the APP_KEY value to the key you copied.

heroku config:set APP_KEY=<key generated above>
Enter fullscreen mode Exit fullscreen mode

We need to set the error log to Heroku error log.

To do that, go to config/app.php and set "log"=>"errorlog".

With this, you can view your app error log on your Heroku dashboard. Save and git add and commit.

It’s time to host the app! Grab a cup of coffee ☕️, take a sip and move on to the next step.

Step 6

To host the app to heroku, run

git push heroku master --app <myappname> 
Enter fullscreen mode Exit fullscreen mode

Note: the part is --app <myappname> only necessary if you have more than one app hosted on Heroku.

To launch the app from command line run

heroku open --app <myappname>
Enter fullscreen mode Exit fullscreen mode

If your only goal is to host your Laravel app without a database, then you can ignore the next section and jump to the conclusion.

Part 2: Integrating Mysql Database to the App

Our next target is to add a Mysql database to our app. There are several sites that offer free Mysql server. But for this article, we’ll be using db4free.

The steps are as follows:

Sign up on db4free. After signing up, you’ll be prompted by mail to verify your email. After verification, you’ll be redirected to a page where you’ll see the following data:

Database: [database name you specified]
Username: [username you specified]
Email:    [your email]
Enter fullscreen mode Exit fullscreen mode

Step 2

Go to your Laravel project and navigate to config/database.php and update it with the db4free MySQL database details you created earlier as shown below:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'db4free.net'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'your_database_name from_db4free'),
            'username' => env('DB_USERNAME', 'your_db4free_      username'),
            'password' => env('DB_PASSWORD', 'your_db4free_password'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
Enter fullscreen mode Exit fullscreen mode

Save it. git add , commit and push to Heroku.

git add config/database.php
git commit -m "update database"
git push heroku master --app <myappname>
Enter fullscreen mode Exit fullscreen mode

Step 4

You need to use the Heroku bash in order to run your migrations. Run,

heroku run bash 
Enter fullscreen mode Exit fullscreen mode

Next is to run your migrations, ensure you're now running subsequent commands on the Heroku bash.

php artisan migrate --app <myappname>
Enter fullscreen mode Exit fullscreen mode

At this point, your database is set. If you want to seed the database then you can proceed to the next steps as follows:

Step 5

php artisan db:seed --app <myappname>
Enter fullscreen mode Exit fullscreen mode

If you encounter an error similar to the screenshot shown below, do not panic, you've got covered. 😌

Screenshot showing error

To fix this, follow the steps below:

  • Go to your laravel project, navigate to composer.json
  • Replace "fzaninotto/faker" of "require-dev" with "require"
  • Exit the heroku bash by running exit
  • Git add and commit the changes to your project
git add composer.json
git commit -m "modified composer.json"
git push heroku master --app <myappname>
Enter fullscreen mode Exit fullscreen mode
  • Go back to Heroku bash,
  • Runheroku run bash
  • Run composer install
  • After the composer has finished installing the necessary dependencies, proceed to migrate and seed your database by running the commands below:
php artisan migrate:refresh --seed --force
Enter fullscreen mode Exit fullscreen mode

Boom! everything now works fine! 👯‍♂️

Conclusion

You have now learned how to host your awesome Laravel app on Heroku. You have also learned how you can incorporate MySQL server to enable your app work with the MySQL database. If you'd want to learn more about Heroku, you can visit Heroku Docs. If you also want to learn about auto-deploy to Heroku from Git click here.

Thanks for taking the time to read, I do hope the article has helped you!

Feel free to reach out should you have any question or a contribution that will make this article better for the community.

This article was originally published on medium

Top comments (8)

Collapse
 
yashamihsan profile image
yashamihsan

when I do git push Heroku master, after pushing it start installing platform packages then I got this error

@php artisan package:discover --ansi
remote:
remote: In app.php line 29:
remote:
remote: syntax error, unexpected ''env'' (T_CONSTANT_ENCAPSED_STRING), expecting ']
remote: '
remote:
remote:
remote: Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
remote: ! WARNING: A post-autoload-dump script terminated with an error
remote:
remote: ! ERROR: Dependency installation failed!
remote: !
remote: ! The 'composer install' process failed with an error. The cause
remote: ! may be the download or installation of packages, or a pre- or
remote: ! post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
remote: ! in your 'composer.json'.

Collapse
 
bijayhash profile image
bijay-hash

did you find the solution i am having same issue please share your solution

Collapse
 
myehiz profile image
Ehiz Michael

Thanks.
Followed every step. But i get a 500 server error on opening the heroku app

Collapse
 
nedsoft profile image
Chinedu Orie

You could try this couple of commands to debug the problem.

heroku logs --tail -a your-app-name // to view the error logs

heroku ps:exec -a your-app-name // to access your codes in the server

Collapse
 
myehiz profile image
Ehiz Michael

Okay

Collapse
 
maximabdalov profile image
Максим

Instead place db password in config.php in step 2 better place db config params in heroku config:set DB_PASSWORD=your_password

Collapse
 
yvan99 profile image
Ishimwe Yvan

Thanks mn ,
if you're still getting the 500 server error you may try this
webune.com/forums/lzpyzz.html

Collapse
 
donphelix profile image
Don Phelix

Thank you man, You have just saved my day.