What is Laravel Forge?
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.
We'll be diving into how we can deploy a Laravel application to Forge using Digital Ocean and GitHub.
How Does Laravel Forge Work?
You can think of Forge as a GUI or interface for maintaining your Digital Ocean servers (also called droplets) easily.
The steps to using Forge are pretty simple. The gist of it is:
- Link your cloud provider (Digital Ocean in this case)
- Create a server
- Create a site (a default site is already provided per server)
- Link site to a GitHub repo
But Digital Ocean 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
- 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 use 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 on 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 Digital Ocean
Once you have a Forge account, you'll want to go into your settings and get things set up.
Link to GitHub or Bitbucket
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 in 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. Next step is creating servers and sites!
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 Digital Ocean 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 for 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
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 Digital Ocean 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:
- SSH Keys
- Scheduler and cron jobs
- Monitoring Services
- Other meta settings
You can already see that Forge provides a great deal of 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 dashboard to get things done.
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
- Stop services
- 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 let's us give access to users that may not have the most server experience.
Sites are going to be how we configure our applications. Forge and Nginx already has 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
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:
cd /home/forge/default git pull origin master composer install --no-interaction --no-dev --prefer-dist 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.
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.
Here's what a Slack notification would look like:
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.
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 Digital Ocean.
Hopefully this gives you a good look at how easy Forge is to setup and deploy sites.