博客搭建-wordpress&hexo

【写在前面】

今天写写博客(个人网站)的搭建。从wordpress和hexo+github 两种方式进行搭建,重点讲解第二种,wordpress比较简单,先用10分钟介绍一下就ok啦。

先讨论一些哲学问题,我们为什么要搭博客?
1.程序员必备技能,通过搭博客(即使未成功),你可以接触很多新的东西,比如nodejs(指不定就由此走进前端的大门了呢略略略),git。
2.推荐刘未鹏博客:书写是为了更好的思考。
3.推荐《软技能:代码之外的生存之道》:博客可以塑造个人品牌。

过了这两个小时你会收获什么?
1.运气好的话,你可能马上就拥有自己的博客了。
运气差的话,你可能要迟几天拥有博客了。
2.不管你运气咋样,你至少可以了解到建站的一些基本知识,了解到一些可能之后会经常用的软件或名词概念,知道搭建博客的基本步骤。

简单介绍一下两者的区别

wordpress:php和mysql开发的博客平台,十分简单且强大。目前很多网站都是wordpress搭建的。上线需要自己的域名和服务器。
hexo :简洁优雅。使用 Node.js 作为构建引擎,拥有着丰富的插件库,开源;它支持 Markdown ,方便了博客的撰写。并且,对服务器的要求非常低,低到可以使用github(国外)或者Coding.net(国内)提供的托管服务,我们可以利用hexo+github实现我们的博客网站。

一个网站需要什么?

简单来说,需要三样东西;
域名+主机+网站程序(wordpress/hexo)。

wordpress

  1. 相关的配置文件 (wordpress + wamp)
  2. 配置数据库
  3. 配置wp-config-php
  4. 后续的wordpress后台提供的基本功能与主题插件

搞定了√
想不到吧。

hexo+github

首先需要下面两样东西

  • git :https://git-scm.com/
  • node.js : https://nodejs.org/en/
  • 【nodejs专场】
    JavaScript是一门脚本语言,它需要一个运行环境。就好像PHP需要Apache,JSP需要Tomcat等等…… 而NodeJS之前,JavaScript运行环境是浏览器,也就是JavaScript在网页中才能跑起来。NodeJS之后JavaScript又多了一个运行环境,就是NodeJS。

注册github (用户名 用小写字母。)

创建Repository(仓库名 你的用户名.github.io)

配置SSH keys相关

  1. 查看本机的SSH keys

    1
    $ cd ~/. ssh
  2. 生成SSH keys

    1
    $ ssh-keygen -t rsa -C "注册用的邮件地址@youremail.com"

补充:
1)大写的C
2)会需要输入密码 即deploy项目的时候输入的密码 直接回车表示无密码。
3)出现的页面会有保存id_rsa.pub的地址。通过这个找到id_rsa.pub的文件(记得勾选文件拓展名+隐藏的项目),找到之后txt打开全选复制

  1. 添加SSH keys 到你的github
    /setting/ssh
    title+keys
  2. 设置 连接github

    1
    2
    3
    4
    $ ssh -T git@github.com
    The authenticity of host 'GitHub.com (207.97.227.239)' can't be established.
    RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    Are you sure you want to continue connecting (yes/no)
  3. 完善用户信息

    1
    2
    $ git config --global user.name "taleyoung"//输入注册时的用户名
    $ git config --global user.email "1090117853@qq.com"//填写注册邮箱

SSH key 搞定√

搭建hexo

npm install -g hexo-cli

创建博客的本地文件夹(username.github.io)
生成博客初始配置文件

$ hexo init

安装依赖包

$ npm install

确保git部署

$ npm install hexo-deployer-git --save

去见他的第一面

$ hexo g (genarate)
$ hexo s        (server)

配置博客文件

# Hexo Configuration
## Docs: http://zespia.tw/hexo/docs/configure.html
## Source: https://github.com/tommy351/hexo/

# Site 这里的配置,哪项配置反映在哪里,可以参考我的博客
title: Taleyoung's blog #博客名
subtitle: to be continued... #副标题
description: My blog #给搜索引擎看的,对网站的描述,可以自定义
author: taleyoung #作者,在博客底部可以看到
email: 1090117853@qq.com #你的联系邮箱
language: zh-CN #中文。如果不填则默认英文

# URL #这项暂不配置,绑定域名后,欲创建sitemap.xml需要配置该项
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.com
root: /
permalink: :year/:month/:day/:title/
tag_dir: tags
archive_dir: archives
category_dir: categories

# Writing 文章布局、写作格式的定义,不修改
new_post_name: :title.md # File name of new posts
default_layout: post
auto_spacing: false # Add spaces between asian characters and western characters
titlecase: false # Transform title into titlecase
max_open_file: 100
filename_case: 0
highlight:
  enable: true
  backtick_code_block: true
  line_number: true
  tab_replace:

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Archives 默认值为2,这里都修改为1,相应页面就只会列出标题,而非全文
## 2: Enable pagination
## 1: Disable pagination
## 0: Fully Disable
archive: 1
category: 1
tag: 1

# Server 不修改
## Hexo uses Connect as a server
## You can customize the logger format as defined in
## http://www.senchalabs.org/connect/logger.html
port: 4000
logger: false
logger_format:

# Date / Time format 日期格式,可以修改成自己喜欢的格式
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-M-D
time_format: H:mm:ss

# Pagination 每页显示文章数,可以自定义,贴主设置的是10
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Disqus Disqus插件,我们会替换成“多说”,不修改
disqus_shortname:

# Extensions 这里配置站点所用主题和插件,暂时默认
## Plugins: https://github.com/tommy351/hexo/wiki/Plugins
## Themes: https://github.com/tommy351/hexo/wiki/Themes
theme: landscape
exclude_generator:
plugins:
- hexo-generator-feed
- hexo-generator-sitemap

# Deployment 站点部署到github要配置
## Docs: http://zespia.tw/hexo/docs/deploy.html
deploy:
  type: git
  repository: 
  branch: master

第一次本地服务器跑一下

$ hexo g #生成
$ hexo s #启动本地服务,进行文章预览调试,退出服务用Ctrl+c

最最最后一步:部署到github
在github主页上找到刚刚为博客建立的Repository
复制ssh码
配置博客文件的deploy

deploy:
  type: git
  repository: git@github.com:taleyoung/taleyoung.github.io.git
  branch: master

部署上传

$ hexo g
$ hexo d

发布文章

$ hexo new "我的第一篇blog"
$ hexo clean
$ hexo generate
$ hexo server
$hexo deploy

new之后会生成文章的最基本信息
可用txt/notepad等打开 需要使用md语法