Setup Hexo with Dokku on AWS EC2

Recently, I bought a .tech domain on Mashable.com ($39.99 for 10 years) and want to make something cool about it.

I am a big fan of Heroku and always wish I can self-host an heroku-like service.

Then I read about dokku, it is a very cool software that replicate the deploy process of heroku.

This is a guide on how to setup a blog with Hexo, dokku and AWS EC2.

AWS Configuration


In this section, I assume you have already create an account on AWS

One-Click Deploy with dokku-aws

Go to dokku-aws Github Repo and click on Launch Stack

Follow the instruction and it will create an Ubuntu EC2 instance with dokku installed and an Elastic IP.

You can find all information in your AWS Console.

Update DNS Record

Copy your Elastic IP address and add 2 A Records in your domain DNS Setting, in this example, I use example.com.

Name IP
example.com Elastic IP
*.example.com Elastic IP

SSH Settings

You will need ssh and git to access and deploy applications to EC2 instance.

Add these line in your machine ~/.ssh/config.

In this example, I use example.com and the key location ~/.ssh/example-com.pem

1
2
3
Host example.com
Hostname example.com
IdentityFile ~/.ssh/example-com.pem

Enable SWAP (optional)

The free tier of EC2 instance has only 1 GB of ram, you may need more ram for dokku to build bigger dokcer images.

Create a 1GB swapfile with this command, you can change 1G to any size you want.

1
sudo fallocate -l 1G /swapfile

Verify the swap file size

1
ls -lh /swapfile

Change permission of the swapfile to 600

1
sudo chmod 600 /swapfile

Enable the swapfile

1
2
sudo mkswap /swapfile
sudo swapon /swapfile

Make it persistence

1
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Dokku Configuration


Create App (your EC2 instance)

1
dokku apps:create blog

Enable SSL with Let’s Encrypt (your EC2 instance)

Install plug-in

1
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git

Enable plug-in on your apps

In this example, I use `you@example.com` as your email.

1
2
3
4
5
dokku config:set --no-restart blog DOKKU_LETSENCRYPT_EMAIL=you@example.com
dokku letsencrypt myapp

# Auto-renew
dokku letsencrypt:cron-job --add

Hexo Configuration


Install Hexo

1
npm install hexo-cli -g

You can find more about Hexo here

Generate Site

After you setup your theme, write your first post. You generate site with this command

1
hexo generate

Deploy to dokku

After you generate site, the root folder should have public/

Create a new folder out side of hexo project folder and copy contents of public/ into it.

Create Dockerfile in this folder

1
2
3
FROM nginx:alpine
COPY . /usr/share/nginx/html
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

Create nginx.vh.default.conf in this folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 5000;
server_name localhost;

port_in_redirect off;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

access_log /var/log/nginx/access.log combined;
}

Then create git repo in this folder

1
2
git init
git commit -am"My first blog post"

Then add remote pointing to your AWS EC2 instance

In this example, I use example.com as your domain.

1
git remote add dokku dokku@example.com:blog

Then push

1
git push --set-upstream dokku master
Share Comments