Hosting your Hugo site on Netlify

When I built this website, I checked for a free static hosting solution that was easy to use and maintain. After a little research, I landed on Netlify. I found hosting a Hugo site on Netlify to be an effective solution for deploying static websites. Combining Hugo’s speed with Netlify’s robust features, such as continuous deployment, a user-friendly dashboard, and advanced build settings, made everything feel seamless.

Why Use Netlify?

Netlify is popular for its ease of use, free hosting tier, and powerful features like automated builds, custom domain support, and built-in SSL. With Hugo’s efficient static site generation, deploying on Netlify enables fast, seamless updates and management.

Step 1: Set up netlify.toml configuration

The netlify.toml file is essential for configuring build settings in Netlify. Here’s a setup tailored for various deployment environments, including production, deploy previews, and branch deploys.

Add the following netlify.toml file to your Hugo project’s root directory:

[build]
publish = "public"
command = "hugo --gc --minify"

[context.production.environment]
HUGO_VERSION = "0.134.0"
HUGO_ENV = "production"
HUGO_ENABLEGITINFO = "false"

[context.split1]
command = "hugo --gc --minify --enableGitInfo"

[context.split1.environment]
HUGO_VERSION = "0.134.0"
HUGO_ENV = "production"

[context.deploy-preview]
command = "hugo --gc --minify --buildFuture -b $DEPLOY_PRIME_URL"

[context.deploy-preview.environment]
HUGO_VERSION = "0.134.0"

[context.branch-deploy]
command = "hugo -b $DEPLOY_PRIME_URL"

[context.branch-deploy.environment]
HUGO_VERSION = "0.134.0"

[context.next.environment]
HUGO_ENABLEGITINFO = "false"

Explanation of key parameters

  • Build Command: hugo --gc --minify triggers garbage collection (--gc) and minifies output.
  • HUGO_VERSION: Specifies the Hugo version to avoid compatibility issues.
  • HUGO_ENABLEGITINFO: Used for tracking commit metadata in production.
  • Deployment Contexts:
    • Production: Builds optimized, production-ready files.
    • Deploy Preview: Builds the site using the pull request URL, helpful for staging.
    • Branch Deploy: Enables previews for branches other than the main one.

Step 2: Connect your repository to netlify

  1. Log in to Netlify: Go to Netlify.

  2. Create a new site: Choose “New site from Git” from the dashboard.

  3. Select your repository: Choose GitHub, GitLab, or Bitbucket, authenticate, and select your Hugo site repository.

  4. Configure build settings:

    • Build command: Leave as hugo.
    • Publish directory: Set as public (Netlify automatically reads from netlify.toml if it’s present).
  5. Deploy your site: Click “Deploy site” to initiate the first build. Netlify will pull from your repository, build, and deploy the site, which you can access via the provided default Netlify URL (e.g., https://example-site.netlify.app).

Step 3: Preview and customize your site

Once the build completes, visit the live preview URL to verify that your site is working correctly.

Step 4: Set Up a custom domain (Optional)

To configure a custom domain:

  1. Go to the Netlify dashboard, select “Domain settings,” and add your domain.
  2. Follow the provided DNS setup instructions. If you want Netlify to manage DNS, you can transfer the domain or add a Netlify DNS record.
  3. Netlify automatically provisions an SSL certificate through Let’s Encrypt, adding secure HTTPS to your site.

Step 5: Enable continuous deployment

Netlify supports continuous deployment by default. Each push to your repository triggers a new build and redeploys the latest changes. This is the feature I love the most, and it makes easy to manage updates or collaborate with others on site changes.

Conclusion

Deploying a Hugo site on Netlify offers a streamlined experience, leveraging Hugo’s fast builds with Netlify’s advanced hosting features. With a few simple steps, your site is live, secure, and ready for easy content updates directly from Git.

I hope this setup is ideal for efficient deployment with customized environments, making it easy to manage development and production versions and maintain a polished, dynamic website.

Further readings

Read More