Welcome to a series of Deploying a Laravel Application.
Laravel applications are deployed on many sites.
I will be taking you through on how to deploy a laravel application which has a database and to be specific, Postgresql Database.
- PHP (and ideally some Laravel) knowledge.
- A Heroku user account: SignUp is free and instant
- Familiarity with getting started with PHP on Heroku guide, with the following things installed: +PHP. +Composer. +Heroku CLI.
To have complete success, we would need the Heroku CLI in our local machine.
To do that we need to run this set of commands in our command-line which will be found here.
Let us embark on our main objective. So we have an application with has a database structure and has migrations with it.
So as to fit in your shoes I will be assuming you have an application is fully ready to be in production.
After installing the Heroku CLI you may do the following command.
$ heroku login
heroku: Enter your login credentials
Email [[email protected]]:
Fill in your credentials and we are now set to launch our production application.
We will head to the project which you want to deploy as shown below:
$ cd <application name>
We will then create or initialize a git repository and commit our current state
$git init
$git add .
$git commit -a -m "new Laravel project"
We will then add the remote git repo for the application by creating the application.
$heroku create <application-name>
This will consist of the git repo from heroku where we will host of application files.
To deploy your application to Heroku, you must first create a Procfile, which tells Heroku what command to use to launch the web server with the correct settings.
By default, Heroku will launch an Apache web server together with PHP to serve applications from the root directory of the project. However, your application’s document root is the public/ subdirectory, so you need to create a Procfile that configures the correct document root:
$ echo web: heroku-php-apache2 public/ > Procfile
$ git commit -a -m "Procfile for Heroku"
By default Laravel offers the environmental variables in a file called .env. We will need to send the information to the heroku servers. This is an example of how the env should look like
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
We will need to push the appropriate data to heroku using a regex that i found from a great developer called Jakhax
$ heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')
Remember to set your DEBUG
to false
so as to prevent leak of data.
When you open your heroku dashboard to the settings panel and click on reveal config values, it should look like this.
First, add the Heroku add-on for Postgresql. If you were using mysql I request you go to this link.
$ heroku addons:create heroku-postgresql:hobby-dev
you should see an output like this:
Adding heroku-postgresql:hobby-dev on app-name-here... done, v14 (free)
Attached as HEROKU_POSTGRESQL_COLOR_URL
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql` to view documentation.
As you can see the add-ons creates a database which is empty and doesn't contain any information. We would need to store information into it.
By default you should have DATABASE_URL
configuration created after installing postgres addon to heroku.
At this point your database should be up and running. Now, let's edit your Laravel config to point to the PostgreSQL database.
Once again, if this is real app, you're going to want to only be making these changes in your production configuration settings, but for now we're just hacking at a dummy app.
First, change the value of 'default' in app/config/database.php to 'pgsql'.
'default'=>'pgsql',
Set the following at the top of your database.php above the return values:
$url = parse_url(getenv("DATABASE_URL"));
$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);
Then change your pgsql entry in that same file to be the following:
'pgsql' => array(
'driver' => 'pgsql',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
That is it! Commit and we are ready to push our changes.
$ git add .
$ git commit -m "Convert to use Heroku PostgreSQL database"
$ git push heroku master
We have two ways of pushing the database.
If you want to start a fresh without anything you may run the following command:
$ heroku run php /app/artisan migrate
This is done if you want to push your local db to the database in heroku:
heroku pg:push <The name of the db in the local psql> DATABASE_URL --app <heroku-app>
You may now open your application :
$ heroku open
This process is really confusing but I hope it will help in your endevours.
Remember heroku does not offer support for media files in the free tier subscription so find some where else to store those e.g UploadCare(I am making a documentation based on this).
This was written by: Ben Karanja Email me @ [email protected]
I try use heroku config:set$(cat .env | sed '/^$ /d; /#[[:print:]]*$/d') but it not working! :( How to fix that?