Easy Setup for Directus through Docker on AWS EC2

Nicholas Hrboka
7 min readAug 16, 2020

--

Directus is a powerful “headless” CMS written in PHP. After many months of research, I settled on directus for a few reasons, mainly:

  • GraphQL API
  • Full control over custom schemas
  • simple collection based dashboard
  • no limitations on user roles

I love the collection based dashboard, where most CMS’s are blog or page focused, Directus allows you to define your schema with the full range of freedom of coding it yourself, no limitations.

Unlike some CMS’s, Directus offers a free self hosting option. After reading a plethora of documents setting up my first instance of Directus, I decided to create a cheat sheet here and share it with you (just you).

The Overview

The tech stack I will be using for this article is as follows:

  • Directus 8
  • Docker
  • docker-compose
  • AWS EC2 with Ubuntu
  • MySQL (installed with Directus)
  • PuTTY
  • PuTTYgen

It’s not much when you break it down. We will use an EC2 instance on AWS for hosting. After installing docker, we use docker-compose to read our .YAML file and install Directus 8 with MySQL. We simply use PuTTY to SSH into our instance and PuTTYgen for our private key.

Directus has excellent documentation, and is also well documented on setting up with Docker. It gives us a few options, but I like docker since docker containers handle builds and install dependencies for you.

The Setup — EC2

First step is to set up an AWS account if you don’t already have one. Go to services and click on EC2. From there click Launch Instance. We will be using Ubuntu for this example. Select the option seen below:

Next, select the t2.micro instance (free tier eligible). We don’t need to go into anything extra for this tutorial, so select Review and Launch. Review the setup and select Launch again.

On the next screen, you will want to create a new key pair. This is for security, and is highly recommended. Simply type in a name, and hit Download Key Pair.

Note: Move the key to a secure location. It will be needed to access your instance. Obviously, don’t share this with anyone unless they need access.

Now select Launch Instances!

The Setup — PuTTY

Not too difficult, right? Select View Instances to see an overview of all of your instances. For this next step we will access the instance via SSH. A popular method (for windows) is PuTTY. If you are using mac or linux, AWS will recommend another option when you select your instance and press Connect.

So, press Connect.

You will see a screen that looks like this:

Ignore this for now. We need to install PuTTY and PuTTYgen.

PuTTY: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

PuTTYgen: https://www.puttygen.com/

PuTTY is an SSH client,a dn PuTTYgen is a PuTTY key generator that converts your private key into a format that works for PuTTY.

Now open PuTTY. Once you get past the 90’s design and interface, you will see a lot of options under Category. We will only be using two of these categories, Session and Connection > SSH > Auth. Let’s focus on Session.

Under Session, you will type in the name of your root user and the Public DNS AWS gave you in the above image. For our example, the root user, unless changed, will always be ‘ubuntu’. See below:

ubuntu@[insert public DNS here]

Leave the port as port 22. Below where it says connection type, select SSH.

Now go to Connection, then SSH, then Auth. Here is where we put the private key. Before we do that, let’s use PuTTYgen to make sure ours is compatible.

Open PuTTYgen. Here, hit Load next to load an existing private key. Go to where you stored the private key you downloaded earlier. It’s important to change the file type to All Files (*.*), otherwise the key won’t show. Once you select they key, hit Save Private Key. Continue without a password despite the warning, it is not needed for us. Save the file. It is now a .ppk files instead of a .pem. PuTTY reads .ppk files.

Close PuTTYgen and go back to the Auth category in PuTTY. Where it says “Private key file for authentication”, click browse and select the new file you generated. Now, before we open the session, go back to the Session category. Under Saved Sessions, type the name of this setup and hit save. From now on, you can just open PuTTY and select this session to open it. (If you get a PuTTY warning, just select yes and continue.)

The Setup — Docker

We are in! Next step is installing docker. But first, it’s a good idea to update the local database of software to make sure you’ve got access to the latest revisions. Enter this:

sudo apt-get update

Next, it’s recommended to uninstall any old Docker software before proceeding.

sudo apt-get remove docker docker-engine docker.io

Obviously, it’s not installed, but it’s good practice to do this. Next, install Docker:

sudo apt install docker.io

You will receive a prompt, enter ‘Y’ and press enter to install.

The Docker service needs to be setup to run at startup. To do so, type in each command followed by enter:

sudo systemctl start dockersudo systemctl enable docker

Lastly, always verify your version to double check installation.

docker --version

Next make sure you add your user to the docker group. If you changed the name of the user, replace ubuntu with your name.

sudo usermod -aG docker ubuntu

Now, we want to generate a docker container from a .YAML file, to do that we need docker-compose.

Use this link (https://github.com/docker/compose/releases) to check for the most recent update, and replace they version you want in the command below. I used version ‘1.27.0-rc1’.

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.0-rc1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Next, change the file permissions.

sudo chmod +x /usr/local/bin/docker-compose

and finally double check… (may have to log out and back in to work)

docker–compose –version

Done! Docker is set up and ready to go! Give yourself a high five!

The Setup — Directus

Now comes actually installing Directus. We will be following along with the Directus docs here: https://docs.directus.io/installation/docker.html.

First create the docker-compose.yaml file. Then use VIM to edit the file. (If you are are not familiar with VIM, just follow the instructions and save yourself the headache.)

touch docker-compose.yamlvim docker-compose.yaml

Now this code comes directly from the Directus docs:

version: "3"networks:
directus:
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: "directus"
MYSQL_USER: "directus"
MYSQL_PASSWORD: "directus"
MYSQL_ROOT_PASSWORD: "directus"
ports:
- "3306:3306"
networks:
- directus
directus:
image: directus/directus:v8-apache
ports:
- "8080:80"
environment:
DIRECTUS_APP_ENV: "production"
DIRECTUS_AUTH_PUBLICKEY: "some random secret"
DIRECTUS_AUTH_SECRETKEY: "another random secret"
DIRECTUS_DATABASE_HOST: "mysql"
DIRECTUS_DATABASE_PORT: "3306"
DIRECTUS_DATABASE_NAME: "directus"
DIRECTUS_DATABASE_USERNAME: "directus"
DIRECTUS_DATABASE_PASSWORD: "directus"
volumes:
- ./data/config:/var/directus/config
- ./data/uploads:/var/directus/public/uploads
networks:
- directus

Go ahead and and edit the names, usernames and passwords to whatever you will use. I prefer storing these somewhere else as well for reference later. Copy and paste this into the VIM editor. To edit in VIM, press ‘i’ and begin editing. You don’t need to edit if you simply copy then right click the editor. After that, (press escape if you were in edit mode) type “:wq” and enter. Done!

Double check that the database name, username, and password are the same in both areas.

Next two commands will Pull the latest images of docker and ‘run the stack’. Essentially docker-compose will read your .YAML file and handle the Directus and MySQL installation for you.

docker-compose pulldocker-compose up -d

Now, we run the Directus install and add your first admin user! Run the command below, and replace the email and password with the login credentials you would like.

docker-compose run --rm directus install --email email@example.com --password d1r3ctu5

The Test

Now that we have everything set up, let’s test it out! Go back to the AWS EC2 Management Console, and select your instance. Copy your public DNS, and type it into the browser with port 8080 at the end (see below).

ec2………..us-west-1.compute.amazonaws.com:8080

You should see this:

If you don’t see anything, then double check your security group that port 8080 is exposed to all traffic.

Fin

That’s it! You may now log in with the email and password you used in the directus install command!

Cheers!

--

--

Nicholas Hrboka
Nicholas Hrboka

Written by Nicholas Hrboka

Self taught developer, musician, and trekkie.

No responses yet