hexo部署博客并上传到域名上

文章转载来自使用 Hexo + Github 搭建自己的博客(图文教程)
替换主题参考Hexo 安装和替换主题、自定义博客主题
由于github部署的博客连接较慢,因此在购买腾讯云服务器之后将博客内容部署到腾讯云上,特此记录一下过程。

搭建过程

安装git node.js

查看版本命令用

npm -v
node -v

新建仓库

新建一个repository,名称为 name.github.io

安装Hexo

npm install hexo -g
hexo -v
hexo init
npm install
hexo g
hexo s  #查看本地新建的hexo
npm install hexo-deployer-git --save

修改_config.yml

deploy:
  type: git
  repository: git@github.com:sjl3110/sjl3110.github.io.git
  branch: master

建立ssh密钥

ssh-keygen -t rsa -C "mail@xx.com"

并将默认保存位置的id_rsa.pub内容存放到github网站中的SSH密钥中。

维护过程

hexo new post "blog-name"
hexo d -g

云服务器部署

服务器安装CentOS 7操作系统,首先安装Nodejs和git:

yum install nodejs
yum install git

创建git用户和仓库

adduser git
chmod 740 /etc/sudoers
vim /etc/sudoers
=== root    ALL=(ALL)     ALL
+++ git     ALL=(ALL)     ALL
chmod 400 /etc/sudoers

sudo passwd git
su git
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
#然后将电脑中执行 C:\Users\28236\.ssh\id_rsa.pub ,将公钥复制粘贴到authorized_keys
linux: cat ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
ssh -v git@SERVER

# su git  切换用户进行后续操作
cd /home/git/
mkdir -p projects/blog # 把项目目录建立起来
mkdir repos && cd repos
git init --bare blog.git # 创建仓库
cd blog.git/hooks
vim post-receive # 创建一个钩子
+++ #!/bin/sh
+++ git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f
chmod +x post-receive # 添加可执行权限
exit # 返回到root用户
chown -R git:git /home/git/repos/blog.git # 给git用户添加权限

使用nginx反向代理

安装nginx及使用:

yum install nginx
nginx -s reload  #重载配置文件
nginx -s stop
sudo service nginx restart
systemctl enable nginx.service  #设置开机自启动

配置nginx,修改/etc/nginx/nginx.conf:

user root;         //用户
...
server {
        listen       80;
        # listen       [::]:80 default_server;
        server_name  www.jianliang-shen.cn jianliang-shen.cn;   //个人域名
        root         /home/git/projects/blog;                  //仓库位置

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

保存后重新部署。在公网中尝试连接,一般来说是失败。

开放云服务器端口

因为腾讯云服务器并没有开放端口无法外部ping通,因此需要在“安全规则中”开放端口,如下所示:

fig1

设置Hexo仓库

deploy:
    type: git
    repo: git@server_ip:/home/git/repos/blog.git    
    branch: master

提交更新:

hexo d -g

配置域名解析

例如aliyun的域名解析设置如下:

fig2

域名备案

各类云服务器在绑定域名时都需要备案,否则无法使用,例如我用的阿里云域名和腾讯云服务器,就需要在腾讯云中登记公网ip绑定的域名。