# Hosting a Jekyll Website on S3 with SSL

Source: https://www.ironin.it/blog/hosting-a-jekyll-website-on-s3-with-ssl.html

Published 8 February 2019

[web](https://www.ironin.it/blog/tags/web) [devops](https://www.ironin.it/blog/tags/devops) [infrastructure](https://www.ironin.it/blog/tags/infrastructure)

![Hosting a Jekyll Website on S3 with SSL](https://www.ironin.it/api/media/file/ironin-seed-ZmE2MDZiNmItNDlkOS00NjMxLThjNzAtY2Q4OWNhOWMwOGNl_hosting-a-jekyll-website-on-s3-with-ssl.jpg)

When you have Jekyll-built static websites and want to easily host them, you can do it on Amazon S3. We outline the exact process + all the code necessary.

[Jekyll](https://jekyllrb.com/) is an excellent tool for creating static sites (from plaintext, markdown, and other basic styles) that can be easily hosted on [Amazon S3](https://aws.amazon.com/s3/), [GitHub pages](https://pages.github.com/), [Heroku](https://www.ironin.it/blog/deploying-jekyll-website-to-heroku.html) or any other provider that can serve HTML files.

In this article we will show you how to deploy an existing Jekyll-based website to S3 with CDN for improving the site’s performance, using [CloudFront](https://aws.amazon.com/cloudfront/).

## CloudFront for serving content over HTTPS

Serving static websites directly from S3 or GitHub pages works fine as long as you don't need to serve it over HTTPS - neither allows custom SSL certificates for HTTPS. As far as we know, putting a load balancer, reverse-proxy or CDN, such as `CloudFront`, in front is the only solution (but if you know more do not hesitate to mention them in the comments!).

So with that, let’s dive into our solution. We will start by creating a new `Content Distribution` on CloudFront. Visit the [AWS CloudFront Management Console](https://console.aws.amazon.com/cloudfront), click `Create Distribution`, and select `Web` as the `Delivery Method`. Fill the `Origin Settings` section as outlined in this screenshot:

![Blog post image](https://www.ironin.it/api/media/file/ironin-seed-YmViZDY3YmYtNmNiOC00NDJjLThmZTQtNDM0Zjc4NzFlZmI0_image1.png)

In the field where we put `your_domain_or_bucket` type the S3 endpoint that can be found in the `Hosting Website` section (ie: `your-bucket.s3-website.eu-central-1.amazonaws.com`). This allows you to respect the S3 redirect rules when accessing the website through CloudFront.

Other sections can be left with default settings for now.

In the `Distribution Settings` section specify the SSL certificate you want to use:

![Blog post image](https://www.ironin.it/api/media/file/ironin-seed-MjdhNjVkMjgtMzUxOS00YjE5LWFkM2UtMGMzNmJlNDc4MzQz_image2.png)

If you don’t have a certificate added yet, then go to the `Certificate Manager` service on AWS and upload one there. It’s better to do it this way than import it directly in the Distribution Settings, because it makes managing the certificate easier (uploading updated versions, etc.).

## Versioning static files

Before we start using CDN for serving content, we also need to make sure we have correct caching headers - so that unmodified files can be loaded from the browser's cache. Despite this, we will need to be able to deliver new versions whenever they are updated (even when the cache headers are still valid). To do this, we will fingerprint file names - so the browser won't find the new file in its cache, triggering a request to fetch it from our CDN.

For managing assets we will use the [jekyll-assets](https://github.com/jekyll/jekyll-assets) gem. Add it to your `Gemfile`, run `bundle install` and update the `_config.yml` as below:

```yml
gems:
  - jekyll-assets
assets:
  autoprefixer:
    browsers:
      - "last 2 versions"
      - "IE > 9"
  prefix: '/assets'
  assets:
    - "main.css"
    - "*.js"
    - "*.png"
    - "*.jpg"
    - "*.eot"
    - "*.ttf"
    - "*.woff"
  sources:
    - _assets
```

### Migrating stylesheets

We also need to move all the `stylesheet` files from the `_sass` directory to `assets/css` and then update `main.scss` to be our entry point for importing other files:

```css
@import "variables";
@import "global";
```

In the above example we assume you have the following structure in your `_assets/css` directory:

- _global.scss
- _variables.scss
- main.scss

Remember to update your path to the assets in your templates and stylesheets:

- Replace `<img src="path/to/image.jpg" alt="some alt" />` with `<img src="path/to/image.jpg" alt:'some alt' />`
- Replace `<script src="path/to/script.js" />` with `{% js 'path/to/script' %}`
- Replace `<link rel="styleshee" href="path/to/stylesheet.css" />` with `%{ css main.css %}`
- If you reference any files (fonts, images) in your stylesheets replace `url('path/to/file')` with `url(asset_path('path/to/file'))`

For more info visit the [jekyll-assets](https://github.com/jekyll/jekyll-assets#tags) page.

### Migrating javascripts

We also need to move our scripts to `assets/scripts` folder and create a `main.js` file to reference our other files:

```javascript
//= require jquery
//= require bootstrap
```

And include that main.js file in the template:

```ruby
{% raw  %}{% js main.js %}{% endraw %}
```

Now when you build your website with the `JEKYLL_ENV` set to `production` the gem will concatenate, compress, and fingerprint your asset files automatically.

## Deploying to S3

In order to deploy our static website to s3 we use the [s3_website](https://github.com/laurilehmijoki/s3_website) gem. It allows us not only to push the files to the bucket, but also to create invalidation in our distribution for our \*.html files. HTMLs can't be fingerprinted because we don't want to change the links every time we deploy a new version.

Add the `s3_website` gem to your `Bundle` file and run the `bundle install` command. You’ll also need to have Java installed because the gem is using Scala under the hood.

Now create a basic config file in the project root:

```yml
# s3_website.yml
s3_id: AWS_ACCESS_KEY
s3_secret: AWS_SECRET_KEY
s3_bucket: your-bucket-name

index_document: index.html
error_document: error.html

max_age: 3600

gzip: true

s3_endpoint: eu-central-1

cache_control:
  "assets/*": public, max-age=3600
  "*": no-cache, no-store, max-age=0 must-revalidate

cloudfront_distribution_id: YOUR_CLOUDFRONT_DISTRIBUTION_ID

cloudfront_distribution_config:
  default_cache_behavior:
    min_ttl: <%= 60 * 60 * 24 %>
  aliases:
    quantity: 1
    items:
      - www.your-website.com

cloudfront_wildcard_invalidation: false
```

We disable caching (`no-cache, no-store, max-age=0 must-revalidate`) for all files except assets (those will be HTML files anyway unless you use a custom setup) .

Let's apply those settings to our `CloudFront` distribution to check if everything is configured correctly:

```bash
bundle exec s3_website cfg apply
```

If you get something similar to the message below, it means the website is ready to be deployed:

```ruby
Applying the configurations in s3_website.yml on the AWS services ...
Bucket your-bucket now functions as a website
No redirects to configure for your-bucket bucket
Bucket your-bucket is now readable to the whole world
Detected an existing CloudFront distribution (id 123456789) ...
  Applied custom distribution settings:
    :caller_reference: '123456789'
    ...
```

To deploy the website to S3, simply use the push command:

```bash
bundle exec s3_website push
```

If you would like to check which files are going to be sent first, then you can pass the `--dry-run` flag to the above command.

This will create invalidation on CDN. To check its status, go to `CloudFront`, select your distribution and visit the `Invalidations` tab. Usually it takes a few minutes before the HTML file is invalidated in CDN and a new version from your S3 bucket is fetched.

For more deployment configurations, organizing your website infrastructure, or website development in general, you can ask the experts at iRonin for assistance. Our team of highly skilled specialists have deep experience and are available to take a closer look at all your web development needs.

Share:

Author: iRonin IT Team

Need software like this built?

[Software development →](https://www.ironin.it/ai-software-engineering)

Our code is merged into 10 open-source AI-agent projects, two of them with tens of thousands of GitHub stars.

[Talk to us →](https://www.ironin.it/contact-us.html?offer=ai-software-engineering)

## Read similar articles

- [Ruby 3.1 – what is new?](https://www.ironin.it/blog/ruby-3-1-changes.html)
- [How to handle connection with tens of API providers in one web application](https://www.ironin.it/blog/how-to-handle-connection-with-tens-of-api-providers-in-one-web-application.html)
- [Digital Transformation in Fintech industry - Benefits and Challenges of Innovation](https://www.ironin.it/blog/digital-transformation-in-fintech.html)
- [How to deal with Big Data challenges for financial application performance](https://www.ironin.it/blog/how-to-deal-with-big-data-challenges-for-financial-app-performance.html)

## Read next

- [Which AI model is best for your own task? Our own test and a public leaderboard disagreed](https://www.ironin.it/blog/own-ai-model-evals.html)
- [What uses up a Claude plan's usage limit? What each kind of token costs against it](https://www.ironin.it/blog/claude-usage-limit-token-costs.html)
- [Our take on the 18th RRUG meeting - what we learned](https://www.ironin.it/blog/our-take-on-the-18th-rrug-meeting-what-we-learned.html)
