搭建基于 SQLite 的 Typecho 站点

博客系统从 Hugo 迁移到 Typecho 了,这里简单记录一下安装配置过程。

环境为 Ubuntu 20.04,总的来说挺顺利的。

安装

首先安装 PHP 8.0、SQLite 扩展,以及一些和 handsome 主题相关的扩展。

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt install php8.0-fpm php8.0-sqlite3 php8.0-mbstring php8.0-curl
$ systemctl status php8.0-fpm

配置 PHP

$ grep ^listen /etc/php/8.0/fpm/pool.d/www.conf
listen = /run/php/php8.0-fpm.sock
listen.owner = www-data
listen.group = www-data

配置 Nginx

$ sudo rm -f /etc/nginx/sites-available/*
$ sudo rm -f /etc/nginx/sites-enabled/*
$ sudo vi /etc/nginx/conf.d/runsisi.com.conf
server {
    listen 80;
    listen 443 ssl;
    server_name www.runsisi.com;
    ssl_certificate /home/runsisi/runsisi.com/certs/runsisi.com.crt;
    ssl_certificate_key /home/runsisi/runsisi.com/certs/runsisi.com.key;

    return 301 https://runsisi.com$request_uri;
}

server {
    listen 80;
    server_name runsisi.com;

    return 301 https://runsisi.com$request_uri;
}

server {
    listen 443 ssl;
    server_name runsisi.com;
    ssl_certificate /home/runsisi/runsisi.com/certs/runsisi.com.crt;
    ssl_certificate_key /home/runsisi/runsisi.com/certs/runsisi.com.key;

    root /home/runsisi/runsisi.com/typecho;

    location / {
        include fastcgi.conf;

        ### comment out these overrides if pseudo static is disabled
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param SCRIPT_NAME /index.php;

        fastcgi_index index.php;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }

    location /admin/ {
        include fastcgi.conf;

        fastcgi_index index.php;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }

    location ~ [^/]\.php(/|$) {
        include fastcgi.conf;

        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }

    location ~* ^.+\.(gif|jpeg|jpg|png|xml|txt|css|js|svg|ico|json|eot|ttf|woff|woff2|swf)$ {
        expires max;
    }
}

当然,这里对于 URL 的处理有多种,网上常见的如下所示:

location / {
    index index.php;

    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
    }

    try_files $uri $uri/ =404;
}

或者:

location / {
    index index.php;

    try_files $uri $uri/ /index.php$uri$is_args$args;
}

配置 Typecho

$ cd typecho/usr/
$ mkdir sqlitedb uploads backups
$ sudo chown www-data.www-data sqlitedb/ uploads/ backups/

然后访问首页按照 Typecho 的提示进行配置即可。

最后,在 设置/永久链接 中启用 地址重写功能 功能。

打开 PHP 调试日志

为了定位问题,可以使用 PHP 的 error_log() 接口打印相关的信息,当然前提是打开相关的调试日志:

$ grep ^error_log /etc/php/8.0/fpm/php-fpm.conf
error_log = /var/log/php8.0-fpm.log
$ grep ^catch /etc/php/8.0/fpm/pool.d/www.conf
catch_workers_output = yes

使用 PhpStorm 调试 PHP

如果简单的日志打印难以定位问题,可以使用调试工具进行精准定位:

$ sudo apt install php8.0-xdebug

$ cat /etc/php/8.0/fpm/conf.d/20-xdebug.ini
zend_extension=xdebug.so

xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.client_host=127.0.0.1
xdebug.client_port=9003

Configure Xdebug
https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html

Zero-configuration debugging
https://www.jetbrains.com/help/phpstorm/zero-configuration-debugging.html

Step Debugging
http://xdebug.org/docs/step_debug

How do I view events fired on an element in Chrome DevTools?
https://stackoverflow.com/questions/10213703/how-do-i-view-events-fired-on-an-element-in-chrome-devtools

参考资料

PHP FastCGI Example
https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

Understanding and Implementing FastCGI Proxying in Nginx
https://www.digitalocean.com/community/tutorials/understanding-and-implementing-fastcgi-proxying-in-nginx

Module ngx_http_fastcgi_module
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html

Nginx location priority
https://stackoverflow.com/questions/5238377/nginx-location-priority

Creating NGINX Rewrite Rules
https://www.nginx.com/blog/creating-nginx-rewrite-rules/

NGINX: try_files is evil too
https://www.getpagespeed.com/server-setup/nginx-try_files-is-evil-too

Nginx: When the index and try_files in the same block, why the try_files will be processed, not the index directive?
https://stackoverflow.com/questions/36175676/nginx-when-the-index-and-try-files-in-the-same-block-why-the-try-files-w

Module ngx_http_core_module
http://nginx.org/en/docs/http/ngx_http_core_module.html


最后修改于 2021-04-10