[{"data":1,"prerenderedAt":653},["ShallowReactive",2],{"blog-post-2020-04-18-from-wordpress-to-hexo":3},{"id":4,"title":5,"body":6,"categories":618,"date":620,"description":134,"extension":621,"meta":622,"navigation":644,"path":645,"seo":646,"stem":647,"summary":648,"tags":649,"__hash__":652},"blog\u002Fblog\u002Ffrom-wordpress-to-hexo.md","从 Wordpress 迁移到 Hexo",{"type":7,"value":8,"toc":610},"minimark",[9,13,22,49,52,67,70,73,76,79,107,110,113,118,129,136,140,143,188,192,197,209,218,224,230,234,246,252,256,269,282,288,303,306,315,321,324,327,331,345,353,356,362,365,371,374,386,391,398,404,407,416,434,445,452,455,458,461,467,470,484,487,527,531,537,543,551,554,559,567,571,574,597,604],[10,11,12],"h3",{"id":12},"写在最前面的话",[14,15,16,17,21],"p",{},"大概从 03 年开始接触博客到现在，已有十多年。早期换过无数的 BSP，比如 Blogbus、Live Space、百度空间、Blogger 等；也通过自建服务的方式，搭建过 Wordpress。由于这样那样可抗或不可抗的缘由，搬家无数次，用过第三方服务、免费虚拟主机、合租虚拟主机、VPS。遭遇过各种域名、主机事故，比如被封、被黑、服务中断等，本博客却从未中断，主要内容也通过一次次的迁移得以延续。能延续这么久（虽然最近一些年，由于自己懒，长时间处于半停滞状态），个人感觉最重要的一点在于，购买了博客专用的顶级域名 ",[18,19,20],"code",{},"interjc.net","，这个域名伴随了各个时期不同形态的本博客，也延续了与博友和读者之间的羁绊。",[14,23,24,25,32,33,38,39,44,45,48],{},"自从",[26,27,31],"a",{"href":28,"rel":29},"https:\u002F\u002Fblog.interjc.net\u002Fhomezz-host\u002F",[30],"nofollow","东哥的虚拟主机服务","关停以后，本博客就一直托管在 ",[26,34,37],{"href":35,"rel":36},"https:\u002F\u002Fwww.vultr.com\u002F?ref=7500552",[30],"Vultr VPS"," 上，速度和稳定性都还 OK，并且由于是 VPS，还通过 ",[26,40,43],{"href":41,"rel":42},"https:\u002F\u002Fletsencrypt.org\u002F",[30],"Let's encrypted"," 启用了 ",[18,46,47],{},"https","，整个使用还是比较平滑的。",[10,50,51],{"id":51},"契机",[14,53,54,55,60,61,66],{},"虽然 ",[26,56,59],{"href":57,"rel":58},"https:\u002F\u002Fwordpress.org\u002F",[30],"Wordpress"," 和 ",[26,62,65],{"href":63,"rel":64},"https:\u002F\u002Fhexo.io\u002F",[30],"Hexo"," 都是比较优秀的 CMS 系统，所以在此不讨论他们孰优孰劣的问题。他们之间的区别是显而易见的： Wordpress 是一个相对较重的 CMS 平台，所有的数据保存在 MySQL 数据库中，需要在一个支持 PHP 的环境中运行；Hexo 所有的数据都保存在文件中，通过预编译输出为静态文件，可以托管于任何一个静态文件服务器（基于 Nginx、Apache、IIS 等）中。",[14,68,69],{},"Hexo 像极了早期的 Blogger 自托管模式，每一次你修改了博客中的任何东西，他都需要重新编译所有的内容为新的静态文件，并且需要发布到静态文件服务器中。对于早年的我来说，博客配置、主题的修改和文章的发布、维护相当频繁，自然是无法忍受 Blogger 系统极慢的发布速度，所以后来一鼓作气搬到了 Wordpress。",[14,71,72],{},"但是时过境迁，作为一个连博文都懒得时常更新的人，更不会不停修改自己的博客主题了，并且当下各种本地或在线的工具链，都使得发布过程自动化成为了可能，即便是使用 Hexo 这种预编译的方案，也可以使博主只需要专注于博客内容本身。基于此，Wordpress 的优越性逐渐显得荡然无存，臃肿的系统占用着 VPS 资源，如果再配上一个大半年都不更新的博主，也是够浪费资源的。",[10,74,75],{"id":75},"需求",[14,77,78],{},"博客搬家毕竟是麻烦事，各种迁移和设置，所以在实施之前，认真思考了一下自己的需求，做了一个可行性调研。新的博客需要满足：",[80,81,82,86,89,92,95,98,101,104],"ul",{},[83,84,85],"li",{},"支持 HTTPS",[83,87,88],{},"可以绑定独立域名",[83,90,91],{},"旧的博客文章可以方便地迁移",[83,93,94],{},"尽可能方便的文章发布流程",[83,96,97],{},"博客系统高度自主可控",[83,99,100],{},"服务器稳定",[83,102,103],{},"墙内可访问",[83,105,106],{},"尽可能轻量级",[10,108,109],{"id":109},"方案",[14,111,112],{},"说干就干，通过将 Hexo 部署到 Github 上，使用 Github 的 Action 做 CI\u002FCD，完美实现了以上所有的需求。你现在看到的本博客正是基于上述方案搭建的。整个迁移过程经历了以下步骤，我将逐个讲解：",[114,115,117],"h4",{"id":116},"从旧博客导出-wordpress-文件","从旧博客导出 Wordpress 文件",[14,119,120,121,124,125,128],{},"其实这一步是比较简单的，在 Wordpress 后台界面，选择 ",[18,122,123],{},"工具 -> 导出","，将导出的 xml 文件保存在本地，命名为 ",[18,126,127],{},"wordpress.xml","。",[14,130,131],{},[132,133],"img",{"alt":134,"src":135},"","https:\u002F\u002Fcodex.wordpress.org\u002Fimages\u002Fthumb\u002F2\u002F20\u002Fmanageexport_zh-hans.png\u002F640px-manageexport_zh-hans.png",[114,137,139],{"id":138},"github-仓库创建博客项目初始化","Github 仓库创建、博客项目初始化",[14,141,142],{},"我们将创建两个 Github 仓库，一个用于放置博客程序本体，另一个用于发布博客静态页面，按如下要求配置：",[144,145,146,162],"table",{},[147,148,149],"thead",{},[150,151,152,156,159],"tr",{},[153,154,155],"th",{},"用途",[153,157,158],{},"仓库名",[153,160,161],{},"仓库类型",[163,164,165,177],"tbody",{},[150,166,167,171,174],{},[168,169,170],"td",{},"博客程序",[168,172,173],{},"blog",[168,175,176],{},"Private",[150,178,179,182,185],{},[168,180,181],{},"静态文件",[168,183,184],{},"weblog",[168,186,187],{},"Public",[114,189,191],{"id":190},"本地-hexo-系统初始化并导入-wordpress-数据","本地 Hexo 系统初始化，并导入 Wordpress 数据",[193,194,196],"h5",{"id":195},"安装-hexo","安装 Hexo",[14,198,199,200,205,206],{},"参考 ",[26,201,204],{"href":202,"rel":203},"https:\u002F\u002Fhexo.io\u002Fzh-cn\u002Fdocs\u002F",[30],"Hexo 官方手册","，先安装 ",[18,207,208],{},"hexo-cli",[210,211,216],"pre",{"className":212,"code":214,"language":215,"meta":134},[213],"language-shell","npm install -g hexo-cli\n","shell",[18,217,214],{"__ignoreMap":134},[14,219,220,221,223],{},"再使用 ",[18,222,208],{}," 在本地创建 hexo 工程",[210,225,228],{"className":226,"code":227,"language":215,"meta":134},[213],"cd ~\u002Fpath\u002Fto\u002Fworkspace # 此处使用 cd 命令定位到相应目录\nhexo init blog\ncd blog && npm install\n",[18,229,227],{"__ignoreMap":134},[193,231,233],{"id":232},"导入-wordpress-数据","导入 Wordpress 数据",[14,235,236,237,205,242,245],{},"参考官方",[26,238,241],{"href":239,"rel":240},"https:\u002F\u002Fhexo.io\u002Fzh-cn\u002Fdocs\u002Fmigration",[30],"迁移文档",[18,243,244],{},"hexo-migrator-wordpress"," 插件，然后导入之前步骤中下载的 wordpress 导出文件：",[210,247,250],{"className":248,"code":249,"language":215,"meta":134},[213],"npm install hexo-migrator-wordpress --save\nhexo migrate wordpress ~\u002Fpath\u002Fto\u002Fwordpress.xml # ~\u002Fpath\u002Fto\u002Fwordpress.xml 替换为真实路径\n",[18,251,249],{"__ignoreMap":134},[193,253,255],{"id":254},"同步到-github-仓库","同步到 Github 仓库",[14,257,258,259,261,262,265,266,128],{},"在上述创建的 ",[18,260,173],{}," 仓库，找到 ",[18,263,264],{},"Clone or Download"," 按钮，复制命令并在本地命令行工具中执行，形如： ",[18,267,268],{},"https:\u002F\u002Fgithub.com\u002Fyourname\u002Fblog.git",[14,270,271,272,277,278,281],{},"在本地 blog 目录，执行以下命令 (",[26,273,276],{"href":274,"rel":275},"https:\u002F\u002Fhelp.github.com\u002Fen\u002Fgithub\u002Fusing-git\u002Fadding-a-remote",[30],"参考",")，将 hexo 主程序代码上传到 Github 上的 ",[279,280,173],"strong",{}," 仓库：",[210,283,286],{"className":284,"code":285,"language":215,"meta":134},[213],"cd ~\u002Fpath\u002Fto\u002Fworkspace\u002Fblog\ngit init\ngit remote add origin https:\u002F\u002Fgithub.com\u002Fyourname\u002Fblog.git\ngit push -f\n",[18,287,285],{"__ignoreMap":134},[14,289,290,291,296,297,302],{},"此处操作，未接触过 git 的朋友，建议学习一下 ",[26,292,295],{"href":293,"rel":294},"https:\u002F\u002Fwww.ruanyifeng.com\u002Fblog\u002F2015\u002F12\u002Fgit-cheat-sheet.html",[30],"git 相关操作","后进行，或者使用 ",[26,298,301],{"href":299,"rel":300},"https:\u002F\u002Fwww.sourcetreeapp.com\u002F",[30],"SourceTree"," 等可视化工具。",[114,304,305],{"id":305},"主题设置",[14,307,308,309,314],{},"你可以使用默认主题，也可以使用主流第三方主题，比如 ",[26,310,313],{"href":311,"rel":312},"https:\u002F\u002Fgithub.com\u002Ftheme-next\u002Fhexo-theme-next",[30],"Next","，具体配置方法自行查看：",[14,316,317],{},[26,318,319],{"href":319,"rel":320},"https:\u002F\u002Ftheme-next.org\u002Fdocs\u002Fgetting-started\u002F",[30],[114,322,323],{"id":323},"构建和发布",[14,325,326],{},"无论我们是否需要自动化构建和发布，都需要在本地尝试发布静态博客，把这个步骤走通以后，再进行后续的高阶设置。",[193,328,330],{"id":329},"修改-hexo-设置","修改 Hexo 设置",[14,332,333,334,337,338,341,342,344],{},"在 ",[18,335,336],{},"_config.yml"," 文件中，搜索 ",[18,339,340],{},"deploy","，修改为 ",[18,343,184],{}," 对应的仓库：",[210,346,351],{"className":347,"code":349,"language":350,"meta":134},[348],"language-yaml","deploy:\n- type: git\n  repo: git@github.com:yourname\u002Fweblog.git # yourname 替换为你的 github 真实用户名\n  branch: gh-pages\n","yaml",[18,352,349],{"__ignoreMap":134},[193,354,355],{"id":355},"安装发布插件",[210,357,360],{"className":358,"code":359,"language":215,"meta":134},[213],"npm install hexo-deployer-git --save\n",[18,361,359],{"__ignoreMap":134},[193,363,364],{"id":364},"构建并发布",[210,366,369],{"className":367,"code":368,"language":215,"meta":134},[213],"hexo g -d\n",[18,370,368],{"__ignoreMap":134},[193,372,373],{"id":373},"查看发布状态",[14,375,376,377,380,381,385],{},"此时我们打开 github.com\u002Fyourname\u002Fweblog\u002Fsettings 的仓库配置页面，找到 ",[18,378,379],{},"GitHub Pages"," 这一栏，你会找到一个相应的访问地址，那就是你的博客地址了，形如（",[26,382,276],{"href":383,"rel":384},"https:\u002F\u002Fguides.github.com\u002Ffeatures\u002Fpages\u002F",[30],"）：",[14,387,388],{},[132,389],{"alt":134,"src":390},"https:\u002F\u002Fguides.github.com\u002Ffeatures\u002Fpages\u002Flaunch-theme-chooser.png",[14,392,393,394,397],{},"至此，你已经完成了最基本的迁移，如果需要绑定域名，只需要填下下面的 ",[18,395,396],{},"Custom Domain"," 信息即可。",[14,399,400,401,128],{},"需要注意的是，",[279,402,403],{},"当你绑定域名时需要做一些其他的关联操作，这个将在下面的专门章节中讲解",[114,405,406],{"id":406},"域名绑定",[14,408,409,410,415],{},"如果需要为 weblog 仓库上的页面添加独立域名，可以参考 ",[26,411,414],{"href":412,"rel":413},"https:\u002F\u002Fhelp.github.com\u002Fen\u002Fgithub\u002Fworking-with-github-pages\u002Fabout-custom-domains-and-github-pages",[30],"Github 官方手册","，主要关注以下链接：",[80,417,418,426],{},[83,419,420,425],{},[26,421,424],{"href":422,"rel":423},"https:\u002F\u002Fhelp.github.com\u002Fen\u002Fgithub\u002Fworking-with-github-pages\u002Fabout-custom-domains-and-github-pages#using-a-subdomain-for-your-github-pages-site",[30],"Using a subdomain for your GitHub Pages site"," (绑定子域名，比如： xx.yy.com)",[83,427,428,433],{},[26,429,432],{"href":430,"rel":431},"https:\u002F\u002Fhelp.github.com\u002Fen\u002Fgithub\u002Fworking-with-github-pages\u002Fabout-custom-domains-and-github-pages#using-an-apex-domain-for-your-github-pages-site",[30],"Using an apex domain for your GitHub Pages site"," （绑定顶级域名，比如： yy.com）",[14,435,436,437,440,441,444],{},"按照官网所说的步骤设置即可，Github 会自动往仓库里添加一个名为 ",[18,438,439],{},"CNAME"," 的文件，由于 Hexo 每次构建时，都会删除所有的静态文件，所以我们需要将此文件原样拷贝到本地 blog 程序下的 ",[18,442,443],{},"source"," 目录中。",[14,446,447,448,451],{},"当域名审核完毕后，就可以勾选 ",[18,449,450],{},"Enforce HTTPS"," 复选框。至此，整个博客的迁移工作也就已经基本完成了。",[114,453,454],{"id":454},"自动化构建和发布",[193,456,457],{"id":457},"添加秘钥对",[14,459,460],{},"打开命令行提示符，输入如下命令，创建秘钥对（如果是 Windows 电脑需要用 ssh-keygen 命令生成一组私钥和公钥）：",[210,462,465],{"className":463,"code":464,"language":215,"meta":134},[213],"ssh-keygen -f blog-deploy-key # 无脑回车\n",[18,466,464],{"__ignoreMap":134},[14,468,469],{},"产生如下文件：",[80,471,472,478],{},[83,473,474,477],{},[18,475,476],{},"blog-deploy-key"," (私钥)",[83,479,480,483],{},[18,481,482],{},"blog-deploy-key.pub"," (公钥)",[14,485,486],{},"在 Github 仓库中，做如下操作：",[80,488,489,511],{},[83,490,491,492,495,496,499,500,502,503,506,507,510],{},"在 weblog 仓库中，点击 ",[18,493,494],{},"settings -> Deploy keys -> Add deploy key","，填写 ",[18,497,498],{},"name"," 为 ",[18,501,482],{},", ",[18,504,505],{},"value"," 为",[279,508,509],{},"公钥"," (blog-deploy-key.pub) 中所有的文本内容（可以用 vscode、sublime 等文本编辑器打开）",[83,512,513,514,495,516,499,518,502,521,506,523,526],{},"在 blog 仓库中，点击 ",[18,515,494],{},[18,517,498],{},[18,519,520],{},"HEXO_DEPLOY_PRI",[18,522,505],{},[279,524,525],{},"私钥"," (blog-deploy-key) 中所有的文本内容",[193,528,530],{"id":529},"配置持续集成发布cicd","配置持续集成\u002F发布（CI\u002FCD）",[14,532,513,533,536],{},[18,534,535],{},"Actions -> New Workflow -> Set up a workflow yourself","，输入文件内容如下：",[210,538,541],{"className":539,"code":540,"language":350,"meta":134},[348],"# This is a basic workflow to help you get started with Actions\n\nname: Publish latest blog\n\n# Controls when the action will run. Triggers the workflow on push or pull request\n# events but only for the master branch\non:\n  push:\n    branches: [ master ]\n  pull_request:\n    branches: [ master ]\n\n# A workflow run is made up of one or more jobs that can run sequentially or in parallel\njobs:\n  build:\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        node-version: [16.*]\n\n    steps:\n      - uses: actions\u002Fcheckout@v1\n\n      - name: Use Node.js ${{ matrix.node-version }}\n        uses: actions\u002Fsetup-node@v1\n        with:\n          node-version: ${{ matrix.node-version }}\n\n      # 注意将 user.email 修改为你自己的\n      - name: Configuration environment\n        env:\n          HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}\n        run: |\n          mkdir -p ~\u002F.ssh\u002F\n          echo \"$HEXO_DEPLOY_PRI\" > ~\u002F.ssh\u002Fid_rsa\n          chmod 600 ~\u002F.ssh\u002Fid_rsa\n          ssh-keyscan github.com >> ~\u002F.ssh\u002Fknown_hosts\n          git config --global user.name \"Blog CI Action\"\n          git config --global user.email \"yourname@email.com\"\n\n      - name: Install dependencies\n        run: |\n          npm i -g hexo-cli\n          npm ci\n\n      - name: Publish hexo\n        run: |\n          npm run publish\n\n      # 这一步的主要目的是向百度站长 API 提交链接，如果有需要再取消注释\n      # - name: Submit to baidu\n      #   run: |\n      #     bash submit.sh\n",[18,542,540],{"__ignoreMap":134},[193,544,546,547],{"id":545},"完成-cicd-参考","完成 CICD ",[26,548,276],{"href":549,"rel":550},"https:\u002F\u002Fhdj.me\u002Fgithub-actions-hexo-cicd\u002F",[30],[14,552,553],{},"至此，你每一次同步 blog 仓库的代码到 github 都会触发 CICD actions，博客就会自动构建发布了。盗一张阮一峰的图给大家看看构建过程的 log 长啥样：",[14,555,556],{},[132,557],{"alt":134,"src":558},"https:\u002F\u002Fwww.wangbase.com\u002Fblogimg\u002Fasset\u002F201909\u002Fbg2019091108.jpg",[14,560,561,562,128],{},"更多高阶玩法可以查看阮一峰的 ",[26,563,566],{"href":564,"rel":565},"http:\u002F\u002Fwww.ruanyifeng.com\u002Fblog\u002F2019\u002F09\u002Fgetting-started-with-github-actions.html",[30],"Github Actions 入门教程",[114,568,570],{"id":569},"seo","SEO",[14,572,573],{},"先安装和配置以下这几个插件：",[80,575,576,583,590],{},[83,577,578],{},[26,579,582],{"href":580,"rel":581},"https:\u002F\u002Fgithub.com\u002Fhexojs\u002Fhexo-generator-sitemap",[30],"hexo-generator-sitemap",[83,584,585],{},[26,586,589],{"href":587,"rel":588},"https:\u002F\u002Fgithub.com\u002Fconeycode\u002Fhexo-generator-baidu-sitemap",[30],"hexo-generator-baidu-sitemap",[83,591,592],{},[26,593,596],{"href":594,"rel":595},"https:\u002F\u002Fgithub.com\u002Fhexojs\u002Fhexo-generator-feed",[30],"hexo-generator-feed",[14,598,599,600,603],{},"时间有限暂时先不详细的说这部分内容了，先放出 CI\u002FCD 这一步中缺失的 ",[18,601,602],{},"submit.sh"," 源码：",[210,605,608],{"className":606,"code":607,"language":215,"meta":134},[213],"#!\u002Fbin\u002Fbash\n\n# Generate sitemap.txt\nsed -n 's:.*\u003Cloc>\\(.*\\)\u003C\u002Floc>.*:\\1:p' .\u002Fpublic\u002Fbaidusitemap.xml > .\u002Fpublic\u002Fsitemap.txt\n\n# submit to baidu\n# 在 https:\u002F\u002Fziyuan.baidu.com\u002Flinksubmit 中获取相关 token\ncurl -H 'Content-Type:text\u002Fplain' --data-binary @public\u002Fsitemap.txt \"http:\u002F\u002Fdata.zz.baidu.com\u002Furls?site=https:\u002F\u002Fblog.interjc.net&token=your_baidu_token\"\n",[18,609,607],{"__ignoreMap":134},{"title":134,"searchDepth":611,"depth":611,"links":612},2,[613,615,616,617],{"id":12,"depth":614,"text":12},3,{"id":51,"depth":614,"text":51},{"id":75,"depth":614,"text":75},{"id":109,"depth":614,"text":109},[619],"it","2020-04-18T22:19:00+08:00","md",{"excerpt":623},{"type":7,"value":624},[625,627,631],[10,626,12],{"id":12},[14,628,16,629,21],{},[18,630,20],{},[14,632,24,633,32,636,38,639,44,642,48],{},[26,634,31],{"href":28,"rel":635},[30],[26,637,37],{"href":35,"rel":638},[30],[26,640,43],{"href":41,"rel":641},[30],[18,643,47],{},true,"\u002Fblog\u002Ffrom-wordpress-to-hexo",{"title":5,"description":134},"blog\u002Ffrom-wordpress-to-hexo","写在最前面的话 大概从 03 年开始接触博客到现在，已有十多年。早期换过无数的 BSP，比如 Blogbus、Live Space、百度空间、Blogger 等；也通过自建服务的方式，搭建过 Wordpress。由于这样那样可抗或不可抗的缘…",[650,651],"wordpress","hexo","s2-DtAFs41SICAslHbHDaBVyueSTSEjYuTlts1iu4Y8",1795177972806]