Fast Free Grav Development

EC2 + ServerPilot + PHP7 = Boss Setup!

9 mins

I had originally planned on penning a blog post about how easy it is to setup and deploy a Grav site with ServerPilot with DigitalOcean. While that is a fantastic VPS service, someone on Gitter asked me about setting up Grav on Amazon's AWS, so I checked it out. What I found was pretty amazing. Even the free tier of the EC2 t2.micro package runs Grav like a boss! Combine this with awesome automated setup and server management via ServerPilot, and you have the makings of an unbeatable combination. In this blog post I'll go through step-by-step on how to get this setup on your own. The hardest part is setting up EC2 as it's a bit ungainly, but other than that it's a doddle, and more importantly, it's free!

Amazon Web Services

The Amazon Web Services (AWS) name is really a banner for a wide variety of cloud offerings. The one we are interested is called Amazon Elastic Compute Cloud (Amazon EC2) which is simply Amazon's VPS solution. This means you can host Virtual Machines running a variety of operating systems in the cloud. AWS is known for its solid value and performance, and they even have a free tier of micro instances that just happen to work perfectly for Grav.

Before going any further you will need to click the Create an AWS Account to create your account.

You will have to enter a credit card during this process but you will only get charged if you go over the Free Usage Tier limits. Currently this is 750hrs/month for 12 months. So basically you get a year of free hosting, and after that it will be less than $10/month.

Once you account is created, you can log in to the Amazon Web Services Console and you will be greeted by a dizzying array of service offerings. The one we need is the first Compute section labeled EC2:

After launching into the EC2 Dashboard click the blue Launch Instance button to start configuring the virtual server we will be deploying. The first step in this process is to choose the Amazon Machine Image (AMI) to use. Because we are going to be using ServerPilot to manage our server, we need to pick Ubuntu Server 14.04 LTS 64bit

In step 2, choose the instance type you wish to use. I recommend going with the free tier t2.micro type.

This is the default selected option, so all you need to do next is click the blue Review and Launch button. You should see something like this:

You don't need to do anything at this point, simply click the blue Launch button. You will then be greeted by a modal window asking you to set up a key pair which you will use to log in to perform the next steps. Choose the Create a new key pair option, and give it a name, then click the Download Key Pair button to download the private key file (gravcms.pem in our example):

Click the blue Launch Instances button to start up your instance. You will see a page with Launch Status information, but you can skip this for now and click the blue View Instances button to see your server running. This next view contains a lot of information, but there are two things we need to do next.

First jot down and save the Public IP which is listed in the description tab at the bottom of the page. We will need this frequently when setting up the server.

Second, note the name of the Security groups, a few lines below the Public IP setting. It will probably be something like launch-wizard-1. Click the Security Groups option in the sidebar on the left in the Network & Security section, then click the row of your security Group (launch-wizard-1). You will see a list of Inbound ports, and at this point it only contains an entry for SSH on port 22. We need to add HTTP and HTTPS by clicking the Edit button, then Add Rule:

After clicking the blue Save button, you should see these three rules in the Inbound tab.

Getting Ready for ServerPilot

Usually with most VPS services such as DigitalOcean and Linode, you enter a password for the root account when you create your instance. The more secure key pair mechanism is used by EC2 instances, but unfortunately ServerPilot does not support this. To deal with this, we have to enable SSH login via password before we can go any further. This is not terribly difficult if you follow some easy steps.

I'm assuming you have access to command line based SSH. If you are running windows and don't have an ssh terminal client, you can google how to do these steps with Putty or another GUI SSH client.

You will need your .pem file you downloaded earlier so you need to know the path to it's location, or move it somewhere you can access it. I like to keep mine in my ~/.ssh folder, so I'll do that step first:

$ mv ~/Downloads/gravcms.pem ~/.ssh

Next we need to set some restrictive permissions on this file so SSH doesn't squawk about it being insecure:

$ chmod 400 ~/.ssh/gravcms.pem

Now we can login to our EC2 instance using this file for authorization using the Public IP you noted before:

$ ssh -i ~/.ssh/gravcms.pem ubuntu@<YOUR_PUBLIC_IP>

You should be confronted by a prompt that looks something like: [email protected]. This means you are actually connected to your EC2 server.

Next we need to enable the SSH service to allow root user passwords as an acceptable authentication method:

$ sudo vi /etc/ssh/sshd_config

I am assuming you have at least a rudimentary knowledge of the VI editor. If not please read this great introduction.

You will need to change the PermitRootLogin and PasswordAuthentication options:

PermitRootLogin yes
PasswordAuthentication yes

After you have saved this file you just need to restart the SSH service:

$ sudo service ssh restart

The last step we need to make is to actually set a root user password:

$ sudo passwd root

Just follow the prompts and enter a secure password. Let's test it!

$ exit

This will drop you out of the remote instance and back to your local machine, this type, let's try to connect as root and enter the new password when prompted:

$ ssh root@<YOUR_PUBLIC_IP>

If everything has been done properly, you should be logged in as root. Great, we can move on!

ServerPilot Setup

Running your own VPS is a little more involved compared to using traditional shared hosting. Shared hosting provides you with a nice web-based Control Panel and is a great option for web-beginners. However, if you want better performance, full control over your environment including what web server you are using, the latest and greatest PHP versions, optional modules and software, a VPS solution is the way to go.

Normally this would mean the onus of managing, maintaining, and configuring that server falls on you, and a significant amount of technical know-how is required. ServerPilot attempts to ease this burden by managing your server for you. It ensures your server is kept up to date with the automated updates of the latest packages, secures the server with a firewall, and provides reporting and monitoring.

It sets up a pre-configured hosting environment that uses Nginx to reverse proxy request to an Apache web server for optimal performance and compatibility. It installs PHP-FPM for maximum performance and also the ability to choose the PHP version all the way up to the newly released PHP 7. ServerPilot provides many more advanced features, some of which require a paid subscription, and you can read about them on their site, however the free account contains the essential stuff.

After signing up for your free account, you will be prompted to Connect a Server.

Enter your Public IP of your EC2 server, also enter the Root Password you created in the previous section. Also enter a new password for the SFTP Password. This will be the password you use when connect via SSH or SFTP using the serverpilot user on your server. Then simply click the blue Connect to SeverPilot button.

This will take a couple of minutes, but it's doing a lot of work. It's basically installing all the web hosting packages and configuring them to allow for easy deployment by ServerPilot. When the process is finished you should see a + Create App button, so click it.

ServerPilot allow you to setup many apps on a single instance, each with their own domain name and PHP version. For our example we'll use the popular Grav Blog Skeleton to fully test out our new server.

Fill in the details as you see fit. We are going to use PHP 7.0 as Grav runs nice and fast on it. I used a domain called gravblog.dev. This domain doesn't actually exist, it's intended for development. To get things working I need to add an entry for gravblog.dev in my /etc/hosts file that points to the public IP of the server.

If you need help on how to edit your hosts file check out this useful howto guide

You can of course use a domain name you own. I suggest picking a domain you can use entirely for this single server and then setup a wildcard A record in the DNS configuration for the domain and point that at your public IP. This way if you have a wildcard record for acme-development.com, then you can simply enter a domain of client-a.acme-development.com in one app, and client-2.acme-development.com in another, and it will just work.

Test your setup by pointing your browser to http://gravblog.dev or whatever domain you have configured. You should see something like this:

This lets you know things are up and running as expected, all we need to do now is install Grav:

  1. SSH to your server and login with serverpilot user using the SFTP password you entered during server setup:
    $ ssh [email protected]

    or

    $ ssh serverpilot@<YOUR_PUBLIC_IP>
  2. Navigate to the app and download the skeleton you wish to use:
    $ cd apps/gravblog
    $ wget https://github.com/getgrav/grav-skeleton-blog-site/releases/download/1.1.0/grav-skeleton-blog-site-v1.1.0.zip
  3. Move the old public/ folder out of the way, extract the skeleton, and rename it to public/:
    $ mv public public.old
    $ unzip grav-skeleton-blog-site-v1.1.0.zip
    $ mv grav-skeleton-blog-site public
  4. Refresh your browser pointing to http://gravblog.dev.

If all has gone well, you should see your Grav site up and running. The great thing about this is that ServerPilot sets things up so that the user you login with serverpilot is the same user that webserver runs under. This means you will never have any permissions issues that haunt so many setups where your user and your server run with different permissions. You can take this opportunity to test this out with the CLI tools Grav provides and install the admin plugin:

$ cd ~/apps/gravblog/public
$ bin/gpm install admin

This one command will install the Form, Email, Login, and Admin plugins. Now when you refresh your browser you will be prompted to create a new admin user. Fill out the form and you will now see the dashboard:

Good luck!

Grav Premium
Turbo-charge your Grav site - from the creators of Grav