Deploy A Laravel Project To Heroku

Follow these simple steps on how to deploy a laravel application to heroku/


1. Install heroku:
$ sudo snap install --classic heroku

2. Login to your Heroku Account
# heroku login

3. Create a Procfile in the project's root directory
$ touch Procfile

Add the following line to the Procfile:
$ echo "web: vendor/bin/heroku-php-nginx public/" > Procfile

4. Initialize the project with GIT. Install git if you don't have it already

$ git init

5. Create a Heroku instance of your application
$ heroku create NAME_OF_APPLICATION

Herokup Application Name must start with a letter, end with a letter or digit and can only contain lowercase letters, digits,and dashes.


NOTE: this will also be the name of the url, example: NAME_OF_APPLICATION.herokuapp.com
NOTE 2: The name of the application must be something unambigious otherwise you will get an error that says: Name xxx is already taken

you will see a message upon successful:
Creating ⬢ NAME_OF_APPLICATION... done
https://NAME_OF_APPLICATION.herokuapp.com/ | https://git.heroku.com/NAME_OF_APPLICATION.git

Open the url in your browser:
$ xdg-open https://NAME_OF_APPLICATION.herokuapp.com/

6. Now that you have your app setup in Heroku, lets push our app to Heroku, next lets commit our project into Heroku first.

$ git add .

$ git commit -m "Initial Commit"

$ git push heroku master

if this command doesn't work try this one:

$ git push heroku main

ERORR: error: src refspec master does not match any

To solve this error, you can use either of the following commands:

FIX: $ git push heroku HEAD:master

or

FIX: $ git push heroku main

Refresh your browser to https://NAME_OF_APPLICATION.herokuapp.com/
YOU will see a 500 Error!

You will see a 500 error. Don't worry, follow the instructions on the following page to fix it.

https://www.webune.com/forums/lzpyzz.html

7.

Continue with the next step to setup your database connection
For this example, we will be attaching a MySQL

a. Go to your browser and open the application we just created and click on the "Resources" tab
b. Click on the "Find more add-ons" button
c. Find "JawsDB MySQL" and click on it
d. In the JawsDB MySQL page, click on the "Install JawsDB MySQL" button
e. In the "App to provision to" field, enter the name of your application [NAME_OF_APPLICATION]
f. Click the "Submit Order Form" button
g. The new datbase has been attached to your app, you can confirm by going to the settings tab and click on the Reveal Config Vars button, you will see "JAWSDB_URL" in the KEY field
h. To get the credentials for the newly created database resource, in the dashboard, go to settings > Reveal Config Vars > JAWSDB_URL
you will see something like this: mysql://myddo8pxnb1n08o:pykdd8m4cro63fi@phtfddd4p6a970uc0.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/t587gxdd71dj04e9s9
To breakdown:
HOST: phtfddd4p6a970uc0.cbetxkdyhwsb.us-east-1.rds.amazonaws.com
USERNAME: myddo8pxnb1n08o
PASSWORD: pykdd8m4cro63fi
PORT: 3306
DATABASE: t587gxdd71dj04e9s9


8. Next, configure you application configurations

a. Open config > database.php
b. Setup a configuration variable for our database. Copy and paste the following below the line: use Illuminate\Support\Str;

$DATABASE_URL=parse_url('DATABASE_URL');

c. Find the MySQL section in the database.php file and change:
FROM:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

TO:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
'host' => isset($DATABASE_URL['host']) ? $DATABASE_URL['host'] : null,
'port' => isset($DATABASE_URL['port']) ? $DATABASE_URL['port'] : null,
'database' => isset($DATABASE_URL['path']) ? ltrim( $DATABASE_URL['path'], "/") : null,
'username' => isset($DATABASE_URL['user']) ? $DATABASE_URL['user'] : null,
'password' => isset($DATABASE_URL['pass']) ? $DATABASE_URL['pass'] : null,
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

d. Be sure to set the default database as MySQL
 'default' => env('DB_CONNECTION', 'mysql'),

e. Commit the changes:
$ git add .
$ git commit -m "Update Db Configs"
$ git push heroku master

ERROR: I got this error while rebuilding the app:
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'.

FIX: https://stackoverflow.com/questions/61837729/heroku-deployment-the-composer-install-process-failed-with-an-error

Commands To remove and reclone GIT repository

Access the Application Container Bash Shell Terminal:

heroku run bash

List all the current files:

ls

Remove/delete all the application files in the container

rm -rf ./*

List all the files again, you should see none

ls

Exit the Heroku Bash Shell:

exit

Rebuild the application in Heroku

git push heroku master -f

ERROR: If you still get an error, try this command:

heroku git:clone -a [APP_NAME]

You will have an app with a database connection.
Refresh your browser to https://NAME_OF_APPLICATION.herokuapp.com/

if you have migrations you can run php artisan in the heroku platform of you application with the following command:

$ heroku run php artisan migrate

 

 Do you really wish to run this command? (yes/no) [no]:
 > yes


Done

NOTE: if this is your first time using git in your computer, you will have to sign in with git also or else you will get an error message:
*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'xxxr@xxx-Docker.(none)')

A nice video for you.

skGZ8laUQco