Hexo 博客搭好了有差很少两周时间了,这期间走了不少弯路,跳了不少坑。一些坑本身 bing 到了答案,找到了解决方法,一些坑则是本身摸索出来的解决方法。如今准备写几篇关于搭建流程、搭建过程当中遇到的问题和解决方法。俗话说得好,好记性不如烂键盘嘛。php
暂时准备写三篇关于 Hexo 博客搭建的博文,博客园只放了其中第一篇部署过程,后面两篇在简书和石匠屋能够找到:css
本博客是关于 Hexo 博客搭建的第一篇,经过三步简述如何将 hexo 博客部署到腾讯云的服务器上。html
- 云服务器端 git 的配置
- Nginx 的配置
- 本地端 hexo 的设置更改
本博客中全部操做使用的服务器环境java
操做系统 | CPU | 内存 | 带宽 |
---|---|---|---|
CentOS 7.2 64位 | 1核 | 2GB | 1Mbps |
安装依赖库和编译工具node
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install gcc perl-ExtUtils-MakeMaker package
下载 gitpython
/usr/local/src
目录cd /usr/local/src
/usr/local/src
文件夹里wget https://www.kernel.org/pub/software/scm/git/git-2.16.2.tar.gz
解压编译 gitnginx
git-2.16.2.tar.gz
tar -zvxf git-2.16.2.tar.gz
git-2.16.2.tar.gz
目录下cd git-2.16.2
make all prefix=/usr/local/git
/usr/local/git
目录下make install prefix=/usr/local/git
配置 git 环境变量git
echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc
source /etc/bashrc
查看 git 版本vim
git --version
若是此时能查看到 git 的版本号,说明咱们已经安装成功了。浏览器
home/git
的目录下,建立一个名为hexoBlog
的裸仓库。home/git
目录,须要先建立;而后修改目录的全部权和用户权限。mkdir /home/git/
chown -R $USER:$USER /home/git/ chmod -R 755 /home/git/
而后,执行以下命令:
cd /home/git/ git init --bare hexoBlog.git
刚才这一步主要建立一个裸的 git 仓库。
建立一个新的 git 钩子,用于自动部署。
/home/git/hexoBlog.git
下,有一个自动生成的 hooks
文件夹。咱们须要在里边新建一个新的钩子文件 post-receive
。vim /home/git/hexoBlog.git/hooks/post-receive
i
键进入文件的编辑模式,在该文件中添加两行代码(将下边的代码粘贴进去),指定 Git 的工做树(源代码)和 Git 目录(配置文件等)。#!/bin/bash git --work-tree=/home/hexoBlog --git-dir=/home/git/hexoBlog.git checkout -f
而后,按 Esc
键退出编辑模式,输入:wq
保存退出。
chmod +x /home/git/hexoBlog.git/hooks/post-receive
到一步为止,git 仓库彻底搭建好了。下面进行 Nginx 的配置。
yum install -y nginx
service nginx start
wget http://127.0.0.1
可以正常获取如下欢迎页面说明Nginx安装成功。
Connecting to 127.0.0.1:80... connected. HTTP request sent, awaiting response... 200 OK Length: 43704 (43K) [text/html] Saving to: ‘index.html’ 100%[=======================================>] 43,704 --.-K/s in 0s 2018-07-12 11:04:09 (487 MB/s) - ‘index.html’ saved [43704/43704]
测试网页是否能打开
在浏览器中输入服务器 ip 地址,就是服务器的公网 ip。
配置 Nginx 托管文件目录
/home/hexoBlog
目录,用于 Nginx 托管。mkdir /home/hexoBlog/
chown -R $USER:$USER /home/hexoBlog/ chmod -R 755 /home/hexoBlog/
nginx -t
vim /etc/nginx/nginx.conf
server {
listen 80 default_server; listen [::]:80 default_server; root /home/hexoBlog; #须要修改 server_name evenyao.com; #博主的域名,须要修改为对应的域名 # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { }
按i
键进入插入模式,将其中的 root 值改成 /home/hexoBlog
(刚才建立的托管仓库目录)。
将 server_name 值改为本身相应的域名地址
。
service nginx restart
到这一步为止,服务器端配置就结束了。接下来,就只剩本地 hexo 的配置了。
deploy:
type: git
repo: root@xx.xx.xx.xx:/home/git/hexoBlog //xx.xx.xx.xx为服务器地址 branch: master
cd 你的 hexo 目录 hexo clean hexo generate hexo deploy
若出现以下报错,为 git 没有设置SSH key
,建立SSH key
以后从新 hexo d
便可
Error: ssh: Could not resolve hostname cvm XX.XX.XX.XX : nodename nor servname provided, or not known fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. at ChildProcess.<anonymous> (/Users/Yao/Desktop/hexoBlog/node_modules/hexo-util/lib/spawn.js:37:17) at ChildProcess.emit (events.js:182:13) at maybeClose (internal/child_process.js:961:16) at Socket.stream.socket.on (internal/child_process.js:380:11) at Socket.emit (events.js:182:13) at Pipe._handle.close (net.js:595:12)