在VPS上搭建Hexo博客

starlin 699 2018-01-18

前言

最近在linode上买了VPS,之前一直是做梯子用的,发现有点浪费,于是想把GitHub上的博客搬到VPS上,期间看了很多搭建教程,发现还有点坑的啊,于是乎就有了这篇文章,仅仅做一个记录吧,本次仅记录VPS上搭建过程,本地部署Hexo就不写了,因为简单的多,好了,废话不多刷开始吧。

准备工作

原理如下:
在本地生产静态文件后,把静态文件部署到VPS上,这点和在github上没什么差别,然后在VPS上用Nginx直接做web服务。因hexo支持git部署,所以VPS上还需要安装Git
需要的工具如下:

  • Git
  • Nginx

开始安装

安装环境:CentOS7

安装Git

yum install -y git 

安装Nginx

  1. 在CentOS7上安装Nginx上需要先将Nginx添加到到YUM源

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

  2. 安装Nginx

    sudo yum install -y nginx

新建用户git,添加sudo权限

(其实也可以不用建用户,直接在root下操作)

adduser git
chmod 740 /etc/sudoers
vim /etc/sudoers

在vi编辑内容中找到如下内容:

## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL    

在 下面添加如下内容:

git     ALL=(ALL)       ALL

保存后退出执行

chmod 440 /etc/sudoers

创建git仓库,并配置ssh登陆

su git
cd ~
mkdir .ssh && cd .ssh
touch authorized_keys
//在这个文件中粘贴进刚刚申请的key(在id_rsa.pub文件中,路径为C:\Users\admin\.ssh,生成的过程就省略了)
vi authorized_keys
cd ~ 
mkdir hexo.git && cd hexo.git
git init --bare

设置完成后测试一下,如果在 本机下git bash中输入ssh git@VPS的IP地址 没有出现密码登陆的话,则表示设置成功了,
如果提示以下错误,有可能是权限不够

Error: Host key verification failed.

设置下目录权限即可

chmod 700 /home/username/.ssh
chmod 600 /home/username/.ssh/*

这里需要注意下,复制秘钥的一定要正确,当初我这里因为少一个空格,导致ssh连接时一直需要密码

创建网站根目录,并赋予git用户权限

cd /var/www
mkdir hexo
chown git:git -R /var/www/hexo

配置配置git hooks

su git
cd /home/git/hexo.git/hooks
vim post-receive    

在vi编辑框中输入一下内容:

#!/bin/bash
GIT_REPO=/home/git/hexo.git #git仓库
TMP_GIT_CLONE=/tmp/hexo
PUBLIC_WWW=/var/www/hexo #网站目录
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

然后赋予权限

chmod +x post-receive

配置Nginx

安装Nginx,Centos安装用yum install nginx 即可,其他方式略

这里建议按照Nginx官方的配置,然后修改相关配置即可
我的配置如下:

vim /etc/nginx/conf.d/hexo.conf

插入如下代码:

server {
    listen       80;
    server_name  域名;#这里最好输入你网站域名,我当时因为输入的VPS的ip地址,导致用域名访问的一直显示Nginx的欢迎页,这里坑的有点久
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        root   /var/www/hexo;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
  }

重启Nginx服务

systemctl restart nginx.service

开机启动

systemctl enable nginx

取消开机启动

systemctl disable nginx

配置https证书

这里偷个懒吧,直接放github连接了,请点击
手动更新证书命令:acme.sh --renew -d starlin.top

Webroot mode

假设服务器在运行着的,网站域名为 example.com,根目录为 /home/wwwroot/example.com。那么只需要执行下面这条语句就行。

acme.sh  --issue  -d example.com  -w /home/wwwroot/example.com

Apache / Nginx mode

acme.sh  --issue  -d example.com  --apache  # Apache

acme.sh  --issue  -d example.com  --nginx   # Nginx

如果找不到配置文件的话可以自行配置。

acme.sh  --issue  -d example.com  --nginx /etc/nginx/nginx.conf  # 指定nginx的conf

acme.sh  --issue  -d example.com  --nginx /etc/nginx/conf.d/example.com.conf  # 指定网站的conf

更改端口

一般我们会修改vps的默认端口22,修改了端口后再执行hexo d命令时会错,用下面的方式处理即可
在本机的.ssh目录下创建一个config文件,内容如下:

Host #VPS的ip
HostName #vps name,输入Ip也可以
User git #vps上的用户
Port 12307 # 端口,要和vps上的对应起来
IdentityFile ~/.ssh/id_rsa  # ssh私钥

配置git代理

在部署的时候,如果服务器不小心被Q了,部署会失败,这时我们需要设置git代理(前提是必须要有梯子)才能完成部署,命令如下:

// 查看当前代理设置
git config --global http.proxy
git config --global https.proxy

// 设置当前代理为 http://127.0.0.1:1080 或 socks5://127.0.0.1:1080
git config --global http.proxy 'http://127.0.0.1:1080'
git config --global https.proxy 'http://127.0.0.1:1080'

git config --global http.proxy 'socks5://127.0.0.1:10808'
git config --global https.proxy 'socks5://127.0.0.1:10808'

// 删除 proxy
git config --global --unset http.proxy
git config --global --unset https.proxy

// 只对github.com
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

// 取消代理
git config --global --unset http.https://github.com.proxy)

安装结束End

以上就是VPS上安装Hexo的过程,最主要的是Nginx安装,其他都还好。


# Hexo