基于typecho搭建个人博客
摘要
本文详细讲解了基于typecho搭建个人博客的完整流程,请放心食用。
- 服务器:2核2G,3M带宽,40G SSD
- 操作系统:Ubuntu 24.04
- 部署方式:手动部署
- 架构:Nginx + Typecho + PHP + Mysql
一.架构
硬件资源
服务器:2核2G,3M带宽,40G SSD
阿里云99计划购买的服务器,一年。
操作系统:Ubuntu 24.04
软件
采用 Nginx 1.24.0 作为Web服务器
采用 Mysql 8.0.41 作为数据库
安装 PHP 8.3.6 环境和扩展
二.安装
参考:官方安装文档
环境要求
- PHP 7.4 以上
- MySQL, PostgreSQL, SQLite 任意一种数据库支持,并在 PHP 中安装了相关扩展
- CURL 扩展支持
- mbstring 或 iconv 扩展支持
1.安装PHP
安装PHP环境和相关 PHP 扩展
# 这里仅安装 php8.3-mysql mysql的扩展,因为这里采用mysql作为数据库
# 如果使用postgresql 或者 SQLite,则安装对应的扩展 php8.3-sqlite3 php8.3-pgsql
apt install php8.3-fpm php8.3-common php8.3-curl php8.3-mysql php8.3-mbstringPHP版本
# php -v
PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologiesphp-fpm 版本,后续用于nginx处理php
# php-fpm8.3 -v
PHP 8.3.6 (fpm-fcgi) (built: Dec 2 2024 12:36:18)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologiesphp-fpm 对应服务
systemctl status php8.3-fpm.service php-fpm 对应配置文件
# 默认配置池文件,可以调整用户,用户组,listen等信息
/etc/php/8.3/fpm/pool.d/www.conf
# 配置文件,可以调整上传大小,开启opcache等配置
/etc/php/8.3/fpm/php.ini
# 启动的sock文件,后续nginx配置该listen处理php
/run/php/php8.3-fpm.sock
# php-fpm启动的sock文件是在 /etc/php/8.3/fpm/pool.d/www.conf 中配置的
listen = /run/php/php8.3-fpm.sock
2.安装Mysql数据库
安装数据库
apt install mysql-server-8.0数据库版本
# dpkg -l|grep mysql-
ii mysql-client-8.0 8.0.41-0ubuntu0.24.04.1 amd64 MySQL database client binaries
ii mysql-client-core-8.0 8.0.41-0ubuntu0.24.04.1 amd64 MySQL database core client binaries
ii mysql-common 5.8+1.1.0build1 all MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-server-8.0 8.0.41-0ubuntu0.24.04.1 amd64 MySQL database server binaries and system database setup
ii mysql-server-core-8.0 8.0.41-0ubuntu0.24.04.1 amd64 MySQL database server binaries连接数据库
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1755
Server version: 8.0.41-0ubuntu0.24.04.1 (Ubuntu)
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
3.初始化数据库
创建数据库,创建用户,授权
# 进入数据库,这里根据个人配置连接即可
mysql -u root -p
# 创建数据库,数据库名称自定义,比如:typecho
CREATE DATABASE typecho;
# 创建数据库用户,比如: typecho,但是可能多数人都知道,不安全,可以自定义用户名
# 连接地址根据自身情况选择设置。后续通过127.0.0.1就配置127.0.0.1,通过localhost连接就配置localhost连接,所有主机是*
# 用户名和密码,进行修改调整,这里仅提供示例
# 允许 typechouser 用户通过 localhost 连接mysql数据库,密码是 typechopassword
CREATE USER "typechouser"@"localhost" IDENTIFIED BY "typechopassword";
# 授权 typechouser 通过 localhost 访问Mysql数据库 ,拥有 typecho 数据库的所有权
GRANT ALL PRIVILEGES ON typecho.* TO "typechouser"@"localhost";
# 刷新授权
FLUSH PRIVILEGES;
# 退出
EXIT4.安装typecho
下载上传typecho
# 下载
wget https://github.com/typecho/typecho/releases/latest/download/typecho.zip
# 解压
unzip typecho.zip -d typecho
# 这里将typecho放置在了/opt 目录下,即typecho家目录为
/opt/typecho
# 授权,否则后续会提示 /usr/uploads 目录没有权限,无法创建 config.inc.php 等问题
# 因为没有修改php-fpm的用户,默认是 www-data,所以这里对/opt/typecho目录和下面的所有子目录授予 www-data 权限
chown www-data /opt/typecho -R5.通过Nginx发布
5.1 安装nginx
apt install nginxnginx版本
# nginx -V
nginx version: nginx/1.24.0 (Ubuntu)
built with OpenSSL 3.0.13 30 Jan 2024
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/nginx-DlMnQR/nginx-1.24.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/nginx-DlMnQR/nginx-1.24.0=/usr/src/nginx-1.24.0-2ubuntu7.1 -fPIC -Wdate-time -D_FORTIFY_SOURCE=3' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --with-http_perl_module=dynamic --with-http_xslt_module=dynamic --with-mail=dynamic --with-stream=dynamic --with-stream_geoip_module=dynamic5.2 发布typecho
参考: 常见问题 中的nginx推荐配置
个人习惯将nginx配置文件放置在 /etc/nginx/conf.d/ 目录中
创建配置文件 /etc/nginx/conf.d/typecho.conf
server {
# 监听端口
listen 80;
# 网站名称
# server_name yourdomain.com;
server_name _;
# typecho 路径
root /opt/typecho/;
# 支持 index.php
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ .*\.php(\/.*)*$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
}
access_log /var/log/nginx/typecho_access.log combined;
}其中内容,根据自身需求进行调整。
检查nginx配置,如果有问题,处理问题,没有问题进行reload
nginx -t热加载
nginx -s reload6.配置typecho
访问页面,比如:http://yourdomain.com ,应该会自动跳转到安装地址 http://yourdomain.com/install.php
初始化配置
之前在mysql上,已经为typecho创建了数据库和用户,并进行了授权
数据库连接信息示例
访问地址:localhost
用户名: typechouser
密码: typechopassword
数据库: typecho相关安装设置
数据库适配器(两个选项:Pdo 驱动 Mysql 适配器 / Mysql 原生函数适配器,因为只安装了php关于mysql的扩展,如果想要使用SQLite或者PostgreSQL,则需要安装对应的扩展 php8.3-sqlite3 或 php8.3-pgsql,会显示相关的选项)
这里选择默认的 Pdo 驱动 Mysql 适配器
数据库前缀(默认前缀是 "typecho_")
typecho_
数据库地址(您可能会使用 "localhost")
localhost
数据库用户名(您可能会使用 "root"。由于为typecho单独创建了用户,所以这里填写创建的用户信息)
typechouser
数据库密码
typechopassword
数据库名(请您指定数据库名称)
typecho
高级选项
数据库端口(如果您不知道此选项的意义, 请保留默认设置)
3306
数据库编码(选择 utf8mb4 编码至少需要 MySQL 5.5.3 版本,两个选项,utf8mb4 和 utf8)
utf8mb4
默认就是 utf8mb4,查了下 utf8mb4 支持emoji表情
数据库引擎
InnoDB
数据库 SSL 证书(如果您的数据库启用了 SSL,请填写 CA 证书路径,否则请留空)
这里没有启用,留空
启用数据库 SSL 服务端证书验证
启用
这里也是采用的默认,没有调整
创建您的管理员账号
网站地址
http://yourdomain.com
这是程序自动匹配的网站路径, 如果不正确请修改它
用户名(请填写您的用户名)
自定义用户名
登录密码(请填写您的登录密码, 如果留空系统将为您随机生成一个)
自定义登录密码
邮件地址(请填写一个您的常用邮箱)
邮箱地址这里就按照要求填写
7.安装成功
安装成功,会有提示信息
- 会提示用户名,密码信息
- 有点击访问控制面板地址
- 有点击查看Blog地址
希望您能尽情享用 Typecho 带来的乐趣!
到这里,没有问题的话,应该就安装成功了。后续就是个人对typecho的探索了。