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
Set Up Your Hugo Site:
- If you haven’t already, create a new Hugo site:
hugo new site mysite
- If you haven’t already, create a new Hugo site:
Add Content and Themes:
- Populate your site with content and customize it with your desired themes.
Set the Base URL:
- In your
config.toml
(orconfig.yaml
/config.json
), set thebaseURL
for your primary domain:baseURL = "http://example.com/"
- In your
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.
- 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
- Use the
Step 3: Set Up Lighttpd to Serve Multiple Domains
Lighttpd is a lightweight web server that can easily handle serving multiple domains.
Install Lighttpd:
- On your server, install Lighttpd with:
sudo apt-get install lighttpd
- On your server, install Lighttpd with:
Create Configuration Files for Each Domain:
For each domain, create a separate configuration file in
/etc/lighttpd/conf-available/
. Here are examples forexample.com
andexample.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" ) }
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
- Use symbolic links to enable each domain’s configuration in
Restart Lighttpd:
- Apply the changes by restarting the Lighttpd service:
sudo systemctl restart lighttpd
- Apply the changes by restarting the Lighttpd service:
Testing Your Configuration
- Point your domains (
example.com
andexample.org
) to your server’s IP address using DNS configuration. - 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!