How can we build a blog with Hexo + GitHub + Netlify
Why did I choose Hexo + GitHub + Netlify?
Hexo
Hexo is a fast, simple & powerful blog framework which can help us to generate the static files of blog and deploy the sites. The reason why I choose Hexo is only NexT theme of Hexo.
GitHub
I use GitHub to manage all the articles of my blog and regard it as a Cloud Drive to sync files.
Netlify
The reasons why I choose Netlify are:
- It can help me to generate and deploy the sites automatically, which means that I don’t need to memorize the command of Hexo such as
hexo generate
andhexo deploy
. I only need togit push
my articles to my repo on GitHub. - I can add my customized domain so easily. It can address DNS verification problem and also https problem in a very simple way.
Alternatively, you can also choose GitHub Pages (Maybe I will move my blog to GitHub Pages in the future).
Steps to build your own blog
Node.js and Git
Installation
You should install Node.js and Git on your own laptop first. It’s recommended to follow the official website of Node.js and Git.
Here I also recommend to install GitHub Desktop because of its convenience.
GitHub
New repo
Create a new repo for storing all the files of your blog.
Note that you should not name your repo as yourname.github.io
if you want to use Netlify. Because if you use yourname.github.io
as your repo name, GitHub will regards it as a GitHub Page automatically and do GitHub Actions (Build with Jekyll) to deploy the site automatically.
Also you should set the repo as Public and without README.md file.
Here because I have created the repo named blog
, it said that the repo exists on my account.
New SSH key
First you need to set the configurations of git on your laptop:
1 | git config --global user.name "your_name" |
And then you need to generate a SSH key on your labtop using:
1 | ssh-keygen -t rsa -C "your_email" |
Open your profile of your GitHub. Click settings
-> SSH and GPG keys
-> New SSH key
. You can set any name of title. Return to the bash and input:
1 | cat ~/.ssh/id_rsa.pub |
Copy the output to Key box. And Click Add SSH key
.
Finally, for testing whether you have added SSH key successfully, you can use:
1 | ssh -T git@github.com |
If you can see your username of GitHub, you have added SSH key successfully.
Hexo
Installation
It is highly recommended that you follow the steps on the Hexo official website to install Hexo.
What you need to do is just:
1 | npm install hexo-cli -g |
Note that blog
in hexo init blog
and cd blog
is the folder name of your blog. You can customize it such as hexo init myblog
.
NexT Theme
Installation
Follow the official instructions to install NexT Theme.
I also recommend the first method that mentioned in it.
1 | cd blog |
Note that if you install NexT in this way (npm), the theme will appear in the node_modules folder. If you install NexT in the second way (git), it will appear in the theme folder.
Configure your NexT Theme
Follow the official docs of Configuration of NexT Theme.
Please ensure you are using Hexo 5.0 or later.
Create a config file in site’s root directory, e.g.
_config.next.yml
.Copy needed NexT theme options from NexT config file into this config file. If it is the first time to install NexT, then copy the whole NexT config file by the following command:
1
2
3
4# Installed through npm
cp node_modules/hexo-theme-next/_config.yml _config.next.yml
# Installed through Git
cp themes/next/_config.yml _config.next.yml
Configuration of Hexo
Then, we need to set some configurations of Hexo.
Open the configuration file _config.yml
in your blog folder. And set the variables as follows:
1 | # Site |
And also:
1 | ## Themes: https://hexo.io/themes/ |
Note that the repository attribute should be SSH version of GitHub repo. If you use HTTPS version of GitHub repo, you will be asked to input the username and password of GitHub and also will fail to deploy your blog site because you will need to create Personal access tokens in the Developer settings of GitHub.
Push Blog to GitHub
Before we push the blog to GitHub, we need to make sure two things:
Add a README.md file manually and put it in the blog root folder.
Make sure you have a .gitignore file in the blog root folder.
Then you can push the blog files to GitHub just as follows:
1 | cd blog |
Note that you should not run hexo generate
or hexo g
or hexo d
because Netlify will help us to generate and deploy the site automatically using the build command hexo generate
.
Netlify
Deploy your blog site
Log in Netlify website, select import from an exitsting project and select GitHub. Choose the repo that you created just now (whose name is not username.github.io). And set Build command to hexo generate
and set Publish directory to public
as follows:
Then the site has been successfully deployed.
Custom domains
You can easily customize your own domain in Domain management on the side bar or the guide page if you deploy your site first time.
DNS verification and HTTPS
You can do DNS verification and enable automatic TLS certificates so easily in the HTTPS part of Domain management on the side bar (just need to click the button).
How should you write a new article
Create a new article
You just need the following commands to create a new article:
1 | cd blog |
Then, you will find a new md file whose name is new_article_file_name
in source -> _posts
folder.
For the title of your article, you can set it on the top part of md file.
Note that:
new_article_file_name
is the Markdown file name, but not the title of your article.- When you start writing Markdown file, the highest head should be head 2 not head 1 because you have set the title on the top part of md file.
Now you can edit the Markdown file to write the articles.
By the way, I use Typora + iPic (Image Upload Tool) + Imgur (Image hosting service) to write the blog (Mardown files). Here is also a perfect Markdown tutorial website.
Push the local repo to GitHub
Bash
You can use bash to push the repo to GitHub:
1 | git add . |
GitHub Desktop
You can also simply use GitHub Desktop to commit and push. You can edit the comments on the left-bottom corner and push on the top button:
How should you delete the articles
You can simply remove md files in source -> _posts
folder. And push the local repo to GitHub again. Netlify will help you regenerate and deploy the blog site.
Tips
Insecure mixed content detected in
You may note that there is an alert of Netlify which said that Insecure mixed content detected in. The reason for that is the configuration file sets url
as http
and example.com
.
So you need to modify the configuration file _config.yml
of you blog.
1 | # URL |
Hexo + GitHub Pages
Because Netlify uses the build command hexo generate
, we cannot use hexo generate
and hexo deploy
(which uses the deploy settings in _config.yml
) to deploy the blog site on the repo whose name is yourname.github.io
. That means Netlify is in conflict with hexo deploy
on your local environment and also in conflict with the repo whose name is yourname.github.io
.
If you want to deploy the blog site on GitHub Pages, you just need to set the deploy settings in _config.yml
as in the section Configuration of Hexo. And then run hexo generate
and hexo deploy
on your local environment. GitHub will automatically deploy the blog site with GitHub Actions (although in actions workflow it say that it builds with Jekyll, it actually build with Hexo). You don’t need to git init
local blog folder and connect to the GitHub repo because hexo deploy
will only use the deploy settings in _config.yml
and help you to push and deploy the blog site on GitHub.
Conclusion
Hexo + GitHub + Netlify is a very convenient combo for blog writing.
Enjoy the blogging!