Fork me on GitHub

基于hexo+nginx快速建站

前言

跟公司大神学习技术知识,发现大神们都有自己的技术博客,拥有个人网站,感觉很酷。所以趁着一波阿里云打折,开启自己个人网站之路。准备着手从个人博客网站开始,无奈自己是一个小小后端程序猿,前端技术了解不多,难道要开始先积累前端技术栈再建站吗?orz。当然不,网上有大量博客框架和教程可以快速建站,比如Hexo,Wordpress等。本人博客 1.0版本就准备用hexo开始。
本文并非部署到github上,想要部署到github另行参考。

Hexo特点

不妨登录Hexo官网瞧一瞧。

  • Blazing Fast
  • Markdown Support
  • One-Command Deployment
  • Various Plugins

总之就是很快,支持markdown,简单到一键部署,拥有多样性插件。

快速搭建博客

前提条件

一台阿里云ECS,环境安装有:

Ubuntu安装Node.js

# 安装依赖包python-software-properties
apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install nodejs

Ubuntu安装Git

add-apt-repository ppa:git-core/ppa
apt update
apt install git

Ubuntu安装Nginx(略)

安装 Hexo

npm install -g hexo-cli

建站

创建博客需要文件

cd /data
midir hexo-blog
cd hexo-blog
hexo init
$ npm install

配置网站信息

修改 _config.yml 文件, 仅修改了站点信息和地址信息, 更多的配置信息参考官网

# Site
title: 陈瑞文的个人网站
subtitle:
description: 你好,旅行者
keywords:
author: 陈瑞文
language: zh-Hans
timezone:

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://www.chenruiwen.cn
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:

顺便下载了github上比较高排名的hexo主题:Next.

git clone https://github.com/iissnan/hexo-theme-next

将文件夹拷贝到Hexo博客目录的themes文件夹下,并修改站点主题

theme: next

剩下的个性化配置参考:
https://theme-next.iissnan.com/getting-started.html
进行配置。此外,参考了大佬们的文章:

通过Nginx发布

Nginx下载安装

略。

部署静态站点

生成静态文件:

➜  hexo-blog hexo g
➜  hexo-blog ls
_config.yml       node_modules      public            themes
db.json           package-lock.json scaffolds
debug.log         package.json      source

生成好的静态文件在public 文件夹内。

修改nginx配置文件

修改nginx配置文件:

vi /usr/local/nginx/conf/nginx.conf

仅需修改http模块的server配置:

server {
        listen       80;
        server_name  www.chenruiwen.cn;
        charset utf-8;

        root   /data/hexo-blog/public;# 这里是静态文件地址
        location / {
            index  index.html;
        }
}

重新加载nginx:

nginx -s reload

至此,网站已初步建成。

部署到github上

通过git部署
  • 创建GitHub Repository(略)
  • 修改配置文件_config.yml
    deploy:
    type: git
    repo: https://github.com/crrrrrw/hexo_static_page.git
    branch: master
    
  • 安装hexo git插件
    npm install hexo-deployer-git --save
    
  • 部署
    hexo generate
    hexo deploy
    
    这样,所有文件就都提交到github库上了。

服务器上,克隆静态文件

cd /data
git clone https://github.com/crrrrrw/hexo_static_page.git
git pull origin master

即可实现静态文件的更新。然后nginx重新指向这个静态文件目录即可。

自动化部署

思路a:
脚本定时更新github地址。

思路b:
基于git hooks实现push后的自动化部署。

总结

总之,搭建博客环境很简单,不过还有很多的工作要做,比如优化自己博客的样式,增加一些有意思的功能等,以后会慢慢优化的。当然,最重要的是写出好的有用的博文分享出去。

-------------本文结束,感谢您的阅读-------------
贵在坚持,如果您觉得本文还不错,不妨打赏一下~
0%