10 C
Texas

How To Install and Create a Blog with Hexo On Ubuntu 20.04

Today we are going to learn that how to install & create a blog with Hexo on Ubuntu 20.04. Hexo is a static blogging platform built on Node.js. With the help of hexo you can write posts in Markdown format. It converts the blog posts into static HTML files with the help of pre-built themes. It varies from the other blogging softwares as it uses static files instead of loading code again & again when you reload the site. So, here we’ll see that how to install it on your system.

Step 1: Update Your System

As usual we do, update your system to have the latest updates installed.

sudo apt update -y
sudo apt upgrade -y

Then install the required packages by typing

- Advertisement -
sudo apt install git
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Note: Replace your git username, email & domain name.

Step 2: Configure Firewall on Ubuntu 20.04

Allow the firewall access by enabling the SSH port. Don’t forget to enable firewall if you’re installing it for the first time.

sudo ufw enable

Then enable the port 4000 in order to communicate

sudo ufw allow 4000

Make sure to allow http, https & then verify the status of firewall.

sudo ufw allow http
sudo ufw allow https
sudo ufw status

Output:

sabi@Ubuntu20:~$ sudo ufw status
 Status: active
 To                         Action      From
 --                         ------      ----
 4000                       ALLOW       Anywhere                  
 80/tcp                     ALLOW       Anywhere                  
 443/tcp                    ALLOW       Anywhere                  
 4000 (v6)                  ALLOW       Anywhere (v6)             
 80/tcp (v6)                ALLOW       Anywhere (v6)             
 443/tcp (v6)               ALLOW       Anywhere (v6)             
 sabi@Ubuntu20:~$ 

Step 3: Install Node.js on Ubuntu 20.04

Hit the below commands to install node.js on Ubuntu 20.04.

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs

Verify the installed version

node --version

Step 4: Install Hexo On Ubuntu 20.04

Fire the below command in your terminal to install Hexo on Ubuntu 20.04

sudo npm install hexo-cli -g

Once installed, make a new directory to install Hexo

sudo mkdir -p /var/www/hexo

Give it proper permissions

sudo chown -R $USER:$USER /var/www/hexo
sudo chmod -R 755 /var/www/hexo

Now switch to the hexo directory & initialize it.

cd /var/www/hexo
hexo init
INFO  Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
 INFO  Install dependencies
 added 185 packages from 430 contributors and audited 191 packages in 6.47s
 14 packages are looking for funding
 run npm fund for details
  found 0 vulnerabilities
 INFO  Start blogging with Hexo!

Finally hit the below command to install hexo

npm install

Step 5: Configure Hexo On Ubuntu 20.04

Edit the _config.yml file

sudo nano _config.yml

And then change the name of options according to your needs.

## Hexo Configuration
##  Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
 #Site
 title: Hexo
 subtitle: ''
 description: ''
 keywords:
 author: sabi
 language: en
 timezone: ''
 #URL
 If your site is put in a subdirectory, set url as 'http://example.com/child' and root as '/child/'
 url: http://example.com
 root: /
 permalink: :year/:month/:day/:title/
 permalink_defaults:
 pretty_urls:
   trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
   trailing_html: true # Set to false to remove trailing '.html' from permalinks

Note: Provide domain name, URL, site title & other required options & do 2 more changes.

  • change default_layout value from post to draft.
  • change post_asset_folder variable to true.

Step 6: Install themes For Your Blog

As hexo comes with the default theme, you can change to your favourite them by the hexo Themes page.

Here we’ll install Next theme for testing. To install this first switch to the /var/www/hexo/ directory & download the theme

cd /var/www/hexo
git clone https://github.com/theme-next/hexo-theme-next themes/next

Once downloaded, edit the _config.yml to update your new theme.

sudo nano _config.yml

Do the changes to theme variable in order to install new theme

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
 theme: next

In order to update the theme settings, change the /var/www/hexo/themes/next/_config.yml file.

Step 7: Create Your First Post

Hit the below command to create your first post.

hexo new first-post
INFO Validating config
INFO Created: /var/www/hexo/source/_drafts/first-post.md

Once created, fire the below command to edit new post.

sudo nano ./source/_drafts/first-post.md

Each post should have it’s own front-master. It constists of title, published date, categories, tags, etc. Update the default details with the accurate one.

title: sabi's First Post
 tags:
 test
 categories:
 Hexo
 comments: true 
 date: 2021-01-02 00:00:00
 Markdown goes here.
**This is our first post!**

To place an image in your post add the below command in your post.

{% asset_img "example.jpg" "This is an example image" %}

Note: Replace example.jpg with your image name & then copy that file to \source_posts\first-post directory.

Then save & exit the file.

Finally publish the post by typing

hexo publish first-post

Step 8: Installing a Plugin For Hexo

Get the plugins for hexo from their hexo plugins page. You can install your favourite one according to your needs. Save the plugin by typing

cd /var/www/hexo
npm i hexo-filter-nofollow --save

Then edit the below file

sudo nano _config.yml

Add the below lines at the end of the above file.

nofollow:
enable: true
field: site
exclude:
- 'exclude1.com'
- 'exclude2.com
enable: To activate the plugin.
field: Explain the scope of the plugin.
exclude: To exclude domains for nofollow links.

Step 9: Testing Hexo Server

Hit the below command to test the hexo server.

hexo server

Then type IP:4000 in browser & you’ll see the welcome page with your post created earlier.

Simply hit Ctrl+C to stop the server.

Step 10: Generate Hexo Static Files

Hit the below command to generate the static files

hexo generate

These files can be found at /var/www/hexo/public. From there, you can server them publically. We’ll use Nginx for this purpose.

Fire the given command to install nginx

sudo apt install nginx

Edit the hexo config file

sudo nano /etc/nginx/sites-available/hexo.conf

Then add the below data into the file.

server {
     server_name hexo.example.com;
root /var/www/hexo/public; 
index index.html index.htm; 
listen 443 ssl http2; listen [::]:443 ssl http2; 
ssl_certificate /etc/letsencrypt/live/hexo.example.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/hexo.example.com/privkey.pem; 
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_timeout 1d; 
ssl_session_cache shared:MozSSL:10m;  
# about 40000 sessions ssl_session_tickets off; 
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES25>             ssl_prefer_server_ciphers off; 
ssl_dhparam /etc/ssl/certs/dhparam.pem; 
location / { 
    try_files $uri $uri/ =404; 
}
 }
 server {
     if ($host = hexo.example.com) {
         return 301 https://$host$request_uri;
     }
     server_name hexo.example.com;
     listen 80;
     listen [::]:80;
     return 404;
 }

Then save & exit the file.

Link the config file by typing

sudo ln -s /etc/nginx/sites-available/hexo.conf /etc/nginx/sites-enabled/

Next, hit the below command to edit the nginx config file

sudo nano /etc/nginx/nginx.conf

Add the given line before the line include /etc/nginx/conf.d/*.conf

server_names_hash_bucket_size 64;

Update the types_hash_max_size value from 2048-4096

types_hash_max_size 4096;

Save the file & verify the syntax.

sudo nginx -t

And restart nginx

sudo systemctl restart nginx

Step 11: Install SSL For Hexo

Now, install certbot by typing

sudo apt install certbot

Hit the below command to generate the certificates then create a DHParams certificates.

sudo certbot certonly --standalone -d hexo.yourdomain.com --preferred-challenges http --agree-tos -n -m [email protected] --keep-until-expiring 
 sudo systemctl start nginx
 sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

You can host hexo sites on your server as well as with git,netlify,vercel & many more platforms.

So, this is how you can install & create a Hexo blog on Ubuntu 20.04.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article