Configuring Hugo to Serve from Multiple Domains and IPs with Lighttpd

Hosting a Hugo site across multiple domains and IPs can be achieved effortlessly by combining Hugo’s flexibility with Lighttpd’s robust domain routing. Here’s a step-by-step guide to set this up.


Step 1: Configure Hugo

  1. Set Up Your Hugo Site:

    • If you haven’t already, create a new Hugo site:
      hugo new site mysite
      
  2. Add Content and Themes:

    • Populate your site with content and customize it with your desired themes.
  3. Set the Base URL:

    • In your config.toml (or config.yaml/config.json), set the baseURL for your primary domain:
      baseURL = "http://example.com/"
      

Step 2: Generate Static Files for Each Domain

Hugo allows you to override the baseURL when generating site files. This enables the same Hugo project to serve multiple domains.

  1. Generate Static Files for Each Domain:
    • Use the --baseURL flag and specify output directories for each domain:
      hugo --baseURL="http://example.com" -d public/example.com
      hugo --baseURL="http://example.org" -d public/example.org
      

Step 3: Set Up Lighttpd to Serve Multiple Domains

Lighttpd is a lightweight web server that can easily handle serving multiple domains.

  1. Install Lighttpd:

    • On your server, install Lighttpd with:
      sudo apt-get install lighttpd
      
  2. Create Configuration Files for Each Domain:

    • For each domain, create a separate configuration file in /etc/lighttpd/conf-available/. Here are examples for example.com and example.org:

      # /etc/lighttpd/conf-available/example.com.conf
      $HTTP["host"] == "example.com" {
          server.document-root = "/path/to/your/hugo/site/public/example.com"
          server.error-handler-404 = "/404.html"
          index-file.names = ( "index.html" )
      }
      
      # /etc/lighttpd/conf-available/example.org.conf
      $HTTP["host"] == "example.org" {
          server.document-root = "/path/to/your/hugo/site/public/example.org"
          server.error-handler-404 = "/404.html"
          index-file.names = ( "index.html" )
      }
      
  3. Enable the Configuration Files:

    • Use symbolic links to enable each domain’s configuration in /etc/lighttpd/conf-enabled/:
      sudo ln -s /etc/lighttpd/conf-available/example.com.conf /etc/lighttpd/conf-enabled/example.com.conf
      sudo ln -s /etc/lighttpd/conf-available/example.org.conf /etc/lighttpd/conf-enabled/example.org.conf
      
  4. Restart Lighttpd:

    • Apply the changes by restarting the Lighttpd service:
      sudo systemctl restart lighttpd
      

Testing Your Configuration

  1. Point your domains (example.com and example.org) to your server’s IP address using DNS configuration.
  2. Visit the domains in your browser to verify:
    • Both domains should correctly serve their respective static files.
    • Links and navigation should work seamlessly.

Conclusion

With Hugo and Lighttpd, you can easily serve your static site across multiple domains and IPs. By leveraging Hugo’s baseURL overrides and Lighttpd’s domain-specific configurations, you can maintain a single project for all your domains.

If you encounter any issues or need further guidance, don’t hesitate to reach out!


Related Posts