前言

最近我购买了一台 DMIT VPS,用来搭建个人开发环境和技术博客。

服务器配置如下:

  • 1 vCPU
  • 2 GB RAM
  • 20 GB SSD
  • Ubuntu 24.04 LTS
  • 域名:xiechengxun.com

本文记录从服务器初始化,到部署 Nginx、Docker、HTTPS 和 Hugo 个人博客的过程。

一、初始化 Ubuntu 服务器

通过 SSH 登录服务器后,首先更新软件包:

apt update
apt upgrade -y

安装常用工具:

apt install -y curl wget git vim unzip htop

确认系统版本:

cat /etc/os-release

二、配置域名解析

我使用 Cloudflare 管理域名 DNS。

在 Cloudflare 中添加一条 A 记录:

类型:A
名称:@
IPv4:服务器公网 IP
代理状态:DNS Only

配置完成后,使用下面的命令检查解析:

dig xiechengxun.com

当返回的地址与 VPS 公网 IP 一致时,说明 DNS 已经生效。

三、安装 Nginx

安装 Nginx:

apt install nginx -y

检查服务状态:

systemctl status nginx

查看监听端口:

ss -tlnp | grep nginx

浏览器访问:

http://xiechengxun.com

如果出现 Welcome to nginx!,说明 Nginx 已经正常运行。

四、配置 HTTPS

安装 Certbot 和 Nginx 插件:

apt install -y certbot python3-certbot-nginx

申请 Let’s Encrypt 证书:

certbot --nginx -d xiechengxun.com

测试证书自动续期:

certbot renew --dry-run

如果出现下面的提示,说明自动续期正常:

Congratulations, all simulated renewals succeeded

在 Cloudflare 的 SSL/TLS 页面,将加密模式设置为:

Full (Strict)

五、安装 Docker

使用 Docker 官方安装脚本:

curl -fsSL https://get.docker.com | sh

检查 Docker 服务:

systemctl status docker

运行测试容器:

docker run hello-world

查看 Docker Compose 版本:

docker compose version

六、安装 Hugo

下载并安装 Hugo:

cd /tmp
wget https://github.com/gohugoio/hugo/releases/download/v0.164.0/hugo_0.164.0_linux-amd64.deb
apt install -y ./hugo_0.164.0_linux-amd64.deb

检查版本:

hugo version

七、创建 Hugo 博客

创建博客项目:

hugo new site /opt/blog --format yaml
cd /opt/blog

安装 PaperMod 主题:

git clone --depth=1 \
  https://github.com/adityatelange/hugo-PaperMod.git \
  themes/PaperMod

博客的主要配置文件是:

/opt/blog/hugo.yaml

我在配置中设置了博客标题、首页介绍、分页、文章阅读时间、代码复制按钮,以及文章、归档、标签和关于我等菜单。

八、创建文章

创建一篇新文章:

cd /opt/blog
hugo new content posts/first-post.md

编辑文章:

nano /opt/blog/content/posts/first-post.md

准备发布时,需要确认文章头部为:

draft: false

九、构建并部署博客

生成静态网页:

cd /opt/blog
hugo --minify

备份原来的 Nginx 网站目录:

cp -a /var/www/html /root/html-backup-$(date +%F-%H%M%S)

将生成的网页复制到 Nginx 目录:

rm -rf /var/www/html/*
cp -a /opt/blog/public/. /var/www/html/

检查并重新加载 Nginx:

nginx -t
systemctl reload nginx

现在访问:

https://xiechengxun.com

就可以看到 Hugo 博客。

十、以后如何发布新文章

创建文章:

cd /opt/blog
hugo new content posts/article-name.md

编辑文章:

nano /opt/blog/content/posts/article-name.md

构建并发布:

cd /opt/blog
hugo --minify
rm -rf /var/www/html/*
cp -a public/. /var/www/html/

总结

至此,我完成了以下工作:

  • 初始化 Ubuntu 24.04 VPS
  • 配置 Cloudflare DNS
  • 安装并配置 Nginx
  • 申请 Let’s Encrypt HTTPS 证书
  • 安装 Docker 与 Docker Compose
  • 安装 Hugo
  • 使用 PaperMod 搭建个人博客
  • 将静态页面部署到 Nginx

Hugo 生成的是纯静态页面,不需要 PHP 和数据库,资源占用很低,非常适合小型 VPS。

后续我还会继续记录 AI、Linux、Docker、VPS 和开发相关的实践笔记。