一开始将本身hexo
部署到github
,结果发现打开页面速度有点慢,而后又将其同时部署到coding
,实现双线路访问,国内解析记录到coding
,国外解析到github
,这样确实网站的速度能提升很多,可是国内访问由于是通过coding
,因此打开网站会有广告,这点不能容忍,因而想到本身的服务器也还空闲着,因而想到能够部署到本身的服务器上,折腾开始
演示站点php
我的使用的环境html
Windows10
(64位)CentOS
7.2 64位)整个部署过程node
git
,NodeJs
,hexo
..)git
,Nginx
)dnspod
)生成ssh公钥(以前生成过的也就不用再执行了,好比部署在github上填写过rsa密钥就不须要再进行这一步了)nginx
ssh-keygen -t rsa -C "邮件地址"
~/.ssh
目录中,看到有id_rsa
,id_rsa.pub
这些文件便可安装git及nodejsgit
yum install git #安装NodeJS curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
建立git帐户github
adduser git chmod 740 /etc/sudoers vim /etc/sudoers
添加内容
找到web
## Allow root to run any commands anywhere root ALL=(ALL) ALL
添加如下内容vim
git ALL=(ALL) ALL
保存退出并改回权限segmentfault
chmod 400 /etc/sudoers
设置git帐户密码tomcat
sudo passwd git
切换至git
用户,建立 ~/.ssh
文件夹和 ~/.ssh/authorized_keys
文件,并赋予相应的权限
su git mkdir ~/.ssh vim ~/.ssh/authorized_keys #而后将本地电脑中执行 cat ~/.ssh/id_rsa.pub | pbcopy ,将公钥复制粘贴到 authorized_keys chmod 600 ~/.ssh/authorzied_keys chmod 700 ~/.ssh
git
ssh -v git@SERVER
SERVER
为填写本身的云主机IP
建立目录
# repo 做为为git仓库目录 mkdir -R /var/repo # hexo 做为网站根目录 mkdir -R /var/www/hexo
配置nginx
(固然Apache
也是能够的,nginx
的安装步骤省略)
首先输入ngixn -t
找到配置文件,个人是在/etc/nginx/nginx.conf
,配置SERVER
server { listen 80; # server_name 填写本身的域名 server_name www.fayne.cn; # 这里root填写本身的网站根目录 root /var/www/hexo; index index.html index.php index.htm; #/usr/local/tomcat/webapps/Forum # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } location ~ .php$ { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
dnspod
设置解析记录,设置解析A
记录www
解析到服务器IP地址
, 解析线路默认CNAME
解析使www
解析到xxx.github.io
,解析线路国外,这里的xxx
为hexo
部署在github
的仓库名称,这样保证了在国外访问速度也是极佳的git
自动化部署博客自动化部署主要用到了git
-hooks
同步
服务器创建裸库,这里要用git
用户登陆,确保git
用户拥有仓库全部权
su git cd /var/repo/ git init --bare blog.git
使用 git-hooks 同步网站根目录
在这里咱们使用的是 post-update
这个钩子(也有多是post-receive
,具体进入文件就知道了),当git有收发的时候就会调用这个钩子。 在 /var/repo/blog.git
裸库的 hooks
文件夹中
vim /var/repo/blog.git/hooks/post-update # 编辑文件,写入如下内容
#!/bin/sh git --work-tree=/var/www/hexo --git-dir=/var/repo/blog.git checkout -f
保存后,要赋予这个文件可执行权限
chmod +x post-update
配置_config.yml
,完成自动化部署
打开_config.yml
, 找到deploy
deploy: type: git repo: github: git@github.com:Finhoo/Finhoo.github.io.git www: git@www.fayne.cn:/var/repo/blog.git branch: master
保存后,便可测试部署
hexo clean && hexo g -d
github
快多了,国外速度也是很好我在部署过程当中,执行 hexo d
发现部署总是出错,什么权限不容许之类的,这里咱们须要检查咱们在上述的git操做部署是否使用了git
用户操做,如果没有,须要给相应的目录更改用户组
使用chown -R git:git /var/repo/
这条命令递归的将repo
目录及其子目录用户组设置为git
,同时chown -R git:git /var/www/hexo
,这样便可解决此类问题