如何在 VPS 上面搭建 hexo 博客(小白教程) - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
echopan
V2EX    Node.js

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

  •  1
     
  •   echopan 2014 年 12 月 17 日 12793 次点击
    这是一个创建于 4079 天前的主题,其中的信息可能已经有所发展或是发生改变。
    本人为大家讲解一下怎么在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
    archiv_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/
    16 条回复    2016-06-11 02:13:52 +08:00
    Starduster
        1
    Starduster  
       2014 年 12 月 17 日
    我看这头像就嘀咕似乎在 USTC LUG 群里见过看到最后一行顿时233了还真是你
    kiritoalex
        2
    kiritoalex  
       2014 年 12 月 17 日 via Android
    @Starduster Linux User Group还有群?
    Showfom
        3
    Showfom  
    PRO
       2014 年 12 月 17 日
    完了看到你的文章我又想写个 Ubuntu 14.04 下安装 Hexo 的教程了 2333333
    Starduster
        4
    Starduster  
       2014 年 12 月 17 日
    @kiritoalex 有哇,334011318
    Jays
        5
    Jays  
       2014 年 12 月 17 日 via Android
    怎么发布文章?
    hjc4869
        6
    hjc4869  
       2014 年 12 月 17 日
    要不是文章太多,并且自己改出来一套还算喜欢的主题,早把Ghost换成Hexo了……
    TrustyWolf
        7
    TrustyWolf  
       2014 年 12 月 18 日 via iPhone
    CentOS的EPEL源里是有nodejs的。
    yum install epel-release
    yum install nodejs
    yum install npm
    npm install -g hexo
    yum install nginx
    搞定
    Twinkle
        8
    Twinkle  
       2014 年 12 月 18 日
    但是感觉这样的话发布文章不方便?
    Starduster
        9
    Starduster  
       2014 年 12 月 18 日 via iPhone
    @hjc4869
    你用过了没,感觉如何?
    PS,当年从ASP的zblog换出来的时候,官方插件商店已经瘫了,导出插件找不到,几十篇文章都是手动复制粘贴的……orz……
    hjc4869
        10
    hjc4869  
       2014 年 12 月 18 日
    @Starduster Hexo我大概了解过,感觉Ghost的作用跟Hexo差不多,占的资源是它的几百倍。。
    多的只是个方便的后台面板而已。。其它的跟静态博客没啥区别。
    Lesilva
        11
    Lesilva  
       2014 年 12 月 18 日
    Ghost能在移动端写作,管理。
    echopan
        12
    echopan  
    OP
       2014 年 12 月 18 日
    @Jays hexo n 发布文章
    echopan
        13
    echopan  
    OP
       2014 年 12 月 18 日
    @Starduster 我也是见过你 23333
    66beta
        14
    66beta  
       2014 年 12 月 18 日
    N年前,我也写过一篇 http://66beta.com/2014/05/06/how-to-setup-hexo/

    但是为啥不放github呢?
    Webb
        15
    Webb  
       2016 年 1 月 18 日
    mark
    NyKO
        16
    NyKO  
       2016 年 6 月 11 日
    @.@
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1190 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 16:46 PVG 00:46 LAX 08:46 JFK 11:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86