Typecho 是一个简洁、高效的 PHP 博客系统。为什么选择它?因为它轻量!轻量!轻量!与其选择那些基于 Docker 的部署脚本,不如直接在 Linux 上手动部署,这样不仅能够保持 Typecho 原有的轻量特性,还能避免 Docker 容器带来的额外开销。容器虽然方便,但对于一个本身已经非常轻量的博客系统来说,容器的加入反而显得有些“本末倒置”。当然,如果你对本地环境的隔离要求较严格还是选择docker较为合适。
在这篇教程中,我将展示如何在 Debian 12 系统上,手动安装并优化配置 Typecho,而无需依赖 Docker。通过一步一步的指导,你将学会如何使用 Nginx、MySQL 和 PHP 来搭建一个高效、安全的 Typecho 博客。
1.更新系统
apt upgrade -y
2. 安装 Nginx
systemctl start nginx
systemctl enable nginx
3. 安装 MySQL(MariaDB)
systemctl start mariadb
systemctl enable mariadb
在 Debian 12 中:MariaDB 默认是 10.11,如有指定需求可指定版本
配置 MySQL 安全性
在执行 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 创建数据库和用户
使用root身份连接mysql -u表示指定用户名 -p表示需要输入密码
在 MySQL 命令行中执行:
CREATE USER 'typecho'@'localhost' IDENTIFIED BY '设置一个密码';
GRANT ALL PRIVILEGES ON typecho.* TO 'typecho'@'localhost';
FLUSH PRIVILEGES;
exit;
创建名为typecho的数据库,并创建一个typecho用户,指定其此用户仅允许本地访问,且设置一个密码,然后并授予该用户对 typecho 数据库的所有权限。最后刷新一下。
5. 安装 PHP 及必要扩展
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
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-data,www-data 是 Apache 或 Nginx Web 服务器通常使用的用户和组。
7. 配置 Nginx
方式一:通过域名访问的配置
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+端口访问的配置
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. 启用站点配置
rm /etc/nginx/sites-enabled/default
nginx -t
systemctl restart nginx
9. 访问网站完成安装
现在你可以通过浏览器访问你的域名或服务器IP,会看到 Typecho 的安装界面。按照以下步骤完成安装:
选择"开始安装"
数据库适配器选择 "MySQL"
数据库地址填写 "localhost"
数据库端口保持默认 "3306"
数据库用户名填写 "typecho"
数据库密码填写你之前设置的密码
数据库名填写 "typecho"
创建管理员账号和密码
注意事项:
- 确保防火墙允许 80 端口访问:
ufw allow 80/tcp
ufw enable
2.如果遇到权限问题,可以检查:
chown -R www-data:www-data /var/www/html
3.如果网站打不开,可以查看日志:
4.记得定期备份数据:
配置Https
- 首先安装 certbot 和 Nginx 插件:
2.申请证书:
方式一(交互式):
执行过程中会:
询问你的邮箱地址(用于证书过期提醒)
是否同意服务条款(输入 Y)
是否愿意分享邮箱(可以输入 N)
是否自动将 HTTP 跳转到 HTTPS(输入 2,选择自动跳转)
方式二:
--nginx:表示让 Certbot 自动为 Nginx 配置 SSL。
--non-interactive:禁用交互模式,不会要求您手动输入任何内容。
--agree-tos:自动同意 Let's Encrypt 的服务条款。
--email [email protected]:提供用于接收证书到期提醒的邮箱。
--redirect:自动将 HTTP 流量重定向到 HTTPS
3.检查证书自动续期是否配置正确:
4.手动测试续期命令(不会真的续期,只是测试):
5.查看 Nginx 配置文件,确认 certbot 是否正确修改了配置:
你应该能看到类似这样的配置:
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;
}
6.重启 Nginx 使配置生效:
systemctl restart nginx
7.确认防火墙允许 HTTPS 流量:
现在你可以通过以下方式验证配置:
- 在浏览器访问 http://examle.com - 应该会自动跳转到 https
2.访问 https://examle.com - 应该能看到安全锁标志
3.查看证书详情,确认是 Let's Encrypt 颁发的
自动续期说明:
证书有效期为 90 天
certbot 会自动在证书过期前 30 天尝试续期
续期是自动的,不需要人工干预
如果续期失败,你会收到邮件通知
可能遇到的问题及解决方案:
- 如果申请证书失败,检查:
- 80和443端口是否开放
防火墙设置是否正确
2.如果网站打不开,检查日志:
3.查看证书状态:
4.如果需要手动更新证书:
常用指令
首先得进入mysql
更改mysql root密码:
查看数据库中的用户表:
重置管理员url:
修改管理员用户名:
重置管理员密码:
此处评论已关闭