当前位置: 首页 > 后端技术 > Java

Jekyll博客搭建及自动部署

时间:2023-04-01 22:20:54 Java

这篇文章旨在教小伙伴们使用Jekyll和阿里云centos7服务器以及github自动搭建自己的服务器,同时记录下自己的搭建过程。中间踩了一些坑,参考了大神们的搭建步骤。如有不足之处,请更正服务器安装步骤。安装Node环境,执行以下命令:wgethttps://nodejs.org/dist/v8.12.0/node-v8。12.0-linux-x64.tar.xzxz-dnode-v8.12.0-linux-x64.tar.xztar-xfnode-v8.12.0-linux-x64.tarln-s~/node-v8.12.0-linux-x64/bin/node/usr/bin/nodeln-s~/node-v8.12.0-linux-x64/bin/npm/usr/bin/npmnode-vnpm-vinstallruby??Jekyll依赖Ruby环境,需要安装Ruby,执行以下命令:wgethttps://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.gzmkdir-p/usr/local/rubytar-zxvfruby??-2.4.4.tar.gzcdruby??-2.4.4./configure--prefix=/usr/local/rubymake&&makeinstall环境变量设置:cd~vim.bash_profile在.bash_profile文件中设置如下内容:PATH=/usr/local/ruby/bin:$PATH:$HOME/binexportPATH安装gcc安装gcc执行以下命令:yum-yupdategccyuminstallgcc-c++libstdc++-devel或yum-yinstallgcc+gcc-c++如果更新失败,请看https://www.cnblogs.com/bpzblog/p/13918199.htmlhttps://www.jianshu.com/p/68a5b33099d2安装jekyll最后安装Jekyll,执行以下命令yuminstallzlib-develyuminstallgemgemupdategem安装jekylljekyll--versiongemupdate--sy如果错误信息ERROR:Loadingcommand:install(LoadError)cannotloadsuchfile--zlibERROR:Whileexecutinggem...(NoMethodError)undefinedmethod`invoke_with_build_args'fornil:NilClassisreportedduringsteminstallation,进入rubysource文件夹(如果按照上面的步骤,cd~ll会看到)安装ruby自己提供的zlib包cdext/zlibrubyextconf.rb--with-zlib-dir=/usr/local/zlib如果有此时出现错误,没有规则来创建目标`zlib.o'所需的`/include/ruby.h'。停止。事实上,ext/zlib/Makefile忘记给路径变量top_srcdir赋值。当然调用的时候报错了。修改Makefile并添加top_srcdir=../..makemakeinstall如果不能解决问题,请参考报错,请看这里继续geminstalljekyll如果报错Unabletorequireopenssl,installOpenSSLandrebuildruby??(preferred)oruse非HTTPS源,然后1.yuminstallopenssl-devel-y2。在ruby安装包/root/ruby-2.4.4/ext/openssl中,执行ruby./extconf.rb3。执行make,如果make:***Noruletomaketarget`/include/ruby.h','ossl.o'需要。停止。;在Makefile的顶部添加top_srcdir=../..。添加完成后,保存并重新执行make4。执行make5.执行makeinstall解决问题gemupdate--systemcontinuegeminstalljekyllinstallJekyll成功部署我的博客installgitprepareyuminstallgit然后请自己fork或者直接forkjekyll源码到你自己linux下的github我的地址:逍遥风github地址gitclone解压后复制地址进入压缩文件夹,然后bundleupdate,然后bundleexecjekyllserve(report如果有依赖错误不多,重复bundleupdate(直到更新成功),如果依赖错误较多,删除目录下的Gemfile.lock文件,执行bundleupdate)至此jekyll可以启动成功,并且可以通过curl命令查看服务器例如curlwww.xiaoyaowind.com将博客部署到Nginx服务器上新建一个文件/etc/yum.repos.d/nginx.repo编辑以下内容并保存[nginx]name=nginxrepobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1执行nginx安装命令yuminstallnginxNginx配置成功后,需要设置Nginx配置.配置文件路径为/etc/nginx/conf.d/default.conf,配置内容如下:server{listen80;server_namexiaoyaowind.comwww.xiaoyaowind.com;位置/{root/root/blog/html;indexindex.htmlindex.htm;}error_page404/404.html;error_page500502503504/50x.html;location=/50x.html{root/root/blog/html;Nginx服务器安装成功后,需要将Jekyll编译好的博客静态html文件输出到Nginx服务器(个人理解记得加bundleexec相当于windows下使用兼容格式或者忽略版本差异模式编译)cdgit在博客文件夹下例如:cd/xiaoyaowind.github.iobundleexecjekyllbuild--destination=/root/blog/html启动Nginx服务器,可以正常访问博客页面启动nginxcd/usr/如果需要在浏览器上访问sbin./nginx,需要在阿里云ECS控制台的安全组件中暴露80端口。如果你想通过域名访问,你需要将域名解析设置指向你的服务器。(这是自己百度)非www域名到www的重定向比如我要访问http://xiaoyaowind.com,重定向到http://www.xiaoyaowind.com,需要配置Nginx文件/etc/nginx/conf.d/default.conf,修改配置如下:listen80;server_namexiaoyaowind.comwww.xiaoyaowind.com;if($host!='www.xiaoyaowind.com'){重写"^/(.*)$"http://www.xiaoyaowind.com/$1永久;}自动化部署通过设置github的webhook,可以实现自动化构建和部署。流程是,提交一篇博文或者配置到github仓库,仓库会触发你设置的webhook,向你设置的webhook地址发送post请求,比如我设置的请求是运行在上面的Nodejs程序服务器端,监听gitubwebhook请求,收到请求后,会执行shell命令。设置webhook首先设置github仓库的webhook。在github仓库的项目界面,对比我的项目界面https://github.com/xiaoyowind...,点击Setting->Webhooks->AddWebhook,添加Webhooks配置信息,我的配置信息如下:PayloadURL:http://www.xiaoyaowind.com/incomingContenttype:application/jsonSecret:aaaaa12345678这样Webhook就设置成功了。安装监听组件并自动构建它。现在在博客所在的服务器上监听GithubWebhook发送的请求。我使用一个开源组件来监控github-webhook-handler。项目地址为:https://github.com/rvagg/github。..,首先安装:npminstall-ggithub-webhook-handler安装成功后,在/root/node-v8.12.0-linux-x64/lib/node_modules/github-webhook-handler下新建deploy.js文件:varhttp=require('http')varcreateHandler=require('github-webhook-handler')varhandler=createHandler({path:'/incoming',secret:'aaaaa12345678'})functionrun_cmd(cmd,args,回调){varspawn=require('child_process').spawn;varchild=spawn(cmd,args);varresp="";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=404res.end('没有这样的位置')})}).listen(3002)handler.on('error',function(err){console.error('Error:',err.message)})handler.on('推',功能(事件){console.log('收到%s到%s的推送事件',event.payload.repository.name,event.payload.ref);run_cmd('sh',['./start_blog.sh'],function(text){console.log(text)});})上面代码中指定了nodejs服务的踩踏端口为3002,监听path/incoming,Secret为aaaaa12345678,应该和之前GithubWebhook设置一致代码run_cmd('sh',['./start_blog.sh'],指定接收到后执行./start_blog.shrequest,start_blog.sh文件的代码如下,先进入博客的代码文件,拉取代码,编译。echo`date`cd/root/xiaoyaowind.github.ioechostartpullfromgithubgitpullechostartbuild..bundleexecjekyllbuild--destination=/root/blog/html然后需要使用forever启动deploy.js服务,执行命令如下:sudonpminstallforever-g#install$foreverstartdeploy.js#start$foreverstopdeploy.js#close$foreverstart-lforever.log-oout.log-eerr.logdeploy.js#output日志和错误/root/node-v8.12.0-linux-x64/lib/node_modules/forever/bin/foreverstart-lforever.log-oout.log-eerr.logdeploy.js如果报错:/root/node-v8.12.0-linux-x64/lib/node_modules/forever/bin/foreverstart-a-lforever.log-oout.log-eerr.logdeploy.jsmyselfdeploy.js和start_blog.sh在/root/node-v8.12.0-linux-x64/lib/node_modules/github-webhook-handler目录下,如果启动失败,请查看err.log。如果日志中有3002,那是因为端口已经被占用了。重启服务器,如果报错,按照步骤进入指定目录执行上述命令。最后一步是在nginx服务器的配置文件中添加配置。需要将监听到的/incoming请求转发给nodejs服务。配置代码如下:location=/incoming{proxy_passhttp://127.0.0.1:3002/incoming;}这样当你向gitub提交文章或修改文件时,github通过webhook,服务端收到请求后执行sh命令。sh命令包含了重新拉取代码和编译代码的过程,这样自动部署就完成了,你只需要提交代码,服务器就会触发拉取代码和重新编译的动作。好了,以上就是我实践过的jekyll和nginx自动部署博客系统的步骤。本文由多帖博客平台OpenWrite发布!