How to Self-Host a Static Website with Hugo and Nginx
Introduction
Hugo is a fast and flexible static site generator, perfect for creating blogs and documentation sites. Pairing it with Nginx allows you to self-host your website efficiently. This guide will walk you through setting up Hugo, generating a static website, and serving it with Nginx.
Step 1: Installing Hugo
-
Update your system’s package list:
sudo apt update && sudo apt upgrade -y
-
Install Hugo:
sudo apt install -y hugo
-
Verify that Hugo is installed:
hugo version
Step 2: Creating a New Hugo Site
-
Create a new Hugo project:
hugo new site mywebsite
-
Navigate into the project directory:
cd mywebsite
-
Add a theme (example: “ananke”):
git init && git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
-
Enable the theme in
config.toml
:echo 'theme = "ananke"' >> config.toml
Step 3: Creating and Building Your Content
-
Create a new page:
hugo new posts/my-first-post.md
-
Edit the post with a text editor:
nano content/posts/my-first-post.md
-
Generate the static site:
hugo -D
-
The generated files will be in the
public
directory.
Step 4: Installing and Configuring Nginx
-
Install Nginx:
sudo apt install -y nginx
-
Create a configuration file for your site:
sudo nano /etc/nginx/sites-available/mywebsite
-
Add the following content:
server {
listen 80;
server_name yourdomain.com;
root /var/www/mywebsite/public;
index index.html;
}
-
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled
-
Restart Nginx:
sudo systemctl restart nginx
Step 5: Deploying Your Hugo Site
-
Copy the generated files to the web root:
sudo cp -r public/* /var/www/mywebsite
-
Ensure the proper permissions:
sudo chown -R www-data:www-data /var/www/mywebsite
-
Test the Nginx configuration:
sudo nginx -t
-
Open a browser and visit
http://yourdomain.com
to see your site live.
Conclusion
By following this guide, you have successfully created a static website using Hugo and served it with Nginx. This setup is lightweight, fast, and easy to maintain.