将Typecho迁移至docker容器

发布于 2021-12-10  179 次阅读


迁移自己的第一个博客,感觉有点怪怪的。

备份webroot和数据库

备份操作跟wordpress是完全一样的,按照这篇就可以。

Dockerfile及docker-compose

官方不提供镜像,所以得自己写Dockerfile了。

我的Dockerfile:

FROM php:7.4-fpm

RUN apt-get update || : && apt-get install git unzip wget libzip-dev -y
RUN docker-php-ext-install  mysqli pdo pdo_mysql bcmath zip


RUN set -e

RUN cd ~

#RUN wget http://typecho.org/build.tar.gz -O typecho.tgz
#RUN tar zxvf typecho.tgz
#RUN mv build/* /var/www/html
#RUN rm -f typecho.tgz

RUN chown -R www-data:www-data /var/www/html

CMD ["php-fpm"]

因为是迁移,就不下载源码了,对应的代码就注释掉了,直接把旧的webroot拖过来就可以。

我的docker-compose.yml:

version: "3"

services:
  typecho:
    build: .
    volumes:
      - ./webroot:/var/www/html
    ports:
      - "127.0.0.1:3030:9000"
    container_name: typecho
    restart: always
    networks:
      - default
    stdin_open: true
    tty: true
    depends_on:
      - db

  db:
    image: mariadb
    container_name: typecho_db
    networks:
      - default
    volumes:
      - ./mariadb:/var/lib/mysql
      - ./dump.sql:/dump.sql
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: xxxxxxxxxxxxxxx
      MARIADB_DATABASE: typecho
      MARIADB_USER: typecho
      MARIADB_PASSWORD: xxxxxxxxxxxxxxx

跟wordpress的情况差不多,恢复备份的时候先启动db然后恢复数据库,然后把webroot拖过来再启动php-fpm。

如果改了配置,比如数据库host的地址,可以在config.inc.php修改设置。

Nginx反代

server {
  listen 80;
  listen [::]:80;
  server_name blog.stsecurity.moe;

  access_log /var/log/nginx/blog-access.log;
  error_log /var/log/nginx/blog-error.log;

  root /typecho/webroot;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name blog.stsecurity.moe;

  access_log /var/log/nginx/blog-access.log;
  error_log /var/log/nginx/blog-error.log;

  # Uncomment these lines once you acquire a certificate:
  ssl_certificate     /etc/nginx/ssl/blogfullchain.cer;
  ssl_certificate_key /etc/nginx/ssl/blogkey.key;

  root /typecho/webroot;

  index index.php;

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

    location ~ \.(php)$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass localhost:3030;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param PATH_TRANSLATED /var/www/html$fastcgi_path_info;
      include fastcgi_params;
      fastcgi_read_timeout 3000;
      proxy_connect_timeout 3000s;
      proxy_send_timeout   3000;
      proxy_read_timeout   3000;
    }

}

这里也跟wordpress差不多了,唯一不一样的地方在于根目录的跳转机制,最后要用/index.php$is_args$args,不然会出现无法登陆控制台,尝试登陆时无限跳回登陆页的问题。


Sup