搬瓦工部署 Jekyll 静态网站教程
Jekyll 是最早流行的静态网站生成器之一,使用 Ruby 语言编写,也是 GitHub Pages 的默认引擎。它以简洁灵活著称,支持 Markdown 和 Liquid 模板语法,非常适合搭建博客和文档站点。本文将介绍如何在搬瓦工 VPS 上安装 Ruby 环境、搭建 Jekyll 站点并通过 Nginx 部署。购买搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。
一、安装 Ruby 环境
1.1 安装依赖
apt update && apt upgrade -y
apt install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libffi-dev curl git -y
1.2 使用 rbenv 安装 Ruby
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
rbenv install 3.3.0
rbenv global 3.3.0
ruby -v
二、安装 Jekyll
gem install jekyll bundler
jekyll -v
三、创建 Jekyll 站点
cd /opt
jekyll new mysite
cd mysite
3.1 配置站点
编辑 _config.yml 文件:
cat > /opt/mysite/_config.yml << 'EOF'
title: 我的 Jekyll 博客
description: 使用 Jekyll 搭建的个人博客
baseurl: ""
url: "https://yourdomain.com"
lang: zh-CN
theme: minima
permalink: /:year/:month/:day/:title/
markdown: kramdown
highlighter: rouge
plugins:
- jekyll-feed
- jekyll-seo-tag
- jekyll-sitemap
defaults:
- scope:
path: ""
type: "posts"
values:
layout: "post"
author: "Your Name"
EOF
3.2 创建文章
Jekyll 文章存放在 _posts/ 目录中,文件名格式为 YYYY-MM-DD-title.md:
cat > /opt/mysite/_posts/2026-03-28-hello-world.md << 'EOF'
---
layout: post
title: "Hello World"
date: 2026-03-28
categories: [技术]
tags: [Jekyll, 教程]
---
这是我的第一篇 Jekyll 博客文章。Jekyll 使用 Markdown 语法编写内容,支持代码高亮和数学公式。
## 代码示例
Jekyll 支持丰富的代码高亮功能,方便技术博客展示代码。
EOF
四、构建与部署
4.1 本地预览
cd /opt/mysite
bundle exec jekyll serve --host 0.0.0.0
4.2 生成静态文件
JEKYLL_ENV=production bundle exec jekyll build
构建后的文件在 _site/ 目录中。
4.3 Nginx 部署
apt install nginx -y
cat > /etc/nginx/sites-available/jekyll.conf << 'EOF'
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /opt/mysite/_site;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
error_page 404 /404.html;
}
EOF
ln -sf /etc/nginx/sites-available/jekyll.conf /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl restart nginx
五、自动化部署脚本
cat > /opt/mysite/deploy.sh << 'EOF'
#!/bin/bash
cd /opt/mysite
git pull origin main
JEKYLL_ENV=production bundle exec jekyll build
systemctl reload nginx
echo "[$(date)] Jekyll site deployed"
EOF
chmod +x /opt/mysite/deploy.sh
六、安装插件
在 Gemfile 中添加插件后运行:
bundle install
bundle exec jekyll build
七、SSL 证书
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com
八、常见问题
Bundler 版本冲突
如果遇到 Bundler 版本错误,更新 Bundler 并重新安装依赖:
gem update bundler
bundle install
构建速度慢
Jekyll 对大量文章的构建速度不如 Hugo。可以使用增量构建加速:jekyll build --incremental。
总结
Jekyll 是一款成熟稳定的静态网站生成器,与 GitHub Pages 完美集成。在搬瓦工 VPS 上部署可以获得更多自定义能力。其他静态站生成器教程包括 Hugo、Hexo、MkDocs 等。购买搬瓦工 VPS 请访问 bwh81.net,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。