介绍
Jekyll是一个简单的静态网站生成器. 它需要在Markdown、纺织、液体、HTML和CSS中输入页面,并输出完整的静态HTML页面。 Jekyll 与 GitHub 页面工作得很好,但有一些局限性 - 例如,用 插件工作并不特别容易。 因此,在自己的VPS上托管Jekyll网站可能是一个好主意。
在本指南中,我们将使用以下内容:
- Jekyll为写我们的内容 * nginx为服务我们的内容 * Capistrano为部署
安装要求
在您的VPS中:
地方性:
如果你还没有,安装 Ruby 和 RubyGems. 最好的方法是使用 Ruby Version Manager (RVM)。
1curl -L https://get.rvm.io | bash -s stable --ruby=2.0.0
按照其他提示,在您的系统上安装 Ruby. 一旦安装,您可以安装所需的宝石:
1gem install jekyll capistrano
用 Jekyll 创建博客
Jekyll的网站有一个 快速启动指南,通过创建一个简单的Jekyll网站并服务它,您将获得更多使用细节。
1jekyll new .
2cd myblog
3jekyll serve
你应该能够看到你的网站在 localhost:4000运行。
导航到myblog
目录,你应该看到几个文件夹. 我们关心的是_site
;其中包含由Jekyll生成的静态网站,我们将在下一步部署。
设置博客与Capistrano的部署
如果你的Jekyll网站仍在运行(运行jekyll serve
后),停止这个过程。
1capify .
这会创建必要的文件来部署 Capistrano。Capistrano是(https://github.com/capistrano/capistrano# assumptions)的软件
,假定您将通过SSH部署。当您运行命令时,它应该在config/deploy.rb
创建一个文件;打开它,并更新它看起来像这样:
1# replace this with your site's name
2set :application, "Blog"
3set :repository, '_site'
4set :scm, :none
5set :deploy_via, :copy
6set :copy_compression, :gzip
7set :use_sudo, false
8
9# the name of the user that should be used for deployments on your VPS
10set :user, "deployer"
11
12# the path to deploy to on your VPS
13set :deploy_to, "/home/#{user}/blog"
14
15# the ip address of your VPS
16role :web, "123.456.789.10"
17
18before 'deploy:update', 'deploy:update_jekyll'
19
20namespace :deploy do
21 [:start, :stop, :restart, :finalize_update].each do |t|
22 desc "#{t} task is a no-op with jekyll"
23 task t, :roles => :app do ; end
24 end
25
26 desc 'Run jekyll to update site before uploading'
27 task :update_jekyll do
28 # clear existing _site
29 # build site using jekyll
30 # remove Capistrano stuff from build
31 %x(rm -rf _site/* && jekyll build && rm _site/Capfile && rm -rf _site/config)
32 end
33end
接下来的步骤是设置 VPS 以 Capistrano 所要求的目录结构,您只需要做一次:
1cap deploy:setup
最后,部署您的VPS:
1cap deploy
Capistrano的部署任务将:
- 删除任何现有的静态 Jekyll 站点 2. 构建您的 Jekyll 站点 3. 清理包含在构建中不必要的文件(主要是 cap 文件) 4. 通过 SFTP 将静态站点的内容复制到您的 VPS,将其放入指定的目录
用 nginx 托管你的博客
回到您的 VPS,切换到 nginx 网站可用目录. 此处通常位于:
1cd /etc/nginx/sites-available
如果你在这个目录中运行ls
,你应该(至少)看到一个名为默认
的文件。
1sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
对于静态网站,您不需要太多的配置. 以下配置应该以最低限度工作:
1server {
2 # listen on http (port 80)
3 # remove the "default_server" if you are running multiple sites off the same VPS
4 listen 80 default_server;
5
6 # the IP address of your VPS
7 server_name 123.456.789.10;
8 # see http://nginx.org/en/docs/http/server_names.html for options
9 # to use your own domain, point a DNS A record at this IP address
10 # and set the server name to (eg.) "blog.example.com"
11
12 # the path you deployed to. this should match whatever was in your
13 # Capistrano deploy file, with "/current" appended to the end
14 # (Capistrano symlinks to this to your current site)
15 root /home/deployer/blog/current;
16 index index.html
17
18 # how long should static files be cached for, see http://nginx.org/en/docs/http/ngx_http_headers_module.html for options.
19 expires 1d;
20}
或者你可以使用 this gist来获取基本内容。无论如何,在这个目录中创建一个新的文件,使用你想要的 nginx 配置。一旦你创建它,你需要从网站启用目录中创建一个 symlink:
1sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
使用simlinks意味着通过删除simlink而不触摸原始配置文件,轻松地将网站脱机。
接下来,确保 nginx 能够读取它将提供的文件。 Nginx 需要能够读取和执行其托管目录中的所有内容,以及所有主目录。 为了做到这一点,我们将将网站目录的所有权交给 nginx 用户,即www-data
。
1sudo chown -R www-data:www-data /home/deployer/blog/current
2sudo chmod 755 -R /home
最后,您应该告诉 nginx 更新其配置,您可以通过:
1# tests the nginx configuration; if this is not successful you should fix any errors raised
2sudo nginx -t
3
4# safely restarts the nginx worker
5sudo kill -HUP `cat /var/run/nginx.pid`
或者,您可以简单地重新启动 nginx,但上述选项通常被认为是更安全的。
1sudo service nginx restart
测试和前进
导航到您的VPS的IP地址,您应该看到您的Jekyll网站的主页!
现在你可以开始编写内容并使用Jekyll定制你的网站. 每当你有新内容,你想推动在线,它就像做‘封面部署’一样容易。