How to Deploy Laravel on DigitalOcean with Forge - 2020 Full Guide

Simon Bennett
Simon Bennett ยท Jan 20, 2020

Once you have finished deploying your product/project on of the most rewarding steps is getting it in production. This guide is going to use forge a tool for the Laravel community (can be used with any PHP project) one that we use at SnapShooter and one I recommend using when consulting. You can scale very highly with DigitalOcean before you need to look at services in the AWS realm of container based scaling, (Kubernetes at DigitalOcean is bridging this gap, topic for another day) .

To get started you will need the following already setup:

  1. Laravel project ready to go

  2. Github/bitbucket/gitlab hosted sorted

  3. DigitalOcean account (get $100 free via our link)

  4. A Laravel Forge account

Content :

  1. Create Server

  2. Setup Website

  3. Setup DNS

  4. Deploy

  5. Forge SSL cert

  6. Conclusion

What will not be covered:

  • Setting up Queue/Deamons

  • Auto deployments

Step 1 - Create Server

Create DigitalOcean Server in Forge

Create your first server in forge, I have selected a 1GB server with 1 Core and 25GB SSD, its just for this demo so choose the correct side for your project (you can resize later if required). I have also gone with PHP 7.3 at the time of writing as the project we are setting up is a stage environment and needs to match the production setup for now. Forge makes it very easy to change PHP version. We will cover the upgrade process in a future short article.

I have elected not to use Weekly backups as we will use SnapShooters Daily Backups once completed.

Click crease server and make sure you copy the flashed up credentials, forge will no longer email you server credentials, the reasoning is: they worry about email security. Make sure you copy them or its a real pain to recovery (not impossible). The process of provision will take ~10 minutes.

Forge provisioned server

Step 2 - Setup Site

Your new server has a default website setup. If you visit the IP address you will see PHP info page.

I always remove the default site as don't need it. Click on it then in the bottom right with the other buttons is a delete button. Once deleted you will be promoted to create a new site.

Forge new site config

In my case we are making a new staging site for SnapShooter. Once created you can click on the new site and decide if you want to use a git repo or WordPress. Clearly you want Git :)

Setup github access

Because I am using git, you just want the organisation or username with a backslash and then the repo name. Don't add .git to the end. Once you press install, forge will go ahead and do the first deployment, this will always been the slowest. Select the correct branch for your setup. I forgot to change it to develop which I did later on.

Step 3 - How to Enter a DNS Record in DigitalOcean

Because I want to use a domain hostname to access my server, I need to add an A Record. We are planning to add a full DNS guide this year for DigitalOcean but for now we will presume you have the main record setup, the simple version is:

  1. Find Domain in DigitalOcean control panel

  2. Create new record

  3. Select A (default already selected)

  4. Enter the subdomain name, staging in our case, if you want the root domain enter `@`

  5. Will Direct to, select the Droplet from the dropdown, or you can manually enter the IP

  6. Set TTL, the default is good enough for now unless you know what your doing

Add an A record DigitalOcean DNS

Step 4 - Deploy

When we pressed deploy we ended up with an error message: redis not found. This is because we use the library for communicating with Redis for cache and queuing.

PHP Warning: PHP Startup: Unable to load dynamic library 'redis.so' (tried: /usr/lib/php/20180731/redis.so

Simple fix:

sudo apt-get install php-redis

This time we got a database error:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')

This is only because we forgot to edit the ENV file with the correct details. I will not cover what is required for the ENV file, as its totally depends on your project, but you should have got the details required when you built the server. To update your ENV press the Environment file on the left hand side. Remember to turn off debug mode on any public assessable server or risk exposing all your private keys and passwords!

Step 5 - Forge SSL cert

If you used a domain to access your site, you can now provision a SSL cert. The process if very simple and I am going to just use a LetsEncrypt certificate using the simple wizard.

Forge LetsEncrypt

If the DNS was setup correctly the Lets Encrypt SSL will generate with no issues

Step 6 - Conclusion

As you can see getting setup and running with Laravel forge is a breeze, back in the day I would be installing MySQL and PHP by handing and spending hours getting everything working just fine. Now with Laravel forge the whole process is done for you and you only need todo a little bit of customisation to get it working just right.