CentOS搭建hexo部落格,實現自動化部署(二)
本篇文章為hexo伺服器端搭建,完成自動化部署。
安裝git,請參考ofollow,noindex">Windows7安裝Git客戶端
連線Github,請參考Window7下配置SSH連線GitHub
本機安裝hexo,請參考CentOS搭建hexo部落格,實現自動化部署(一)
前期準備工作
adduser git#建立一個git賬戶,啟用linux ll /etc/sudoers chmod 740 /etc/sudoers vim /etc/sudoers#新增sudo許可權 ## Allow root to run any commands anywhere rootALL=(ALL)ALL gitALL=(ALL)ALL chmod 400 /etc/sudoers
各種安裝 -git nodejs nginx
yum install git yum install -y nodejs sudo yum install -y nginx
配置nginx
sudo vi /etc/nginx/nginx.conf ########################################################################################## server { listen80 default_server; listen[::]:80 default_server; server_namewww.igkor.com igkor.com; root/usr/share/nginx/html/blog;#設定nginx跟目錄,我這裡是/usr/share/nginx/html/blog indexindex.html index.htm # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } ########################################################################################## sudo mkdir -p /usr/share/nginx/html/blog#建立根目錄 sudo chown -R git:git /usr/share/nginx/html/blog#將組和擁有者變為git service nginx restart#重啟應用
安裝github-webhook-handler
cd /usr/share/nginx/html npm install -g github-webhook-handler#安裝github-webhook-handler ###如果沒有安裝成功,可以選擇法2來安裝 npm install -g cnpm --registry=http://r.cnpmjs.org cnpm install -g github-webhook-handler
建立deploy.sh,指令碼內容如下:
cd /usr/share/nginx/html/blog git reset --hard git pull origin master hexo generate
建立webhooks.js,指令碼內容如下:
1、如果在deploy.log看到 Error: Cannot find module 'github-webhook-handler',則說明github-webhook-handler路徑沒有配置正確,需要檢視github-webhook-handler安裝在了哪裡,我的在 /usr/lib/node_modules/github-webhook-handler
2、我的埠使用的是6666,需要確認安全組策略是否放開6666。
var http = require('http') var createHandler = require('/usr/lib/node_modules/github-webhook-handler') var handler = createHandler({ path: '/', secret: 'NIxCkDI5BPG4' }) // 上面的 secret 保持和 GitHub 後臺設定的一致 function run_cmd(cmd, args, callback) { var spawn = require('child_process').spawn; var child = spawn(cmd, args); var resp = ""; child.stdout.on('data', function(buffer) { resp += buffer.toString(); }); child.stdout.on('end', function() { callback (resp) }); } http.createServer(function (req, res) { handler(req, res, function (err) { res.statusCode = 404 res.end('no such location') }) }).listen(6666) handler.on('error', function (err) { console.error('Error:', err.message) }) handler.on('push', function (event) { console.log('Received a push event for %s to %s', event.payload.repository.name, event.payload.ref); run_cmd('sh', ['./deploy.sh',event.payload.repository.name], function(text){ console.log(text) }); })
啟動webhooks.js,完成自動化部署。
nohup node webhooks.js > deploy.log &#deploy.log可以用來排錯。 ll /usr/share/nginx/html/blog#愉快的檢視檔案更新時間吧,自動化部署已經完成。