📖超轻量博客系统typecho——本地手动部署教程

博客地址:youdao.li
RSS源:https://youdao.li/feed/
Typecho 是一个简洁、高效的 PHP 博客系统。为什么选择它?因为它轻量!轻量!轻量!与其选择那些基于 Docker 的部署脚本,不如直接在 Linux 上手动部署,这样不仅能够保持 Typecho 原有的轻量特性,还能避免 Docker 容器带来的额外开销。容器虽然方便,但对于一个本身已经非常轻量的博客系统来说,容器的加入反而显得有些“本末倒置”。当然,如果你对本地环境的隔离要求较严格还是选择docker较为合适。
在这篇教程中,我将展示如何在 Debian 12 系统上,手动安装并优化配置 Typecho,而无需依赖 Docker。通过一步一步的指导,你将学会如何使用 Nginx、MySQL 和 PHP 来搭建一个高效、安全的 Typecho 博客。

1. 更新系统

apt update
apt upgrade -y

2. 安装 Nginx

apt install nginx -y
systemctl start nginx
systemctl enable nginx

3. 安装 MySQL(MariaDB)

apt install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
  • 在 Debian 12 中:MariaDB 默认是 10.11,如有指定需求可指定版本
    配置 MySQL 安全性
mysql_secure_installation

在执行 mysql_secure_installation 时,会问你几个问题:

  • Enter current password for root: 直接按回车
  • Set root password? [Y/n]:输入 Y,然后设置 root 密码
  • Remove anonymous users? [Y/n]:输入 Y
  • Disallow root login remotely? [Y/n]:输入 Y
  • Remove test database? [Y/n]:输入 Y
  • Reload privilege tables now? [Y/n]:输入 Y

4. 为 Typecho 创建数据库和用户

mysql -u root -p
  • 使用root身份连接mysql -u表示指定用户名 -p表示需要输入密码
    在 MySQL 命令行中执行:
CREATE DATABASE typecho;
CREATE USER 'typecho'@'localhost' IDENTIFIED BY '设置一个密码';
GRANT ALL PRIVILEGES ON typecho.* TO 'typecho'@'localhost';
FLUSH PRIVILEGES;
exit;
  • 创建名为typecho的数据库,并创建一个typecho用户,指定其此用户仅允许本地访问,且设置一个密码,然后并授予该用户对 typecho 数据库的所有权限。最后刷新一下。

5. 安装 PHP 及必要扩展

apt install php-fpm php-mysql php-gd php-curl php-mbstring php-xml php-zip -y
systemctl start php8.2-fpm
systemctl enable php8.2-fpm
  • 在 Debian 12 中:PHP 默认是 8.2,如有指定需求可指定版本,指定版本后,记得修改nginx配置文件中对应的部分。例如指定为8.0,那么php8.2-fpm.sock应该为php8.0-fpm.sock

6. 下载和配置 Typecho

cd /var/www/html
wget https://github.com/typecho/typecho/releases/latest/download/typecho.zip
apt install unzip -y
unzip typecho.zip
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
chmod -R 777 /var/www/html/usr/uploads
  • 远程下载typecho源文件并解压
  • /var/www/html 及其所有文件和子目录的所有者和所属组设置为 www-datawww-data 是 Apache 或 Nginx Web 服务器通常使用的用户和组。

7. 配置 Nginx

nano /etc/nginx/sites-available/typecho

方式一:通过域名访问的配置

server {
    listen 80;
    server_name examle.com;  # 替换为你的域名或服务器IP
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

方式二:通过ip 端口访问的配置

server {
    listen 8080;  # 改为你想要的端口号
    server_name 你的服务器IP;  # 直接填写服务器IP
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

8. 启用站点配置

ln -sf /etc/nginx/sites-available/typecho /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default
nginx -t
systemctl restart nginx

9. 访问网站完成安装

现在你可以通过浏览器访问你的域名或服务器IP,会看到 Typecho 的安装界面。按照以下步骤完成安装:

  • 选择"开始安装"
  • 数据库适配器选择 "MySQL"
  • 数据库地址填写 "localhost"
  • 数据库端口保持默认 "3306"
  • 数据库用户名填写 "typecho"
  • 数据库密码填写你之前设置的密码
  • 数据库名填写 "typecho"
  • 创建管理员账号和密码

注意事项:

  1. 确保防火墙允许 80 端口访问:
apt install ufw
ufw allow 80/tcp
ufw enable
  1. 如果遇到权限问题,可以检查:
chmod -R 755 /var/www/html
chown -R www-data:www-data /var/www/html
  1. 如果网站打不开,可以查看日志:
tail -f /var/log/nginx/error.log
  1. 记得定期备份数据:
mysqldump -u root -p typecho > typecho_backup.sql

配置Https

  1. 首先安装 certbot 和 Nginx 插件:
apt install certbot python3-certbot-nginx -y
  1. 申请证书:
    方式一(交互式):
certbot --nginx -d examle.com

执行过程中会:

  • 询问你的邮箱地址(用于证书过期提醒)
  • 是否同意服务条款(输入 Y)
  • 是否愿意分享邮箱(可以输入 N)
  • 是否自动将 HTTP 跳转到 HTTPS(输入 2,选择自动跳转)
    方式二:
certbot --nginx -d example.com --non-interactive --agree-tos --email [email protected] --redirect
  • --nginx:表示让 Certbot 自动为 Nginx 配置 SSL。
  • --non-interactive:禁用交互模式,不会要求您手动输入任何内容。
  • --agree-tos:自动同意 Let's Encrypt 的服务条款。
  • --email [email protected]:提供用于接收证书到期提醒的邮箱。
  • --redirect:自动将 HTTP 流量重定向到 HTTPS
  1. 检查证书自动续期是否配置正确:
systemctl status certbot.timer
  1. 手动测试续期命令(不会真的续期,只是测试):
certbot renew --dry-run
  1. 查看 Nginx 配置文件,确认 certbot 是否正确修改了配置:
cat /etc/nginx/sites-available/typecho

你应该能看到类似这样的配置:

server {
    server_name examle.com;
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/examle.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/examle.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    if ($host = examle.com) {
        return 301 https://$host$request_uri;
    }

    listen 80;
    server_name examle.com;
    return 404;
}
  1. 重启 Nginx 使配置生效:
nginx -t
systemctl restart nginx
  1. 确认防火墙允许 HTTPS 流量:
ufw allow 443/tcp

现在你可以通过以下方式验证配置:

  1. 在浏览器访问 http://examle.com - 应该会自动跳转到 https
  2. 访问 https://examle.com - 应该能看到安全锁标志
  3. 查看证书详情,确认是 Let's Encrypt 颁发的

自动续期说明:

  • 证书有效期为 90 天
  • certbot 会自动在证书过期前 30 天尝试续期
  • 续期是自动的,不需要人工干预
  • 如果续期失败,你会收到邮件通知

可能遇到的问题及解决方案:

  1. 如果申请证书失败,检查:
- 域名是否正确解析到服务器IP
- 80和443端口是否开放
- 防火墙设置是否正确
  1. 如果网站打不开,检查日志:
tail -f /var/log/nginx/error.log
  1. 查看证书状态:
certbot certificates
  1. 如果需要手动更新证书:
certbot renew

常用指令

首先得进入mysql
mysql -u root -p

更改mysql root密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'F)!1x5n1>4>ipf,rUXrQaA1D0d';
查看数据库中的用户表:
SELECT * FROM typecho_users;
重置管理员url:
UPDATE typecho_users SET url = 'https://新域名' WHERE uid = 1;
修改管理员用户名:
UPDATE typecho_users SET name = '新管理员用户名' WHERE name = 'admin';
重置管理员密码:
UPDATE typecho_users SET password = MD5('新密码') WHERE uid = 1;
点赞
  1. poi说道:

    不错不错,我都一键就完事了,比如BT一键。

  2. Rational说道:

    @tof #50
    没用过 不清楚

发表回复

电子邮件地址不会被公开。必填项已用 * 标注

×
订阅图标按钮