
今天我们来介绍一下在 Ubuntu 20.04 上,如何使用 Hexo 安装和创建博客。Hexo 是一个基于 Node.js 的静态博客平台。借助于 hexo ,我们可以用 Markdown 编辑器撰写文章。它借助预先构建的主题将博客文章转换为静态 HTML 文件。hexo 与其他博客平台不同,因为它使用静态文件而不是动态加载。所以,在这里我们将介绍如何在 Ubuntu 20.04 系统上安装 hexo 。
第一步:更新系统
更新系统以安装更新:
sudo apt update -y
sudo apt upgrade -y然后,安装所需的包:
sudo apt install git 
git config --global user.name "Your Name" 
git config --global user.email " youremail@domain.com "注意:替换成自己的 git 用户名、电子邮件和域名。
第二步:在 Ubuntu 20.04 上配置防火墙
启用 SSH 端口允许防火墙访问。如果是第一次安装,请不要忘记启用防火墙。
sudo ufw enable然后开启4000端口:
sudo ufw allow 4000确保允许 http、https 流量, 然后验证防火墙的状态。
sudo ufw allow http
sudo ufw allow https
sudo ufw statusoutput:
  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:~$ 第三步:在 Ubuntu 20.04 上安装 Node.js
使用下面的命令在 Ubuntu 20.04 上安装 node.js 。
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs验证安装的版本:
node --version第四步:在 Ubuntu 20.04 上安装 Hexo
执行以下命令以在 Ubuntu 20.04 上安装 Hexo
sudo npm install hexo-cli -g安装后,创建一个新目录来安装 Hexo
sudo mkdir -p /var/www/hexo给它适当的权限:
sudo chown -R $USER:$USER /var/www/hexo
sudo chmod -R 755 /var/www/hexo现在切换到 hexo 目录并初始化它。
cd /var/www/hexo 
hexo initINFO  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!最后,执行下面的命令安装 hexo
npm install第五步:在 Ubuntu 20.04 上配置 Hexo
编辑 _config.yml 文件:
sudo nano _config.yml然后,根据需要更改选项的名称:
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注意:配置域名、URL、站点标题和其他必需选项并再做二次更改。
- 将 default_layout 值从帖子更改为草稿。
- 将 post_asset_folder 变量更改为 true。
第六步:安装博客主题
由于 hexo 带有默认主题,可以通过 hexo 主题页面更改成自己喜欢的主题。
在这里,我们将安装 Next 主题进行测试。切换到 /var/www/hexo/ 目录并下载主题:
cd /var/www/hexo 
git clone https://github.com/theme-next/hexo-theme-next themes/next下载后,编辑 _config.yml 以更新主题。
sudo nano _config.yml更改主题变量以安装新主题:
Extensions
 Plugins: https://hexo.io/plugins/
 Themes: https://hexo.io/themes/
 theme: next为了更新主题设置,要配置/var/www/hexo/themes/next/_config.yml 文件。
第七步:创建第一篇文章
执行下面的命令来创建第一篇文章。
hexo new first-post
 INFO Validating config
 INFO Created: /var/www/hexo/source/_drafts/first-post.md创建后,触发以下命令以编辑新文章。
sudo nano ./source/_drafts/first-post.md每个帖子都有自己的前台页面。它由标题、发布日期、类别、标签等组成。使用实际信息更新对应内容。
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!要在文章中放置图像,请在文章中添加以下命令。
{% asset_img "example.jpg" "This is an example image" %}注意:将 example.jpg 替换为实际的图像名称,然后将该文件复制到 \source_posts\first-pos t 目录。
保存并退出文件。
最后通过键入发布帖子:
hexo publish first-post第八步:为 Hexo 安装插件
从 hexo 插件页面获取 hexo 的插件。可以根据自己的需要安装插件。
cd /var/www/hexo 
npm i hexo-filter-nofollow --save然后编辑下面的文件
sudo nano _config.yml在上述文件的末尾添加以下几行。
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. 第九步:测试 Hexo 服务器
执行以下命令来测试 hexo 服务器。
hexo server然后在浏览器中输入 IP:4000,将看到之前创建的文章的欢迎页面。
只需按 Ctrl+C 即可停止。
第十步:生成 Hexo 静态文件
点击以下命令生成静态文件
hexo generate这些文件可以在 /var/www/hexo/public 找到。接下来安装 Nginx。
执行下面命令来安装 nginx:
sudo apt install nginx编辑 hexo 配置文件
sudo nano /etc/nginx/sites-available/hexo.conf然后将以下内容添加到配置文件中:
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;
  }然后保存并退出文件。
创建配置文件的软链接:
sudo ln -s /etc/nginx/sites-available/hexo.conf /etc/nginx/sites-enabled/接下来,执行下面的命令来编辑 nginx 配置文件:
sudo nano /etc/nginx/nginx.conf在 include /etc/nginx/conf.d/*.conf 行之前添加下面的行:
server_names_hash_bucket_size 64;更新 types_hash_max_size 值(从 2048-4096)
types_hash_max_size 4096;保存文件并验证语法:
sudo nginx -t重启nginx:
sudo systemctl restart nginx第 11 步:为 Hexo 安装 SSL
现在,通过下面的命令安装 certbot :
sudo apt install certbot点击以下命令生成证书,然后创建 DHParams 证书。
sudo certbot certonly --standalone -d hexo.yourdomain.com --preferred-challenges http --agree-tos -n -m email@yourdomain.com --keep-until-expiring 
 sudo systemctl start nginx 
 sudo openssl dhparam -out / etc/ssl/certs/dhparam.pem 2048可以在服务器、 git、netlify 和 vercel 等平台上托管 hexo 网站。这就是在 Ubuntu 20.04 上安装和创建 Hexo 博客的方法。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/install-and-create-blog-with-hexo-on-ubuntu-20-04/
