如何在 VPS 上面搭建 hexo 博客(小白教程)

2014-12-17 21:53:20 +08:00
 echopan
本人为大家讲解一下怎么在VPS搭建hexo博客,之前都是github搭建教程,很少有VPS这方面的,这方法是我折腾出来的hexo博客具有全静态化,响应速度快,轻量级等优点,是wp没法比的。

如下介绍:

什么是hexo

hexo是一个基于Node.js的静态博客程序,可以方便的生成静态网页托管在github和Heroku上。作者是来自台湾的@tommy351。引用@tommy351的话,hexo:

快速、简单且功能强大的 Node.js 博客框架。
A fast, simple & powerful blog framework, powered by Node.js.

类似于jekyll、Octopress、Wordpress,我们可以用hexo创建自己的博客,托管到github或Heroku上,绑定自己的域名,用markdown写文章。本博客即使用hexo创建并托管在github上。

1. 搭建hexo博客:

首先你得安装nodejs环境,以Ubuntu和CentOS为例,其他系统可以在https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager参考,如下:

Ubuntu安装方法:

curl -sL https://deb.nodesource.com/setup | sudo bash - 开始下载

sudo apt-get install -y nodejs 开始安装

CentOS安装方法:

curl -sL https://rpm.nodesource.com/setup | bash - 开始下载

yum install -y nodejs 开始安装

yum install gcc-c++ make # or: yum groupinstall 'Development Tools' 安装工具包,也可以不装

Git安装方法:

Ubuntu sudo apt-get install git-core ; Cent OS yum install git-core

2.安装hexo:

用npm命令安装hexo, 任意位置都可以

npm install -g hexo

安装完成后,再输入 hexo init <你的网站所在的目录>

cd <你的网站所在的目录>

安装依赖包 npm install

这样就安装完成了,不过没法在网站查看,先配置nginx的conf文件,我的是lnmpa环境,以我的环境为例,conf文件就是/usr/local/nginx/conf/vhost的虚拟机conf文件。文件配置如下:

server {
listen 80;
root /home/wwwroot/example.com/;
server_name hexo.freedom.moe;
access_log /home/wwwlogs/example.com.log;
error_log /home/wwwlogs/example.com.log;
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
root /home/wwwroot/example.com/public/;
access_log off;
expires 1d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
root /home/wwwroot/example.com/;
access_log off;
expires 10m;
}
location / {
root /home/wwwroot/example.com/;
if (-f $request_filename) {
rewrite ^/(.*)$ /$1 break;
}
}

按照上面去做。

接着在网站目录下修改文件_config.yml,注意在 :后面空一个格,不然出错,如下:



# Hexo Configuration

## Docs: http://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Hexo
subtitle:
description:
author:
email:
language: zh-CN

# URL
## If your site is put in a subdirectory, set url as 'http://example.com/child' and root as '/child/'
url: http://example.com/ 修改为你的网站
root: / 不用修改
permalink: :year/:month/:day/:title/
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
permalink_defaults:

# Directory
source_dir: source
public_dir: public

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
highlight:
enable: true
line_number: true
tab_replace:

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Archives
## 2: Enable pagination
## 1: Disable pagination
## 0: Fully Disable
archive: 2
category: 2
tag: 2

# Server
## Hexo uses Connect as a server
## You can customize the logger format as defined in
## http://www.senchalabs.org/connect/logger.html
port: 80 换成80端口
server_ip: 去掉
logger: false
logger_format: dev

# 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: MMM D YYYY
time_format: H:mm:ss

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Disqus
disqus_shortname:

# Extensions
## Plugins: https://github.com/hexojs/hexo/wiki/Plugins
## Themes: https://github.com/hexojs/hexo/wiki/Themes
theme: landscape
exclude_generator:

# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type:

修改完了之后保存,接着cd你网站所在的目录,重启nginx,接着输入 hexo g ,如有缓存文件的话先hexo clean再hexo g。

大功告成了。网站演示例子: http://hexo.freedom.moe/ 本人博客: http://blog.freedom.moe/
12036 次点击
所在节点    Node.js
16 条回复
Starduster
2014-12-17 22:15:44 +08:00
我看这头像就嘀咕似乎在 USTC LUG 群里见过看到最后一行顿时233了还真是你
kiritoalex
2014-12-17 23:17:34 +08:00
@Starduster Linux User Group还有群?
Showfom
2014-12-17 23:18:21 +08:00
完了看到你的文章我又想写个 Ubuntu 14.04 下安装 Hexo 的教程了 2333333
Starduster
2014-12-17 23:26:52 +08:00
@kiritoalex 有哇,334011318
Jays
2014-12-17 23:49:10 +08:00
怎么发布文章?
hjc4869
2014-12-17 23:58:28 +08:00
要不是文章太多,并且自己改出来一套还算喜欢的主题,早把Ghost换成Hexo了……
TrustyWolf
2014-12-18 00:09:24 +08:00
CentOS的EPEL源里是有nodejs的。
yum install epel-release
yum install nodejs
yum install npm
npm install -g hexo
yum install nginx
搞定
Twinkle
2014-12-18 00:29:15 +08:00
但是感觉这样的话发布文章不方便?
Starduster
2014-12-18 01:35:06 +08:00
@hjc4869
你用过了没,感觉如何?
PS,当年从ASP的zblog换出来的时候,官方插件商店已经瘫了,导出插件找不到,几十篇文章都是手动复制粘贴的……orz……
hjc4869
2014-12-18 02:05:26 +08:00
@Starduster Hexo我大概了解过,感觉Ghost的作用跟Hexo差不多,占的资源是它的几百倍。。
多的只是个方便的后台面板而已。。其它的跟静态博客没啥区别。
Lesilva
2014-12-18 06:12:22 +08:00
Ghost能在移动端写作,管理。
echopan
2014-12-18 08:31:36 +08:00
@Jays hexo n 发布文章
echopan
2014-12-18 08:32:10 +08:00
@Starduster 我也是见过你 23333
66beta
2014-12-18 09:30:27 +08:00
N年前,我也写过一篇 http://66beta.com/2014/05/06/how-to-setup-hexo/

但是为啥不放github呢?
Webb
2016-01-18 21:50:39 +08:00
mark
NyKO
2016-06-11 02:13:52 +08:00
@.@

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/154707

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX