CentOS 8.4 部署 Golang 项目上线完整记录
域名:songrc.com
服务器 IP:106.13.3.111
系统:CentOS Linux release 8.4.2105
Go 版本:go1.21.9
数据库:MySQL 8.0.46
项目路径:/mnt/blog/
应用端口:8181
一、上传项目代码
方式一 Git 拉取:
mkdir -p /mnt/blog
cd /mnt/blog
git clone https://你的仓库地址.git .
方式二 scp 上传:
scp -r /本地项目路径/* root@106.13.3.111:/mnt/blog/
二、安装 MySQL 8.0
2.1 下载官方 Yum 源
wget https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql80-community-release-el7-11.noarch.rpm
rpm -ivh mysql80-community-release-el7-11.noarch.rpm
2.2 禁用默认 mysql 模块(CentOS 8 必须)
yum module disable -y mysql
2.3 安装
yum install -y mysql-community-server
2.4 启动并开机自启
systemctl start mysqld
systemctl enable mysqld
2.5 获取临时密码
grep 'temporary password' /var/log/mysqld.log
输出示例:A temporary password is generated for root@localhost: pIIXChQCe3=d
2.6 安全初始化
mysql_secure_installation
依次操作:
- 输入临时密码
- 设置新 root 密码(需大小写+数字+特殊字符,8位以上)
- Change the password for root → N
- Remove anonymous users → Y
- Disallow root login remotely → N(需要远程连接)
- Remove test database → Y
- Reload privilege tables → Y
2.7 创建数据库并允许远程登录
mysql -u root -p
SQL 操作:
CREATE DATABASE blog DEFAULT CHARACTER SET utf8mb4;
CREATE USER 'root'@'%' IDENTIFIED BY '你的密码';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
SHOW DATABASES;
EXIT;
2.8 开放远程访问
cat /etc/my.cnf | grep bind-address
如果绑定的是 127.0.0.1,修改为 0.0.0.0:
vi /etc/my.cnf
添加或修改:bind-address = 0.0.0.0
systemctl restart mysqld
云服务器还需在控制台安全组中放行 3306 端口。
三、配置数据库连接并编译
3.1 修改数据库 DSN
项目没有独立配置文件,数据库连接写在 model/db.go 中:
vi /mnt/blog/model/db.go
将原来的:
dsn := "root@tcp(127.0.0.1:3306)/blog?charset=utf8mb4&parseTime=true"
改为(加上密码):
dsn := "root:你的密码@tcp(127.0.0.1:3306)/blog?charset=utf8mb4&parseTime=true"
3.2 编译
cd /mnt/blog
go mod tidy
go build -o blog .
3.3 验证
ls -lh blog
file blog
应显示:ELF 64-bit LSB executable, x86-64
四、配置 systemd 服务
vi /etc/systemd/system/blog.service
内容:
[Unit]
Description=Blog Go Application
After=network.target
[Service]
Type=simple
WorkingDirectory=/mnt/blog
ExecStart=/mnt/blog/blog
Restart=always
RestartSec=5
Environment=GIN_MODE=release
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start blog
systemctl enable blog
systemctl status blog
看到 active (running) 即成功。
确认端口
ss -tlnp | grep blog
输出:LISTEN 0 128 *:8181 : users:((“blog”,pid=xxx,fd=8))
五、安装并配置 Nginx
5.1 安装
yum install -y nginx
5.2 配置反向代理
vi /etc/nginx/conf.d/songrc.com.conf
内容:
server {
listen 80;
server_name songrc.com www.songrc.com;
location / {
proxy_pass http://127.0.0.1:8181;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
5.3 启动
nginx -t
systemctl start nginx
systemctl enable nginx
六、域名 DNS 解析
到域名注册商控制台(阿里云、腾讯云、Cloudflare 等),为 songrc.com 添加 A 记录:
- A 记录 → @ → 106.13.3.111 → TTL 600
- A 记录 → www → 106.13.3.111 → TTL 600
验证 DNS 生效:
ping songrc.com
ping www.songrc.com
确认返回 106.13.3.111
七、配置 HTTPS(Let’s Encrypt 免费证书)
7.1 安装 Certbot
yum install -y epel-release
yum install -y certbot python3-certbot-nginx
7.2 申请证书
certbot --nginx -d songrc.com -d www.songrc.com
依次操作:
- 输入邮箱 → 回车
- Agree to terms → Y
- Share email → N
- 选择重定向 → 2(Redirect all HTTP to HTTPS)
7.3 证书信息
- 证书路径:/etc/letsencrypt/live/songrc.com/fullchain.pem
- 私钥路径:/etc/letsencrypt/live/songrc.com/privkey.pem
- 到期时间:2026-08-26
- 自动续期:Certbot 已自动配置定时任务
八、部署完成
- Go 项目编译部署 → 已完成
- MySQL 8.0 安装 → 已完成
- blog 数据库创建 → 已完成
- systemd 服务(8181端口) → 运行中
- Nginx 反向代理 → 运行中
- 域名 DNS 解析 → 已生效
- HTTPS 证书 → 已配置,自动续期
访问地址:
九、日常维护命令
查看服务状态:
systemctl status blog
查看实时日志:
journalctl -u blog -f
更新代码并重新部署:
cd /mnt/blog
git pull
go build -o blog .
systemctl restart blog
重启 Nginx:
systemctl restart nginx
查看证书到期时间:
certbot certificates