How to deploy Laravel Application on Dokku

Laravel 5.4

Requirements:

In order to deploy a laravel application on dokku, you first need to install dokku on your VPS.
Follow the instruction on the link below to install dokku on your VPS.

Dokku Installation

After installation, follow the following instructions.

1. Creation of Laravel Project.

To deploy an application, we obviously have to create the application unless already done.
To create a laravel application, we first need to install composer (PHP Package Manager).
To install composer, go to the following link and follow the instructions:

Composer Installation

Then run the following command to be able to run composer as a command:

 mv composer.phar /usr/local/bin/Composer

Remember that the above command is ran on a LINUX based OS

The next step is to create our Laravel Project called blog with the following command:

 composer create-project --prefer-dist laravel/laravel blog

2. Creating the ‘Procfile’ (Process File).

The ‘Procfile’ (capitalization matters) is necessary to the deployment of the application. It tells the dokku Server how to run the application.
Since we are running a Laravel Project, we have to tell dokku how to run application by specifying the command to run in the “Procfile”.
Run the following commands to create and fill the file:

 cd blog
 echo 'web: vendor/bin/heroku-php-apache2 public/' >> Procfile

3. Creating the Application on dokku VPS.

In order to deploy any application on dokku , we first need to create the app on the VPS.
First, Login to your VPS as root and run the following commands:

 dokku apps:create blog

Then specify the web server type that should be used based on the Programming language used in the application.
For Laravel the is a PHP framework, run the command below:

 echo 'web: vendor/bin/heroku-php-apache2 public/' >> Procfile

4. Configuring the BUILDPACK to be used.

In order to build the application with right configuration , we need to tell dokku which buildpack should be used.
For doing this they are two ways:
– Create a .buildpacks file in your project root folder and specify the buildpack in it.

 echo "https://github.com/heroku/heroku-buildpack-php" >> .buildpacks

– Set BUILDPACK_URL environmental variable with dokku config on the VPS.

 dokku config:set blog BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php

For more buildpacks visit:

Heroku Buildpacks

5. Deploy the Application on dokku VPS with git.

First, we initialize git into our project with the following command:

 git init
 git add ./

For the following command, replace your_domain by the IP address of your VPS.

 git remote add dokku dokku@your_domain:blog
 git commit -m "Initial commit"

For the registration of the app on dokku Server, copy the value of APP_KEY variable in the .env file located in the root directory of your Laravel Project. Then, login to your VPS and register the APP_KEY environmental variable for your app:

Replace “{{ key }}” with the you copied earlier

 dokku config:set blog APP_KEY={{ key }}

It should look like this:

 dokku config:set blog APP_KEY=base64:zXHpOcdQVc4bVUo28O1nFb0j1g1LB5Zv21IMHiZeKM8=

6. Finally push the Application on dokku VPS and migrate the Database if needed.

To push the application on dokku , run the following command on yout local machine (in the root folder of your application):

 git push dokku master

Check if the appropriate DB plugin is install on dokku. If YES proceed, unless

Click here to install MySQL Database Service on dokku (Backend Services)

Then, we create the DB and service (on VPS):

 dokku mysql:create blogdb

Then link the new DB service to our application :

 dokku mysql:link blogdb blog

Then set the DB service credential to the application environmental variables: – First, copy the value gotten from the following command:

 dokku mysql:info blogdb --dsn

– Second, set the DATABASE_URL variable with the value gotten from the previous command:

 dokku config:set blog DATABASE_URL={{ value }}

Then, go back to your application on local, and modify the config/database.php file in such a way that the mysql section under collection looks like this:

'mysql' => [
'driver' => 'mysql',
'host' => parse_url(getenv("DATABASE_URL"))["host"],
'database' => substr(parse_url(getenv("DATABASE_URL"))["path"], 1),
'username' => parse_url(getenv("DATABASE_URL"))["user"],
'password' => parse_url(getenv("DATABASE_URL"))["pass"],
'port' => parse_url(getenv("DATABASE_URL"))["port"],
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

Then, run the following commands to update the application since we modified it:

 git add ./
 git commit -m "Modified config/database.php file"
 git push dokku master

Finally to migrate the database, run the following command (on VPS):

 dokku run blog php artisan migrate

7. Browse and Enjoy your Application.

Just browse the link given to you at the end of the ‘git push dokku master’ command.

Don’t forget to like and share.

You can email us if you need assistance.

Leave a comment