搭建Typecho博客(Build Typecho blog)

前言

最开始我用wordpress搭建了博客,因为它的主题、插件非常丰富,功能强大,并且更新活跃。因为我买的便宜的阿里云主机只有1核2G,带宽1M,安装上后发现,单纯浏览博客还行,但是登录后台就非常卡顿。现在对PHP不了解,也没时间去研究,于是我决心换用很轻量的Typecho,最近几个月它又有了更新,所以选择pre-release的1.2版本。

环境配置

apt先更新下。

apt update && apt upgrade

sqlite3

这个博客就是用来记录下学习的琐碎内容,估计也不会有什么流量,所以数据库选最轻量的splite3绰绰有余。

apt install sqlite3

PHP

Typecho 1.2默认可以在php8的环境下运行,如果安装php8的话:

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt update

apt install php8.0-fpm php8.0-sqlite3 php8.0-mbstring php8.0-curl

目前很多开源主题和插件在php8下可能有更多bug,用php7更妥当,如果之前添加过PPA源就先删掉,再安装php7.4:

# 如果添加过
add-apt-repository -r ppa:ondrej/php && apt update

apt install php-fpm php-sqlite3 php-mbstring php-curl

Nginx

直接apt安装nginx

apt install nginx

然后为博客站点编辑配置文件,Ubuntu里在 , 不用也可以生成 新的文件,CentOS的在。

/etc/nginx/sites-available/default
default
/etc/nginx/conf.d/

还没域名和SSL证书前,测试可用:

server {
	listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/html;

  # Add index.php to the list if you are using PHP
  index index.php;

  server_name _;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts to FastCGI server
  #
  location ~ \.php(\/.*)*$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php-fpm.sock;
  }

  if (-f $request_filename/index.html){
  	rewrite (.*) $1/index.html break;
  }
  if (-f $request_filename/index.php){
  	rewrite (.*) $1/index.php;
  }
  if (!-e $request_filename){
  	rewrite (.*) /index.php;
  }
}

购买了域名(国内服务器记得ICP备案和公安备案)以及免费的SSL证书,修改nginx配置

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    root /var/www/html;
    index index.php;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php(\/.*)*$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php-fpm.sock;
    }
    
    if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
    }
    if (!-e $request_filename){
            rewrite (.*) /index.php;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    return 301 https://yczheng.top$request_uri;
}

server {
    listen 80;
    listen [::]:80;

    server_name www.yczheng.top yczheng.top;

    return 301 https://yczheng.top$request_uri;
}

安裝Typecho

为了不因为权限问题导致网站无法运行,这里我将网站放置在目录里 ,php-fpm默认是www-data用户运行,记得给此用户赋予网站的读写权限(或者把网站文件所属赋予www-data)。

/var/www/html
cd ~
# Download pre-release or latest from github
wget https://github.com/typecho/typecho/releases/download/v1.2.0-rc.1/typecho.zip
mv ~/typecho.zip /var/www/html/

cd /var/www/html/
unzip typecho.zip
rm typecho.zip

# php-fpm默认是www-data用户运行
chown -R www-data:www-data /var/www/html/*

然后在本地的浏览器,输入服务器的ip,或者域名(如果配置好),就按照提示一步一步进行就好。

Typecho主题

虽然不如Wordpress,但Typecho仍然有不少主题和插件,在https://typecho.me/能找到很多不错的主题和插件。

我喜欢的一些主题:

  • freewind
  • MWordStar
  • Joe
  • Facile
  • Cuckoo
  • Nexmoe
  • Bearsimple
  • Initial
  • Akina

参考

Configuring Nginx to Serve Your First Site Over HTTPS

https://linuxize.com/post/how-to-install-php-8-on-ubuntu-20-04/#installing-php-80-with-nginx

https://learnku.com/php/t/51997

https://juejin.cn/post/6969113070346403853

https://developer.aliyun.com/article/764024

除此之外,很多Wordpress的建站教程也适用。

————————

preface

At the beginning, I built a blog with WordPress because its themes and plug-ins are very rich, powerful and active. Because the cheap alicloud host I bought has only one core of 2G and a bandwidth of 1m. After installation, I found that it’s OK to simply browse the blog, but it’s very difficult to log in to the background. Now I don’t know PHP and don’t have time to study it, so I decided to change to a very lightweight Typecho. It has been updated in recent months, so I chose version 1.2 of pre release.

Environment configuration

Apt update first.

apt update && apt upgrade

sqlite3

This blog is used to record the trivial contents of learning, and it is estimated that there will be no traffic, so it is more than enough to choose the lightest split3 in the database.

apt install sqlite3

PHP

Typecho 1.2 can run in php8 by default. If php8 is installed:

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt update

apt install php8.0-fpm php8.0-sqlite3 php8.0-mbstring php8.0-curl

At present, many open source themes and plug-ins may have more bugs under php8. It is more appropriate to use php7. If you have added a PPA source before, delete it first and then install php7 4:

# 如果添加过
add-apt-repository -r ppa:ondrej/php && apt update

apt install php-fpm php-sqlite3 php-mbstring php-curl

Nginx

Install nginx directly apt

apt install nginx

Then edit the configuration file for the blog site. In Ubuntu, you can generate new files without using it. CentOS is here.

/etc/nginx/sites-available/default
default
/etc/nginx/conf.d/

Before the domain name and SSL certificate, the test is available:

server {
	listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/html;

  # Add index.php to the list if you are using PHP
  index index.php;

  server_name _;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts to FastCGI server
  #
  location ~ \.php(\/.*)*$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php-fpm.sock;
  }

  if (-f $request_filename/index.html){
  	rewrite (.*) $1/index.html break;
  }
  if (-f $request_filename/index.php){
  	rewrite (.*) $1/index.php;
  }
  if (!-e $request_filename){
  	rewrite (.*) /index.php;
  }
}

Purchased domain name (domestic server registration and public security registration) and free SSL certificate, and modified nginx configuration

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    root /var/www/html;
    index index.php;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php(\/.*)*$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php-fpm.sock;
    }
    
    if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
    }
    if (!-e $request_filename){
            rewrite (.*) /index.php;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    return 301 https://yczheng.top$request_uri;
}

server {
    listen 80;
    listen [::]:80;

    server_name www.yczheng.top yczheng.top;

    return 301 https://yczheng.top$request_uri;
}

安裝Typecho

In order not to prevent the website from running due to permission problems, I put the website in the directory. PHP FPM is run by www data user by default. Remember to give this user the read-write permission of the website (or give the website file to WWW data).

/var/www/html
cd ~
# Download pre-release or latest from github
wget https://github.com/typecho/typecho/releases/download/v1.2.0-rc.1/typecho.zip
mv ~/typecho.zip /var/www/html/

cd /var/www/html/
unzip typecho.zip
rm typecho.zip

# php-fpm默认是www-data用户运行
chown -R www-data:www-data /var/www/html/*

Then, in the local browser, enter the server IP or domain name (if configured), and follow the prompts step by step.

Typecho主题

Although not as good as WordPress, Typecho still has many themes and plug-ins in https://typecho.me/ You can find many good themes and plug-ins.

Some of my favorite themes:

  • freewind
  • MWordStar
  • Joe
  • Facile
  • Cuckoo
  • Nexmoe
  • Bearsimple
  • Initial
  • Akina

reference resources

Configuring Nginx to Serve Your First Site Over HTTPS

https://linuxize.com/post/how-to-install-php-8-on-ubuntu-20-04/#installing-php-80-with-nginx

https://learnku.com/php/t/51997

https://juejin.cn/post/6969113070346403853

https://developer.aliyun.com/article/764024

In addition, many WordPress site building tutorials are also applicable.