Hexo博客部署到个人服务器

本文跳过阿里云创建git仓库、hexo部署到github的步骤,有需要的可以移步下面博客地址查看:

  1. 在阿里云服务器上创建git远程仓库

  2. 使用Hexo建立博客

一、服务器相关配置

本文使用hexo在本地生成静态文件,然后将静态文件提交到个人服务器的git仓库,最后用Nginx提供web服务的方式。

1、Nginx配置

Nginx正常配置一个虚拟主机来存放静态文件即可。

server
     {
         listen 80;
         #listen [::]:80;
         server_name sjzlai.qicunshan.com ;
         index index.html index.htm index.php default.html default.htm default.php;
         root  /home/wwwroot/blog.qicunshan.com/blog;

         #include other.conf;
         #error_page   404   /404.html;

         # Deny access to PHP files in specific directory
         #location ~ /(wp-content|uploads|wp-includes|images)/.*.php$ { deny all; }

         include enable-php.conf;

         location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
         {
             expires      30d;
         }

         location ~ .*.(js|css)?$
         {
             expires      12h;
         }

         location ~ /.well-known {
             allow all;
         }

         location ~ /.
         {
             deny all;
         }

         access_log  /home/wwwlogs/blog.qicunshan.com.log;
     }

需要注意的是:

  • git用户的权限;

  • 虚拟主机路径,且默认主页是index.html。

2、创建git仓库

    #创建仓库目录
    mkdir blog.git
    #进入仓库目录
    cd blog.git
    #创建仓库
    git init --bare

创建完后可以使用下面命令测试一下仓库地址,克隆成功说明没有问题。


git clone 仓库地址

3、Git hooks配置

  • 新建脚本,使git hooks每次push完成之后,将仓库中的静态文件复制到Nginx对应的虚拟主机中;
 #新建脚本
 vim post-receive
脚本内容如下:
#!/bin/bash -l
#git仓库目录
GIT_REPO=/var/git/blog.git
#临时文件目录
TMP_GIT_CLONE=/var/git/tmp/blog
#虚拟主机目录
PUBLIC_WWW=/home/wwwroot/blog.qicunshan.com
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE} ${PUBLIC_WWW}
  • git用户关于post-receive脚本及内容的权限、虚拟主机的权限:
    #赋予文件、文件夹权限
    chmod 777 post-receive
    chmod 777 -R /var/git/tmp
    chmod 777 -R /home/wwwroot/blog.qicunshan.com
    #赋予git用户权限
    chown git:git -R /var/git/tmp
    chown git:git -R /home/wwwroot/blog.qicunshan.com

二、本机Hexo配置

    deploy:
      type: git
      #repo: git@github.com:qicunshan/qicunshan.github.io.git
      repo:
        github: git@github.com:qicunshan/qicunshan.github.io.git
        vps:  git@服务器地址:/home/hexo.git
      branch: master

然后在hexo目录下执行Hexo g -d即可。

原文地址:https://www.cnblogs.com/sjzlai/p/9359944.html