Hugo is a popular static website generator that allows users to create fast and flexible websites without the need for a database. Nginx is a widely used web server known for its speed and reliability. In this article, we will walk you through the process of running a Hugo-based blog with Nginx and Let’s Encrypt on a Debian server.
Step 1: Install Hugo
First, we need to install Hugo on our Debian server. To do this, we can run the following commands:
sudo apt update
sudo apt install hugo
Step 2: Create a New Hugo Website
Now that Hugo is installed, we can create a new Hugo website by running the following command:
hugo new site myblog
This will create a new Hugo website in a directory called “myblog”.
Step 3: Install a Theme
Hugo has a wide range of themes available that can be installed easily. To install a theme, we can navigate to the “myblog” directory and run the following command:
cd myblog
git init
git submodule add https://github.com/<username>/<theme-name> themes/<theme-name>
Replace <username>
and <theme-name>
with the appropriate values for the theme you want to install.
Step 4: Create a New Post
To create a new post, we can navigate to the “myblog” directory and run the following command:
hugo new posts/my-first-post.md
This will create a new Markdown file called “my-first-post.md” in the “content/posts” directory.
Step 5: Configure Nginx
Now that our website is set up, we need to configure Nginx to serve it. To do this, we need to create a new Nginx server block file. We can do this by running the following command:
sudo nano /etc/nginx/sites-available/myblog
In this file, we can add the following configuration:
server {
listen 80;
server_name myblog.com www.myblog.com;
root /var/www/myblog/public;
location / {
try_files $uri $uri/ /index.html;
}
}
This configuration tells Nginx to listen on port 80 and serve our website from the “/var/www/myblog/public” directory. We are also telling Nginx to try serving the requested URL as a file, then as a directory, and finally fallback to serving the “index.html” file.
Step 6: Enable the Nginx Server Block
After we have created our Nginx server block file, we need to enable it. We can do this by creating a symbolic link from the “sites-available” directory to the “sites-enabled” directory:
sudo ln -s /etc/nginx/sites-available/myblog /etc/nginx/sites-enabled/
Step 7: Install Let’s Encrypt SSL Certificate
To secure our website with an SSL certificate from Let’s Encrypt, we need to install the Certbot client. We can do this by running the following commands:
sudo apt-get update
sudo apt-get install certbot python3-certbot-nginx
Now that Certbot is installed, we can use it to request an SSL certificate from Let’s Encrypt. We can do this by running the following command:
sudo certbot --nginx -d myblog.com -d www.myblog.com
This command will request an SSL certificate for the “myblog.com” and “www.myblog.com” domains and configure Nginx to use the SSL certificate.
Step 8: Start Nginx
Finally, we need to start Nginx to apply our changes:
:
sudo systemctl start nginx
That’s it! Our Hugo-based blog is now up and running on our Debian server with Nginx and Let’s Encrypt.