Hammer Don't Hurt 'Em

Part 7: Hosting

Question: How do you automate the deployment of your websites onto web hosting?

You take advantage of git that's how.

More importantly, why would you want to do this?

You also might use git if you're in a team. I am not. So git's advantages for me are speed and accuracy. FTP is notoriously unreliable.

Primarily, though, I wanted to take advantage of Statamic's git integration.

Statamic 3 Pro has built-in git integration. When you save something on the live site it automatically gets committed and pushed to the remote repository.

When you are working locally, a pull from your remote repository means you will have a working copy of the live website. Including content because Statamic is a flat file system. Isn't that cool?

It is cool but there are caveats:

  1. You will get merge errors on the live website if live commits have failed to push. I've seen this happen a few times already
  2. The control panel slows down on saving and committing at the same time (You can add a delay or pass it off to a queuing system)
  3. You need to configure what goes up and comes down. And to a First Year like me it's not really clear from Statamic's docs what that should entail
  4. You need hosting that has built-in git

To get git integration working you need to enable Statamic Pro (add 'pro' => true, to your config/statamic/editions.php file) having bought a license of course.

Then add STATAMIC_GIT_ENABLED=true to your .env file.

You can also add STATAMIC_GIT_PUSH=true if you want Statamic to push your changes back to the remote repository. Otherwise commits will just build up on the live website and you will have to manually push the changes using SSH.

You will also want to add a delay to git kicking in to prevent saving becoming really slow. Use STATAMIC_GIT_DISPATCH_DELAY=3 The number being minutes this is delayed for after saving.

There are loads of other configuration options listed over on the docs.

So how do you get this working in the real world?

There are four basic hosting options for you with Statamic:

  1. Share/VPS hosting with ftp
  2. Shared/VPS hosting with ftp and some kind of git integration
  3. Custom web server with a management layer on top
  4. Custom web server

If you don't have git on the server then you are limited to one-way integration: Namely, pushing changes to the live site but not pulling. You would need to do that in the old fashioned way.

Option 1 is the old and trusted way of getting files onto web servers.

Option 2 would integrate a git-ftp service like Beanstalk can help you deploy over ftp from a remote git repository (e.g. GitHub). Basically the app watches your remote repository and then uses ftp to sync any changes to your web server. I've used Beanstalk for years – but it is expensive.

Option 3 is the sweet spot for me*.

There is a new kind of web hosting app that sits between you and a web server. Why do you need that? Well, web servers are complicated and life is too short. Yes, it's more expensive to do this. But it's worth the extra expense to me.

Laravel Forge is one of these new types of Web host applications. I say web host but it's not really that. It manages both your web servers and deployment of code. There are many other players in this market including RunCloud and Cloudways. Their costs are fairly similar.

What they do, with various degrees of assistance, is allow you to set up a web server from a selection of cloud server providers (e.g. Amazon, Digital Ocean, Vultr.) and give you GUI management tools to manipulate those servers without having to touch them directly. Much.

I tried Cloudways for a while but it runs a custom version of git. However, I couldn't get it to turn off so, in a huff, I decided to trial Laravel Forge. It came highly recommended in Statamic's Discord channel. The Laravel bit also helps Statamic users because it is especially designed for Laravel web apps. Of which Statamic is one.

Laravel Forge

Laravel Forge is really easy to use. It's really fast. You can spin up a new website, add SSL, and pull from a repository, in a matter of minutes.

For a Statamic site you will need to add a .env file. This is easy with Forge as it's a case of clicking the files menu at the bottom of the website's control panel, choosing Edit Environment File and pasting your personal settings. You can use your local development website's .env file as a starting point.

You can choose to deploy your site using a big green button that says "Deploy now" (I said it was easy!) or set up automatic deployment. So when a change is detected on your remote repository Forge runs a deployment script (which you can configure to do anything you like) and pushes the site live. Personally I would only automate deployment on Staging servers.

Nginx is a new one to me as I've always used htaccess to customise directory behaviour. I haven't had to do any configuaration yet with Statamic. It appears to just work out of the box.

I am also successfully running Perch Runway on Forge. I had to add the following to the nginx configuration, replacing the default location requests.

# Match just the homepage
    location = / {
        try_files $uri @runway; 
    }

    # Match any other request
    location / {
        try_files $uri $uri/ @runway;
    }

    # Perch Runway
    location @runway {
        rewrite ^ /perch/core/runway/start.php last;
    }

So to sum up, I really, really like Forge. It's simple, fast and easy to use.


So that's the basics out of the way. I hope you've found some of this drivel useful. Maybe I will even add a menu to this site to celebrate. Stay tuned!

*Because I am assuming that if you are using Option 4 then this article is way, way below your pay grade.