vultr搭建hexo博客

准备工作

安装如下依赖:

  • gcc
  • make 跟git
  • nginx
  • openssl
  • nodejs跟npm

使用root用户安装这些依赖。

$ yum install -y gcc gcc-c++ make git openssl
$ yum install -y nginx
$ curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
$ yum update and yum install -y nodejs

也可以从自定义repo来安装nginx,新建nginx.repo文件,内容如下

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

复制到/etc/yum.repo.d/

$ cp nginx.repo /etc/yum.repo.d/
$ yum update and yum install -y nginx
$ systemctl enable nginx and systemctl start nginx

创建用户

需要创建一个普通用户用于运行博客,下面创建一个vultr用户,并将其添加到用户组wheel跟nginx。

$ useradd -d /home/vultr -m -r -U -s /bin/bash
$ passwd vultr
$ usermod -aG nginx vultr
$ usermod -aG wheel vultr

创建SSH秘钥

$ ssh-keygen -b 4096 -C "[email protected]"

将公钥复制到~/.ssh/authorized_keys

$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

安装hexo

切换到vultr用户,创建 ~/web/web/vultr~/web/git/vultr

### This is our hexo blog root directory
$ mkdir -p ~/web/web/vultr
### This is our git repository directory on the server
$ mkdir -p ~/web/git/vultr
### This is our deploy directory
$ mkdir -p ~/web/git/hexo

进入目录~/web/web/vultr, 然后安装hexo

$ cd ~/web/web/vultr
$ sudo npm install -g hexo-cli hexo-server
$ hexo init && npm install --save

编辑 _config.yml,然后生成静态页面:

$ hexo g

生成的静态页面位于~/web/web/vultr/public

安装nginx

现在还不能访问到网站,需要先配置nginx。

nginx一般使用nginx用户,nginx用户组来运行,这也是前面将vultr添加到nginx用户组的原因。

$ chown -R vultr:nginx ~/web

新增配置文件vultr.conf,放在~/web目录下面。

server {
    listen 80;
    listen [::]:80;
    ## if https is desired, please uncomment the following lines
    #listen 443 ssl http2;
    #listen [::]:443 ssl http2;

    server_name example.com, www.example.com;

    ## if forcing https, please uncomment the following lines
    #if ($scheme = http) {
    #    return 301 https://$server_name$request_uri;
    #}

    location / {
    root /home/vultr/web/web/vultr/public;
    index index.html;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    ## if https is desired, please uncomment the following lines
    #proxy_set_header X-Forwarded-Proto https;
    }
}

vultr.conf 复制到 /etc/nginx/config.d/ , 然后重启nginx。

$ sudo ln -sf ~/web/vultr.conf /etc/nginx/config.d/
$ sudo systemctl restart nginx

使用git部署博客

我们可以在本地撰写博客,然后推送到服务器,让hexo自动生成静态文件,使用vultr用户登录,创建一个git库。

$ cd ~/web/git/vultr
$ git init --bare
$ cd hooks

新建脚本post-receive用于部署博客

#!/bin/bash
deploy_to_dir="/home/vultr/web/git/hexo"

GIT_WORK_TREE=$deploy_to_dir git checkout -f master
echo "DEPLOY:   master  copied to  $deploy_to_dir"

hexo_dir="/home/vultr/web/web/vultr/"

cd $hexo_dir

hexo clean && hexo --silent g

if [[ $? == 0 ]]; then
    echo "Congratulations! Your blog has been correctly deployed"
else:
    echo "Unfortunately your blog has not been deployed correctly"
fi

在部署之前,还需要执行以下命令:

cd ~/web/web/vultr/
rm -rf source scaffolds _config.yml themes
ln -sf /home/vultr/web/git/hexo/themes/tranquilpeak  themes/
ln -sf /home/vultr/web/git/hexo/source .
ln -sf /home/vultr/web/git/hexo/scaffolds .
ln -sf /home/vultr/web/git/hexo/_config.yml .

配置本地git

首先确保本地电脑安装了git,然后新建git库。

$ mkdir -p ~/vultr/git/vultr`
$ cd ~/vultr/git/vultr and git init
$ git remote add production ssh://[email protected]:/home/vultr/web/git/vultr

然后安装hexo并撰写博客:

$ mkdir ~/vultr/vultr && cd ~/vultr/vultr
$ hexo init && npm install --save
$ mv  source  _config.yml themes scaffolds ~/vultr/git/vultr
$ ln -sf ~/vultr/git/vultr/source .
$ ln -sf ~/vultr/git/vultr/_config.yml .
$ ln -sf ~/vultr/git/vultr/scaffolds .
$ hexo new "Test Post"

接下来就可以推送博客了:

$ cd ~/vultr/git/vultr
$ git add . && git commit -m "new post"
$ git push production master

你可能感兴趣的:(vultr搭建hexo博客)