使用Hexo搭建个人博客并部署到私人服务器

使用hexo搭建个人博客的过程。希望能够帮助到有需要的人。

2024/02/06 因为一些原因,已经迁移到了使用HugoAcademic-cv结合Github Pages重构了本站点.

本地环境

nodeJS环境

brew install node

初始化Hexo

# 安装依赖环境
$ npm install -g hexo-cli
$ npm install hexo-deployer-git --save

# 创建博客目录
$ mkdir Blog
$ cd Blog

# 博客初始化
$ hexo init
$ npm install

Hexo使用

# 新建页面
$ hexo new page categories
$ hexo new page tags
# 新建并发布草稿
$ hexo new draft my_draft
$ hexo publish my_draft
# 新建文章
$ hexo new post my_post
# 开启本地服务器
$ hexo server (--debug)
# 生成静态文件
$ hexo generate
$ hexo g
# 部署到服务器
$ hexo deploy
$ hexo d

密钥生成和提交

# 生成公钥和私钥
$ cd ~/.ssh
$ ssh-keygen
# 将公钥复制到服务器
$ ssh-copy-id username@address

服务器环境

环境搭建

# 软件安装
$ apt install nginx
$ apt install nodejs
$ apt install git
# 新建git用户
# 并添加到 /etc/sudoers
$ adduser git
$ vim /etc/passwd
# 修改git用户的默认shell为/ysr/bin/git-shell
# 禁止git用户用ssh登陆

仓库设置

# 来到仓库位置
$ cd /home/git
# 创建空仓库
$ git init --bare Blog.git
# 创建临时文件夹
$ mkdir tmp/Bog
# 设置git钩子
$ vim Blog.git/hooks/post-receive
# 设置权限
$ chmod +x Blog.git/hooks/post-receive
$ chown -R git:git Blog.git
$ chmod 777 -R Blog.git

使用的hooks脚本

#!/bin/bash -l

GIT_REPO=/home/git/Blog.git

TMP_GIT_CLONE=/home/git/tmp/Blog

PUBLIC_WWW=/var/www/blog

rm -rf ${TMP_GIT_CLONE}

git clone $GIT_REPO $TMP_GIT_CLONE

rm -rf ${PUBLIC_WWW}

cp -rf ${TMP_GIT_CLONE} ${PUBLIC_WWW}

另一个可选的脚本

#!/bin/sh
git --work-tree=/var/www/blog --git-dir=/home/git/Blog.git checkout -f

web服务器设置

# 创建静态文件目录
$ rm -rf /var/www/html
$ mkdir /var/www/blog
$ chmod 777 /var/www
# 设置nginx
$ vim /etc/nginx/site-available/default
$ mkdir /home/git/nginxlog
$ touch /home/git/nginxlog/blog_access.log
$ touch /home/git/nginxlog/blog_error.log
# 重启web服务器
$ service nginx restart

nignx配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    access_log  /home/git/nginxlog/blog_access.log;
    error_log  /home/git/nginxlog/blog_error.log;

    root /var/www/blog;
    index index.html

    location / {
        try_files $uri $uri/ =404;
    }
}

虚拟主机配置

$ cd /etc/nginx/sites-available
# 修改虚拟主机配置
# 单物理主机多虚拟主机
$ vim new_server
$ mkdir /var/www/new_server
$ touch /home/git/nginxlog/new_server_access.log
$ touch /home/git/nginxlog/new_server_error.log
# 链接激活
$ ln /etc/nginx/sites-available /etc/nginx/sites-enable
# 检查web服务器
$ service nginx status
$ service nginx restart

git自动部署

修改部署配置

deploy:
  type: git
  message: post-receive
  repo: git@address:/home/git/blog.git
  branch: master

接下来可以正常部署

$ hexo deploy
# 或者
$ hexo d

使用github pages

github提供了免费的github pages

可以创建一个仓库,仓库名为username.github.io

然后将静态文件上传即可使用

在deploy设置中更改对应的repo地址即可

Lei Yang
Lei Yang
PhD candidate

My research interests include visual speech recognition and semantics segmentation.