Ubuntu 系统重装后建站完整流程文档(含 Nginx + PHP + Typecho + HTTPS + 自动续期)
适用于 Ubuntu 22.04 LTS 系统环境,自编译 Nginx + PHP-FPM 架构,脱离 LNMP 全家桶,含 HTTPS 和自动续期配置。
🧱 一、系统准备
1. 更新系统
sudo apt update && sudo apt upgrade -y
2. 安装依赖包
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev unzip curl -y
🌐 二、安装 Nginx(源码方式)
1. 下载并编译
cd /usr/local/src
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && sudo make install
2. 创建目录结构
sudo mkdir -p /usr/local/nginx/conf/sites-enabled
sudo mkdir -p /var/log/nginx
3. 修改主配置文件 nginx.conf
加入:
include /usr/local/nginx/conf/sites-enabled/*.conf;
⚠️ 如果未修改 nginx.conf 并且每个站点的配置都已经直接加入 /usr/local/nginx/conf/nginx.conf 中,也可以保持现状。推荐使用 include 统一管理,方便扩展与维护。
🐘 三、安装 PHP-FPM
sudo apt install php8.1 php8.1-fpm php8.1-mysql -y
sudo systemctl enable --now php8.1-fpm
默认监听路径:
/run/php/php8.1-fpm.sock
🌐 四、网站文件恢复与配置
网站目录统一为:
/home/wwwroot/
恢复 blog(Typecho)等静态站点:
cp -a /opt/server/site/wwwroot/* /home/wwwroot/
chown -R www-data:www-data /home/wwwroot
✏️ 五、Nginx 虚拟主机配置参考
1. 博客站点(PHP)blog.fufubest.com.conf
server {
listen 80;
server_name blog.fufubest.com;
root /home/wwwroot/blog.fufubest.com;
index index.php index.html;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
2. 静态站点参考(如 fufubest.com)
server {
listen 80;
server_name fufubest.com;
root /home/wwwroot/fufubest.com/dist;
index index.html;
access_log /var/log/nginx/fufubest.access.log;
error_log /var/log/nginx/fufubest.error.log;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf)$ {
expires 30d;
access_log off;
add_header Cache-Control "public";
gzip_static on;
}
}
🔐 六、配置 HTTPS(使用 Certbot)
安装 Certbot
sudo apt install certbot python3-certbot-nginx -y
为每个域名申请证书
sudo certbot --nginx -d blog.fufubest.com
sudo certbot --nginx -d fufubest.com
sudo certbot --nginx -d shader.fufubest.com
...
成功后,Nginx 会自动补全 HTTPS 配置。
♻️ 七、自动续期与重载
新建脚本
/root/shell/certbot-renew-with-nginx.sh
#!/bin/bash
certbot renew --quiet && /usr/local/nginx/sbin/nginx -s reload
新建 systemd 服务单元
/etc/systemd/system/certbot-renew-nginx.service
[Unit]
Description=Renew Let's Encrypt certificates and reload Nginx
[Service]
Type=oneshot
ExecStart=/root/shell/certbot-renew-with-nginx.sh
/etc/systemd/system/certbot-renew-nginx.timer
[Unit]
Description=Daily timer to renew certificates
[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=true
[Install]
WantedBy=timers.target
启用定时器
sudo chmod +x /root/shell/certbot-renew-with-nginx.sh
sudo systemctl daemon-reload
sudo systemctl enable --now certbot-renew-nginx.timer
✅ 检查状态
查看计时器是否生效
systemctl list-timers | grep certbot
手动执行测试
sudo systemctl start certbot-renew-nginx.service
查看证书有效期
sudo certbot certificates
📌 附注:自定义配置说明
- 所有站点配置未使用 /etc/nginx/sites-enabled,而是采用自编译路径 /usr/local/nginx/conf/sites-enabled/*.conf 管理。
- 如果未修改 nginx.conf 中的 include 路径,请手动将每个站点配置追加到主配置文件中。
- blog.fufubest.com 使用 Typecho + PHP-FPM,其余如 shader.fufubest.com、fufubest.com 等为静态打包内容,alist.fufubest.com 为 Docker 容器内的 Alist,通过 Nginx 代理端口转发。