Duncan McClean

Setup Statamic v2 on Laravel Forge

14th September 2019

If you're looking for a version of this article for Statamic v3, you should check out Steven's article instead.

A big thanks to Ben Furfie who helped by looking over this article and suggested loads of improvements.

Every site needs to be hosted, and your Statamic site is no different. The recommended solution is a combination of Digital Ocean and Laravel Forge to handle your hosting and provisioning respectively.

This is the setup I use personally, but being around in the Statamic discord for long enough has prompted me that it's something not everyone knows how to do. So I thought I would write up something to help lighten the learning curve.

First things first, it's worth making sure that you know what each part of the puzzle does.

Laravel Forge

Forge is the tool that is designed to make the provisioning, deploying and managing your website easier.

Digital Ocean

Digital Ocean is the company that you rent your server from. You pay them for your own virtual private server which is often abbreviated as a VPS.

Now that we know what each thing is for, let's actually get started with the setup process.

Setting up Laravel Forge for Statamic

Next, you'll need to setup your own Laravel Forge account. You can do this by clicking here and creating one, that is if you don't already have one. After you've entered all your details, you will be prompted to connect with Github or one of the other version control providers. You'll also need to connect with a service provider, which in our case is Digital Ocean.

Forge will ask you for a Digital Ocean token, you can find this by:

  • logging into your Digital Ocean account

  • going into the API section

  • creating a token with Read & Write access.

Then you just need to copy the token it gives you and add it to Forge where it asks for it.

Configuring your first server

You will now be on the server screen. This is where you'll be able to create your first server. It'll ask you to choose some options.

Word of advice: unless you're going to be serving loads of users and you're going to be using loads of storage, then you don't need to go any higher than the minimum $5 Digital Ocean droplet. If you do need to go higher in the future, it's easy to change - all you need to do is change a small setting in the Digital Ocean control panel.

Once you're done with all of the above, Forge will create a server for you in your Digital Ocean account. It usually takes anywhere from five to ten minutes for Forge to provision a new server; so you might want to find a good YouTube video to watch in that time.

Your first Laravel Forge Statamic site

Once the server is ready, you'll be able to click on the name of the server and you'll be given the option to create your first site.

With this first site, it might be clever to start with a dummy domain or a low traffic site; something that you're not going to be panicking about if all goes weird.

Fill in the domain of the site you want to host. You can also add aliases here, like a www version.

The last version is a select. For Statamic sites, leave this as General PHP / Laravel.

You'll also want to change the web directory from /public to /. Of course, if you've setup Statamic to work above webroot or your planning on using a service like Envoyer, this doesn't apply, but as we're planning on keeping everything simple, just set it as /.

Selecting your Statamic Github repo

Next, you'll be asked if you want to set the site up from a repo, or as a fresh WordPress install. As this is a Statamic site, you're going to want to select the first option.

As with most git related services, you need to include both the repo author, and the repo name. So my github name is duncanmcclean, and my example repo is awesome-statamic.com. So the repo name will be duncanmcclean/awesome-statamic.com. That's what you put in that field.

Next, you'll be asked if you want to install any composer dependencies. Untick that box or you're going to have a bad time.

Once you've done that, click the button and Forge will do it's thing. These include things like configuring nginx and other server things you don't need to worry about right now. After Forge finishes loading, your site should be ready.

The only thing I'd recommend doing now is tweaking the default deploy script. In the settings screen, you'll see a box with some options starting with something like:

cd /home/forge/awesome-statamic.com git pull origin master echo "" | sudo -S service php7.3-fpm reload

I often add a couple of extra please commands to make it easier when I push a new version of my site live. My config often looks like this:

cd /home/forge/awesome-statamic.com git pull origin master echo "" | sudo -S service php7.3-fpm reload
 
php please clear:cache php please clear:stache php please clear:static

One thing you might want to look into is using Spock. Spock commits & pushes updates to entries, assets and settings when you make them in the Control Panel. This makes sure your Git repo always has the latest content. Ben Furfie has recorded a little video clip on how to get this setup.

Pointing your domain to Forge

Getting your domain to point to Forge is easily one of the hardest and most fiddly parts of dealing with hosting - largely because how you do this depends on who and where you bought your domain.

The key thing is you'll need to get the server's IP address from Forge and point an A record at it. Beyond the basics, how to point a domain to a server is a whole other topic and is outside the scope of this article (sorry...).

Getting into your server

There may be times when you need to access your server to look at log files or access some sort of system configuration. To do this, you'll need to be able to use SSH.

Unlike other virtual private servers (the droplet you pay Digital Ocean for), you can't just put in a username and password to SSH into a server. Instead you need a username and an SSH key.

If you haven't already created an SSH key for yourself - and you're on a Mac - you can do it by running these commands.

ssh-keychain -t rsa pbcopy < ~/.ssh/id_rsa.pub

I'm not sure on how you do this on Windows as I don't use Windows. But if I find out then I'll add instructions on it to this article.

The first command will create your key, while the second will copy the contents of the public version of that key to your clipboard. Once you've done that, you'll be able to go to Forge account, go to User Settings and then click the SSH Keys tab.

You'll see a box where you can paste in your key. Once you've done that, click save and Forge will now have the part of your SSH key it needs to authenticate your computer.

The last thing you need to do is click on the 'Add to Servers' button, next to the key. This will add that key to each individual server.

Now when you want to SSH into your server, you can type this (replacing 127.0.0.1 with the IP of the server in your Forge account):

ssh forge@127.0.0.1

A few quick things

  • If you want your site to be auto-deployed when you push to your Git repo, you need to enable Quick Deploy on the Site page in Forge.

  • I would recommend that you should install SSL on your site. Thankfully, Forge makes this super easy. Within your site options, click on the SSL link on the left hand menu in Forge. Then click on Let's Encrypt and then click on Obtain Certificate. A fresh certificate should be created and installed for you within a minute.