How to deploy Laravel to DigitalOcean with Laravel Forge

Updated: January 14, 2024 By: Guest Contributor Post a comment

Introduction

Deploying a Laravel application can be a smooth and efficient process with the help of Laravel Forge attached to DigitalOcean services. This tutorial by example will walk you through the process of deploying Laravel to DigitalOcean using Laravel Forge with multiple code examples. By the end of this tutorial, you should have a solid understanding of the deployment process for a Laravel application from scratch to finish.

Laravel Forge is an incredible tool provided by Laravel that helps you provision and manage servers for your applications. It essentially allows you to automate the deployment process straight from your repositories on Github, Bitbucket, or Gitlab to your cloud servers hosted on DigitalOcean (and other cloud providers). It handles the complexity of server management and gives you a user-friendly interface to work with.

Prerequisites: Before we dive into the deployment, let’s make sure you have the following:

  • An account with Laravel Forge
  • A DigitalOcean account
  • A Laravel application under version control (e.g. GitHub, GitLab, Bitbucket)

Deployment Step-by-Step

Step 1: Setting Up a Server on DigitalOcean

The first step is to set up a server on DigitalOcean that will host our Laravel application. Forge makes this easy:

  1. Log in to your Laravel Forge account.
  2. Go to ‘Servers’ and click on ‘Create Server’.
  3. Select DigitalOcean as your provider.
  4. Choose your preferred size for the Droplet.
  5. Choose your server’s region.
  6. Add your SSH keys to allow Forge and your local machine to communicate with your Droplet.
  7. Click on ‘Create Server’ and wait for Forge to provision it for you automatically.

Once the server is ready, Forge will show your server’s IP address, which you’ll need later on.

Step 2: Adding Your Site to Forge

After your server is up and running, it’s time to add your Laravel application as a ‘site’ in Forge:

  1. From the Forge dashboard, click on your server to access the server dashboard.
  2. Click on ‘Sites’ in the server dashboard, then on ‘Create Site’.
  3. Fill in your domain details and make sure you select ‘Laravel’ as the site type.
  4. Once the site is created, Forge will provide a default server block configuration. You may need to tailor this to match your application’s specific needs later on.

Note that you will need to update your DNS to point your domain at the DigitalOcean server’s IP address you obtained earlier.

Step 3: Setting Up Your Repository

At this stage, we will now connect Forge to your code repository:

  1. Inside the server dashboard, click on the site you’ve just created.
  2. Click on the ‘Git’ tab. Here you’ll have the opportunity to connect your Forge site to your source control.
  3. Select the source control system that contains your Laravel application and allow Forge to install your application.
  4. Within this tab, set the branch to deploy. Typically this is ‘master’ or ‘main’, depending on your version control settings.
  5. Add in any build or deployment scripts in the provided text area that Forge should execute after pulling your application. A typical script would be:
cd /home/forge/{your-site-domain} php artisan migrate --force

This script will navigate to your application directory and run the Laravel migrations.

Step 4: Configure Environment

Every Laravel application requires environment variables which are needed for it to run smoothly:

  1. Within the server’s dashboard, click on your site and go to the ‘Environment’ tab.
  2. Edit the environment variables to reflect your application settings, such as your database connection details and any other required service credentials.
  3. Click on ‘Update Environment’ when you finish. This will update your app’s .env file with the proper credentials.

Your environment configuration should be secured and reflect the production settings for your application.

Step 5: Set Up Deployment Trigger

Forge allows you to automatically deploy your application when changes are made to your Git repository:

  1. Go to the ‘Apps’ tab on your Forge site’s dashboard and locate the ‘Deployment’ section.
  2. Enable ‘Quick Deploy’ if you want Forge to automatically deploy new commits to the specified branch.
  3. If you need more control, you can choose to deploy manually each time or set up deployment hooks for custom workflows.

With deployment triggers configured, your application will automatically get deployed upon each push to the connected repository, or you can manually trigger it if you chose that option.

Step 6: Manage SSL Certificates

Ensuring secure HTTPS connections to your Laravel application involves managing SSL certificates:

  1. On the Forge server dashboard, click on your site and navigate to the ‘SSL’ tab.
  2. Here you can choose to install a new SSL certificate. Laravel Forge provides a free LetsEncrypt certificate which is often sufficient for many applications.
  3. Simply click the ‘Let’s Encrypt’ button and Forge will attain and install an SSL certificate for you.
  4. Once the certificate is installed, click ‘Activate’ to enforce HTTPS on your site.

Laravel Forge performs regular renewals of LetsEncrypt certificates, so there is no extra work required after the initial setup.

Step 7: Schedule and Perform Backups

As a final step in deployment, setting up a backup schedule for your data is a wise move. Here’s how to set up backups in Forge:

  1. Inside your Forge server dashboard, navigate to the ‘Backups’ tab.
  2. Forge allows you to configure database backups by clicking on ‘Configure Backup’. You can also back up your files.
  3. Set your backup frequency, retention policy, and backup location, which can often be a cloud storage provider.
  4. Make sure to store your backups in a secure and reliable location separate from your hosting infrastructure.

By faithfully following Forge’s provided protocol, you can configure a stable regime for database and file backups to handle data retrieval in the event of a disaster.

Conclusion

Deploying Laravel to DigitalOcean using Laravel Forge simplifies the deployment process, manages server configuration, and makes automation and scaling more accessible. By leveraging Forge’s features and automation options, you can have your Laravel application running and updated with ease. With today’s deployment handled, your focus can shift back to developing great applications with Laravel and PHP.