前言

主要讲解如何通过docker来搭建Typecho,希望能帮助到一部分人,快速的搭建自己的博客。

安装Docker

# 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

# 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 安装docker
yum install -y docker-ce

# 设置开机启动
systemctl enable docker

# 启动 Docker
systemctl start docker

# 查看版本
docker version

安装docker-compose

要下载并安装Compose standalone,并且运行
curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
添加软连
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

构建Typecho镜像

创建docker-compose.yml文件

version: "3"

services:
  nginx:
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    restart: always
    volumes:
      - ./typecho:/var/www/html
      - ./ssl:/var/www/ssl
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - php
    networks:
      - web
    extra_hosts:
      - ray.docker:172.17.0.1

  php:
    image: tsund/php:7.2.3-fpm
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - ./typecho:/var/www/html
    environment:
      - TZ=Asia/Shanghai
    depends_on:
      - mysql
    networks:
      - web

  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/logs:/var/log/mysql
      - ./mysql/conf:/etc/mysql/conf.d
    env_file:
      - mysql.env
    networks:
      - web

networks:
  web:

映射的路径改成自己服务器的路径
执行docker-compose up -d 拉取和构建镜像

配置nginx

server {
    listen       80;
    server_name  lcl101.cn;
    rewrite ^(.*) https://lcl101.cn$1 permanent;
}

server {
    listen 443 ssl;
    server_name  lcl101.cn;

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

    access_log /var/log/nginx/typecho_access.log main;

    ssl_certificate /var/www/ssl/7439521_lcl101.cn.pem;
    ssl_certificate_key /var/www/ssl/7439521_lcl101.cn.key;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

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

    location ~ .*\.php(\/.*)*$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    
}

初始化设置

2022-06-13T03:56:18.png

进入首页

2022-08-02T08:18:07.png

通过phpmyadmin来访问数据库

由于我们使用的是docker搭建的可以,所以phpmyadmin搭建需要使用docker内部网路。
使用docker network ls查看我们博客的网络名称
2022-08-02T08:19:55.png
搭建命令

docker run --name phpmyadmin -d --link mysql -e PMA_HOST="mysql" --net blog_typecho_web -p 6061:80 phpmyadmin/phpmyadmin

访问phpmyadmin

访问地址:http:[ip]:6061
2022-08-02T08:22:54.png
登录完成既可以看到你的数据库了
2022-08-02T08:24:05.png


Last modification:August 19, 2022
If you think my article is useful to you, please feel free to appreciate