前言
本文將介紹如何搭建基於Typecho的個人網站,以及分享一些遇到問題的解決方案。
Why Typecho
Typecho是一個由國人開發的輕量級CMS,和WordPress一樣,可以快速建立個人博客網站。So Why Typecho?
WordPress有無數的優點,但是選擇Typecho的理由只需一個:簡單,簡潔,輕量級。
Typecho幾乎是專門爲個人博客打造的,全部代碼不足400KB,也不像WordPress一樣對主機性能有一定的要求。界面和控制檯都是極簡風,非常清爽,很容易上手。對MarkDown支持非常友好,不需要額外的插件。
環境
Typecho的推薦環境是LNMP(Linux, Nginx, MySQL, PHP),跟WordPress非常相似,可以共用。
因爲本人之前寫過在Ubuntu上搭建WordPress環境的步驟,爲避免重複造輪,Linux,MySQL,PHP7的部分可以參考這裏。
Nginx
安裝Nginx
sudo apt-get install nginx
驗證Nginx
systemctl status nginx
會得到如下輸出
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-12-05 10:19:16 CST; 4h 29min ago
Process: 80264 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 80384 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 80380 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
啓動Nginx
sudo systemctl start nginx
修改Nginx配置
Nginx默認安裝在 /etc/nginx/目錄下,在此目錄下找到默認的配置文件sites-enabled/default(不同的Nginx版本或者操作系統文件會有區別),把index.php加到index標籤下,同時將PHP對應的location打開
index **index.php** index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
要確保已經安裝了php-fpm,否則Nginx無法正常給php做轉發。如果還沒有安裝,運行如下命令安裝
sudo apt install php-fpm php-mysql
安裝Typecho
直接從官網下載最新的版本,解壓到Nginx目錄
cd /usr/share/nginx
sudo wget http://typecho.org/downloads/1.1-17.10.30-release.tar.gz
tar -zxvf 1.1-17.10.30-release.tar.gz
cp ./build/* ./html/
不要忘了修改一下Nginx發佈目錄的權限
cd /usr/share/nginx/html
chmod -R 755 *
chown {owner}/{group} *
如果是Ubuntu,owner和group都是www-data,如果是CentOS則事nginx,可以通過以下命令查看用戶
ps -ef | grep nginx
驗證Typecho
現在瀏覽器打開 {ip}:80(nginx默認80端口),可以看到Typecho的歡迎頁面
按照嚮導一步一步走下來,可以看到簡潔清爽的博客界面
PHP7可能遇到的問題
502 bad gateway
如果打開頁面報502 bad gateway,是因爲xml解析不兼容造成的,安裝php7.0-xml即可解決:
- Ubuntu
sudo apt-get install php7.0-xml
- CentOS
yum install php7.0-xml
404 not found
如果打開任何Typecho子頁面都報404 not found,需要在nginx的配置文件添加如下配置
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
在php的location下添加參數fastcgi_split_path_info ^(.+.php)(/.+)$;,參考如下
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/typecho;
index index.php index.html;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
從WordPress遷移到Typecho
Typecho有插件支持從WordPress轉移文章,但是建議安裝老版本的Typecho環境,而且對WordPress的版本有要求,至少博主在WordPress5和Typecho1.1下沒有遷移成功。所以建議不要遷移哈哈哈。
總結
Typecho環境的搭建與WordPress非常相似,如果你是想要一個純粹極簡博文網站,並習慣MarkDown寫文,那就感覺上手Typecho吧,你值得擁有。