使用云主机搭建一个简单的Hexo博客
为了分享记录一些知识,搭建了一个博客。涉及的东西非常的琐碎,以下记录了使用 Hexo 在云主机上搭建博客的各种细节。
背景
刚接触互联网时,正是博客刚兴起的时候。2005 年,虽然还没有 github,但已经有开源的博客系统。印象中,我用过 PJblog、LSB,基于 ASP 开发,使用 Access 数据库。往后,社交网络火爆,就很少再用博客。
读Phd时,任何成果都一定是以论文的形式来发表,现在不写论文,要分享积淀信息就必须依赖博客了。
搭建博客
为了节省资源,最方便的选择就是静态博客。对比几个博客的架构,Hexo在模块化,扩展性方面做得比较好,因此选择了 Hexo,下面就开始吧。
购买虚拟机
综合价格与方便考虑,选择在 DigitalOcean 购买了虚拟机。DigitalOcean 会自动分配 Floating IP,不用另外购买。注册域名可以选择 GoDaddy,完成后将注册好的域名的 A 记录指向分配的 Floating IP,同时配置好常用的 CName。此时,流量的入口搭建完毕。
主要配置有:
- gj365.me 的 A 记录配置为:128.199.183.222
- www 的 cname 配置为:@,默认指向A记录
安装 Hexo
Hexo官网有详细的教程[1],主要的步骤包括:
- 使用 npm 完成 Hexo 的安装
npm install -g hexo-cli - 使用 hexo 初始化博客,以 /var/www目录为例
cd /var/www hexo init blog cd blog npm install - 为了方便管理博客内容,可以选择安装 hexo-admin 插件
npm install --save hexo-admin
安装 nginx
nginx用于对外提供静态Web访问,最方便的方法是使用官方的版本[2]。以 Ubuntu 为例,主要步骤有:
- 添加 nginx deb 源的 Keyring
wget http://nginx.org/keys/nginx_signing.key sudo apt-key add nginx_signing.key - 编辑 /etc/apt/sources.list,添加以下内容,其中 codename 需要根据 Ubuntu 的发行版本修改,例如 16.04 为 xenial。
deb http://nginx.org/packages/debian/ codename nginx deb-src http://nginx.org/packages/debian/ codename nginx - 安装 nginx
apt-get update apt-get install nginx
其它版本请参考[2]中的说明。
配置 nginx
nginx 的配置分为两部分:一个 server 用于网站 Web 服务,另一个 Server 用于反向代理,将请求转发到到 hexo-admin。
编辑 /etc/nginx/sites-enabled/default 文件,内容如下
- Web服务:443 端口用于提供
- 反向代理:/admin 路径转发到本地 4000 端口
server {
listen 443 ssl default_server http2 fastopen=3 reuseport;
listen [::]:443 ssl default_server;
ssl_certificate /var/www/ssl/chained.pem;
ssl_certificate_key /var/www/ssl/domain.key;
root /var/www/blog/public;
index index.html index.htm index.nginx-debian.html;
server_name gj365.me;
location /admin {
proxy_pass http://127.0.0.1:4000;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
同时为将 HTTP 请求强制 rewrite 为 HTTPS 请求:
server {
server_tokens off;
listen 80;
access_log /dev/null;
rewrite ^/(.*)$ https://gj365.me/$1 permanent;
}
完成后通过 systemctl reload nginx 生效配置。关于证书的配置,请参考[3]中的内容。
使用 supervisor 启动 hexo
在 supervisor 中配置两个后台任务,一个为 hexo server,用于运行 hexo-admin,另一个为 watch 进程,自动生成最新的网页文件。
hexo server 新建 /etc/supervisor/conf.d/hexo-server.conf 文件,内容如下:
[program:hexo]
command=/usr/bin/hexo server -s --cwd /var/www/blog/
user=git
autostart=true
autorestart=true
stdout_logfile=/var/log/hexo.log
stderr_logfile=/var/log/hexo_err.log
hexo watch
新建 /etc/supervisor/conf.d/hexo-watch.conf 文件,内容如下:
[program:hexo-watch]
command=/usr/bin/hexo generate --watch --cwd /var/www/blog/
user=git
autostart=true
autorestart=true
stdout_logfile=/var/log/hexo.log
stderr_logfile=/var/log/hexo_err.log
supervisor 的 user 字段根据情况配置,需要保证此用户对 /var/www/blog/ 可以读写。
完成后,重启 supervisor:
systemctl restart supervisor
参考
[1] Hexo Installation. https://hexo.io/docs/index.html [2] nginx: Linux Pckages. http://nginx.org/en/linux_packages.html#distributions [3] Let’s Encrypt,免费好用的 HTTPS 证书. https://imququ.com/post/letsencrypt-certificate.html