Create a Blog Using Hugo on CentOS 7

Well this blog is created following the steps below.

Install Hugo

Install Golang

Hugo is written in Go, so we install Golang first.

1
2
3
4
yum -y install golang

# check golang version
go version

And Golang is installed.

Add yum source

There is no Hugo package in official yum source, so we need to add the yum source first. Create hugo.repo in yum repo folder:

1
vi /etc/yum.repos.d/hugo.repo

Insert the text below:

1
2
3
4
5
6
7
8
9
[daftaupe-hugo]
name=Copr repo for hugo owned by daftaupe
baseurl=https://copr-be.cloud.fedoraproject.org/results/daftaupe/hugo/epel-7-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/daftaupe/hugo/pubkey.gpg
repo_gpgcheck=0
enabled=1

And the yum source is added.

Install Hugo

1
2
3
4
yum install -y hugo

# check Hugo version
hugo version

And Hugo is installed.

Create a site

To create a site with Hugo, run the command:

1
hugo new site <mysite>

And you’ve got your site initialized.

Add a theme

The theme I use is hugo-paper, a clean theme to show code, and it supports dark mode.

# enter site directory
cd <path/to/mysite>
# download theme to the theme directory
git clone https://github.com/nanxiaobei/hugo-paper.git themes/paper

Modify the configuration, the config file is in the directory of the site, which calls config.toml.

1
theme='paper'

And you can also copy the config file from theme directory, and modify it according to your requirements.

Pay attention to the baseURL in the config file, set it with the URL of your site.

Add posts

Add posts to your site.

1
hugo new post/first.md

And we have created a post. The — in the beginning is the information of the post which won’t be shown in your page. Your post can be added after the last —.

Build

You can generate static website after modifying the files properly.

1
2
# must be executed in the site directory
hugo

And we’ll get the static website, the generated files are in the public directory.

Deploy pages

I use nginx as the program to deploy the static web pages. Modify the nginx configuration file:

1
vi /etc/nginx/nginx.conf

Modify the user nginx to user root.

Modify the default.conf of nginx:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# add or modify one server
server {
	listen 80;
	server_name <yoursite.com>;

	root <path/to/site>/public;
	location / {
		index index.html index.htm;
	}
}

Reload nginx:

1
systemctl restart nginx

Congratulations! You’ve set up with your blog now, enjoy it.