Total Pageviews

Sunday, 23 March 2025

用Docker搭建静态博客程序Hugo


环境
#系统软件环境
Server: Debian 11

Docker: 20.10.5

#环境安装

#更新包
apt-get install update
#安装Docker
apt-get install docker.io -y
#安装Docker-Compose
apt-get install docker-compose

注:需具备Linux系统简单操作能力,需会使用Docker基本命令

#简述
陆续也使用过一些博客程序,例如Hexo、Vuepress等,发现Hugo的一个主题各方面很符合审美,因此准备将博客迁移。

#目录说明
#实操
#创建相关的目录

#博客根目录
mkdir doduo.cc
#博客源代码目录
mkdir src
#头像文件目录
mkdir assets/img
#静态文件目录
mkdir static

#获取主题示例
博客Github地址:https://github.com/CaiJimmy/hugo-theme-stack

执行以下命令

#克隆主题源代码
git clone https://github.com/CaiJimmy/hugo-theme-stack/ themes/hugo-theme-stack
#复制示例网站源码
cp -r themes/hugo-theme-stack/exampleSite/content ./

cp themes/hugo-theme-stack/exampleSite/hugo.yaml ./config.yaml


content目录保存文章数据,config.yaml 自定义网站的配置,可以参考hugo-theme-stack官方教程,config.yaml 需要配置 baseurl,网站 才可以正常展示。


#Dockerfile:
 
FROM node:14-alpine as node_modules

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
ENV LC_ALL=en_US.UTF-8

RUN echo "Asia/Shanghai" > /etc/timezone

WORKDIR /app

FROM klakegg/hugo:0.107.0-ext-alpine as hugo

WORKDIR /src

COPY ./src/ /src/


RUN hugo

FROM nginx:1.19.7-alpine

RUN  rm -rf /usr/share/nginx/html/*
COPY --from=hugo /src/public /usr/share/nginx/html


#docker-compose.yml:

version: "3"

services:
  server:
    build:
      context: .
    container_name: hugo-server
    restart: always
    ports:
      - "20040:80"


#部署
部署网站:
docker-compose up --build -d

删除中间镜像:

docker rmi $(docker images -f "dangling=true" -q)

#Nginx反向代理配置:

server {
    listen 20443 ssl;
    server_name blog.doduo.cc;
    ssl_certificate  /etc/nginx/cert/doduo.cc/fullchain.cer;
    ssl_certificate_key /etc/nginx/cert/doduo.cc/doduo.cc.key;

    location ~ /tags.*[^/]$ {
        try_files $uri @rewrite;
    }
    location ~ /categories.*[^/]$ {
        try_files $uri @rewrite;
    }
    location @rewrite {
        return 302 $scheme://$http_host$uri/;
    }

    location / {
        proxy_pass  http://96.43.94.91:20040;
    }
}

server {
    listen 80;
    server_name blog.doduo.cc;
    rewrite ^(.*)$ https://$host$1 permanent;
}

#参考:
https://stack.jimmycai.com/guide/getting-started, hugo-theme-stack官方教程
https://gohugo.io/documentation/
https://serverfault.com/questions/739498/nginx-rewrite-on-docker-machine-when-host-port-container-port/1053762#1053762

No comments:

Post a Comment