Tutorial

Deploy PHP Apps Easily with Laravel Forge

Draft updated on Invalid Date
author

Chris on Code

Deploy PHP Apps Easily with Laravel Forge

This tutorial is out of date and no longer maintained.

Introduction

Forge is an incredible tool in the family of Laravel products that helps developers focus on developing, not deploying and hosting. I personally use it when deploying Laravel applications and it does make life much easier.

Many of the pain points of hosting are taken care of for you so you can get right back to developing. Forge takes care of things like:

  • Creating and provisioning a new server
  • Setting up SSH keys
  • Creating domains and subdomains
  • Deploying from GitHub
  • Save common bash scripts and re-run them on many servers
  • Configure scheduled cron jobs
  • Firewall and security pre-configured
  • Configure New Relic and other services
  • Install SSL Certs

Forge can do much more, but those are the main things that it will take care of for you.

This article assumes you have a bit of knowledge about how DigitalOcean (or Linode) and VPSs (virtual private servers) work.

We’ll be diving into how we can deploy a Laravel application to Forge using DigitalOcean and GitHub.

How Does Laravel Forge Work?

You can think of Forge as a GUI or interface for maintaining your DigitalOcean servers (also called droplets) easily.

The steps to using Forge are pretty simple. The gist of it is:

  1. Link your cloud provider (DigitalOcean in this case)
  2. Create a server
  3. Create a site (a default site is already provided per server)
  4. Link site to a GitHub repo
  5. Deploy!

But DigitalOcean is already easy enough you say? Yes, it is, but there are a lot of great features that Forge gives you so that creating and configuring many servers becomes an easy and repeatable process.

Why Use Forge?

Geared towards Laravel hosting This is an obvious one. A product build by Taylor Otwell and Laravel is naturally going to help out its sister products. Each server configured through Forge comes with:

  • Ubuntu 14.04
  • PHP 5.6
  • Nginx
  • Postgres
  • Redis
  • HHVM Ready

Instantly configure domains and subdomains Forge gives us the ability to create domains and subdomains on the fly. It will configure the domain in Nginx, map it to the appropriate directory, and get a site up and running.

Deploy sites from GitHub This makes life much easier as a developer. By default, sites are manually deployed so you’ll have to click Deploy. You can also set it to auto-deploy so that deployment happens whenever you push to your repository.

Reusable bash scripts When creating multiple servers, there are often many things that we have to do for our specific environment. Forge lets us save those steps and reuse them across servers.

So much more Developing Laravel applications becomes much easier when you know that the deployment will match everything that your Laravel app needs. This includes things like cron jobs, server analytics, and more.

Now that we’ve talked about some of the benefits of Forge, let’s look at how we can deploy a Laravel application. For our purposes, we’ll just be deploying the default Laravel install that comes out of the box.

Deploying a Laravel App to DigitalOcean

Once you have a Forge account, you’ll want to go into your settings and get things set up.

If you want to deploy from either of these services, you’ll need to link them to your Forge account. Go ahead and go through that process and you’ll see both are linked and ready to go.

Add SSH Keys

The next simple step is to add your SSH keys to your settings. These SSH keys will be added to every server that Forge creates so that you can SSH into your server. Password authentication into a server is disabled by default and leads to a more secure server.

That’s it for setting up what we’ll need. The next step is creating servers and sites!

Create Server

Let’s move on to what Forge does best: creating servers for us.

Go back to the Dashboard and you’ll see the interface to create servers.

This is very straightforward, which is one of the reasons Forge is so great at its job.

Credentials This will be any cloud providers that you have linked to your account. If you’ve linked two DigitalOcean accounts and one Linode account, all three will show here.

Name The name that you want to name your server. You can name it anything you like. If you have a specific domain in mind for this particular server, then name it that. We’re calling our app einstein-dev since it’s a little project we’re working on currently.

Server Size The size of the server that you’d like to create. Here we have many different options so pick whatever fits the needs of your application. Since this is a dev server, we don’t need too much. We’ll stick to the $10/mo server.

Region Wherever you want your server to be located. Ideally, you’d want it to be close to where you believe your main users will be.

Database Name If your application needs a database, then you can name it here. By default, it will be named forge.

HHVM There is also the option to turn on HHVM. Since HHVM is blazing fast and we always love more speed for our applications, let’s turn it on.

Once you have the configuration you like, go ahead and click Create Server.

You’ll see that Forge is going through the paces of creating a server, configuring it, and getting it ready based on the settings we just input.

Once our new server is ready to go, we can see it’s green and good to go.

We can also go over to our DigitalOcean account and see that our new server is created.

In addition to adding your personal SSH keys to the new server, Forge will also generate a key on the server and add that to your GitHub or Bitbucket account (or both if you have them linked). This way, the server will have the necessary permissions to pull from your repositories.

You’ll get a nice email that states your server has been configured.

Server Management Dashboard

Once we have our server ready to go, we can click Manage to start managing this specific server. From here, you can manage multiple things like:

  • Sites
  • SSH Keys
  • Scheduler and cron jobs
  • Daemons
  • Networking
  • Monitoring Services
  • Other meta settings

You can already see that Forge provides many tools to help us manage our servers. For those who aren’t totally inclined to SSH into their servers and configure everything there, Forge has a good set of tools and dashboards to get things done.

Monitoring Services

Forge provides the ability to install Blackfire.io, New Relic, and Papertrail. All you have to do is add the corresponding credentials and you’re good to go!

Server Tools

In addition to the main dashboard server configuration buttons, you also have the ability to manage a server at the bottom of the server dashboard.

From the management tools at the bottom, you have the ability to:

  • Archive the server for use later
  • Edit config files (PHP FPM and PHP CLI)
  • Restart services
    • Server
    • Nginx
    • MySQL
    • Postgres
  • Stop services
    • Nginx
    • MySQL
    • Postgres
  • Delete the server

The server management tools are very robust and allow us to manage our site without having to SSH in. This convenient dashboard lets us give access to users that may not have the most server experience.

Site Management

Sites are going to be how we configure our applications. Forge and Nginx already have a default site configured. This will be the main site for this server. If you need more sites, then feel free to delete this one and create new ones.

Notice that the directory for this default site is /public. This is how Laravel needs to be installed and just keep that in mind when deploying applications other than Laravel. This will tell Nginx where to look for your application.

To start managing a site, click the manage button next to the site.

This now brings us to the site management area. At this point, there have been three main management pages:

  • Main Dashboard: Create and manage servers
  • Server Dashboard: Manage a server and create sites
  • Site Dashboard: Configure a specific site

The site dashboard looks like this:

From this site dashboard, we have the ability to:

  • Link to a git repo
  • Create environment variables
  • Create queue workers
  • Manage SSL certificates

Let’s add our Laravel install to the default site.

Deploying from GitHub

For simplicities sake, let’s just go over to the GitHub Laravel repo and fork it to our own account.

Once we have our own repository that we want to deploy, we can move forward and configure it with Forge.

All we have to do is fill out the form with our repo and we’re good to go!

Once everything is configured, our application is live. Simple as that!

Setting up auto-deploy: If you want your application to be deployed every time a push is made to that repo and branch, then just click Enable Quick Deploy.

Adjusting deploy script: Forge will handle the git pull, composer install, and php artisan migrate for you every time the site is deployed. If you want to change how this works, click Edit Deploy Script. By default, the deploy script looks like:

  1. cd /home/forge/default
  2. git pull origin master
  3. composer install --no-interaction --no-dev --prefer-dist
  4. php artisan migrate --force

Edit that to however you want your application to work.

In addition to these deployment settings, you can also include third-party application integrations.

Third-Party Integrations

Forge provides a Deployment Trigger URL so that you have the ability to integrate with any applications of your choosing. If that URL is hit, then your application will run through deployment.

You can get notifications for deployments through HipChat and Slack currently.

Environment Variables

Forge gives us the ability to create environment variables. Here we have created some variables and these will be pulled in by our application.

For instance, in a Laravel application under config/database.php, the following variables are used when connecting to a database.

    ...
    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
    ...

If you have a PHP application that isn’t Laravel, you can still pull these PHP variables using the getenv() function.

Conclusion

Laravel Forge takes care of so many jobs for you that deploying Laravel sites is now a walk in the park. It even allows for customizability in case your application has specific needs.

You could even use Forge to deploy any sort of site, PHP or otherwise. Here at Scotch, we are deploying our WordPress install with Forge to DigitalOcean.

Hopefully this gives you a good look at how easy Forge is to setup and deploy sites.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Chris on Code

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more