Create Hugo in Amazon Linux Part 1
2024-08-09
At Very Begining
Yet again, I’ve started hosting my blog and trying to keep writing. The last time I wrote was over two years ago. A lot has changed in these years – Graduated with a master’s degree, moved to a new country, joined a company, and found the one I love.
I think it’s time to start writing again because just consuming without producing isn’t an effective way to learn. I haven’t even coded much in the past two years… It’s quite shameful for a nerd.
Installation
The dnf
repo on Amazon Linux is pretty limited so I installed everything else but Git using prebuild binary.
Latest binary can be found at: go, dart-sass, hugo
# install prerequisites: Git, Go, Dart Sass
sudo yum install git
wget https://go.dev/dl/go1.22.6.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.22.6.linux-amd64.tar.gz
wget https://github.com/sass/dart-sass/releases/download/1.77.8/dart-sass-1.77.8-linux-arm64.tar.gz
sudo tar -C /usr/local -xzf dart-sass-1.77.8-linux-arm64.tar.gz
# install extended hugo
wget https://github.com/gohugoio/hugo/releases/download/v0.131.0/hugo_extended_0.131.0_linux-amd64.tar.gz
sudo tar -C /usr/local/bin -xzf hugo_extended_0.131.0_linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' > ~/.bashrc
echo 'export PATH=$PATH:/usr/local/dart-sass' > ~/.bashrc
source ~/.bashrc
Create a Site
This part is basiclly a copy paste from hugo quick start
hugo new site blog
cd blog
git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
echo "theme = 'ananke'" >> hugo.toml
hugo server
hugo new content content/posts/create-hugo-in-amazon-linux.md
Associate Domain Name with My EC2 Instance
I bought a domain on Cloudflare. I strongly recommend ignoring the top search results of domain registration, including GoDaddy. They are notorious for being expensive and horrible service. They spend all their money on advertising.
aws route53 create-hosted-zone \
--name $domain \
--caller-reference $(date +%s) \
Create a records.json
file that defines the DNS records you want to add. Normally you want to add your domain without any prefix with your EC2 private IP. And add another records that create a alias point www.domain.com to your domain.
{
"Comment": "Add DNS record",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "example.domain.com",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "192.0.2.44"
}
]
}
},
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "www.domain.com",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "HOSTED_ZONE_ID",
"DNSName": "domain.com",
"EvaluateTargetHealth": false
}
}
}
]
}
# Add records to public hosted zone we just created.
aws route53 change-resource-record-sets --hosted-zone-id $zone_id --change-batch file://records.json
# Check nameservers
aws route53 get-hosted-zone --id $zone_id --query "DelegationSet.NameServers[*]"
Next, we add nameservers as NS records to Cloudflare DNS management.
Or If you don’t want setup all of this, get a reserved ip from aws and add it to the Cloudflare as Type A record.
Host on nginx
While waiting for domain propagation, let’s host our static contents using nginx.
sudo yum install nginx -y
sudo mkdir /etc/nginx/sites-enabled
sudo mkdir /etc/nginx/sites-available
touch /etc/nginx/sites-available/blog.conf
# See next code block for config editing
sudo vim $_
# /etc/nginx/sites-available/blog.conf
server {
listen 80;
server_name jichenxu.com www.jichenxu.com;
root /var/www/blog;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
After editing /etc/nginx/sites-available/blog.conf
, remember to add include /etc/nginx/sites-available/*
to /etc/nginx/nginx.conf
.
sudo ln -s /etc/nginx/sites-available/blog.conf /etc/nginx/sites-enabled/
# create a symbolic point to public/
sudo mkdir -p /var/www/blog
sudo ln -s /var/ww/blog /home/ec2-user/workspace/blog/public/
chmod 755 $_
# I am the only user of this instance so whatever.
# Might built a CI/CD pipeline later.
chmod 755 ~
# test config
sudo nginx -t
# reload nginx
sudo systemctl reload nginx
# or start nginx if this is your first time.
sudo systemctl start nginx
sudo systemctl enable nginx
Now the blog should be up and public accessible.
End of This Part
For now I only want to focus on writing so part 1 is end here.
In the future, I might modify the theme, create my own templates, add features like statistics or comments. I will update this series when that happens.