docker安装blog

nginx反代和证书

使用neilpang/nginx-proxy:alpine镜像,内含自动反代和自动生成证书

version: '2'
services:
  nginx:
    image: neilpang/nginx-proxy:alpine
    ports:
      - 80:80
      - 443:443
    volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
        - ./proxy/certs:/etc/nginx/certs
        - ./proxy/acme:/acmecerts
        - ./proxy/conf.d:/etc/nginx/conf.d
        - ./proxy/vhost.d:/etc/nginx/vhost.d
        - ./proxy/stream.d:/etc/nginx/stream.d
        - ./proxy/dhparam:/etc/nginx/dhparam
    network_mode: "host"

开启nginx_proxy容器,之后的所有容器只需要加入-e VIRTUAL_HOST=domain.cn -e ENABLE_ACME="true",就可以自动配置反代和https,当容器时时多端口时需要指定容器端口-e VIRTUAL_POST=port,不然会默认80端口

Dockerfile撰写

blog搭建环境为 nginx+php-fpm+sqlite3(基于alpine linux)
nginx可以直接拉取镜像FROM nginx:alpine
php-fpm+sqlite3可以搭建在一起,命名为php-sqlite

FROM php:7.2-fpm-alpine

LABEL Author="m0yuqi <m0yuqi@126.com>" Blog="http://www.m0yuqi.cn"

RUN sed -i "s/http:\/\/dl-cdn.alpinelinux.org/http:\/\/mirrors.aliyun.com/g" /etc/apk/repositories && \
    apk -U upgrade && \
    apk add --no-cache php7-pdo_sqlite \
    sqlite \
    php7-sqlite3 \
    php7-tokenizer \
    php7-ctype && \
    mkdir /www && \
    chmod -R 777 /www

nginx配置

实现支持php,顶级域名跳转到www和url重写

server {
    server_name domain.cn;
    return 301 $scheme://www.domain.cn$request_uri;
}
server {
        listen      80 default_server;
        root            /www;

        charset utf-8;

        location / {
                index index.php index.html index.htm;
                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php$1 last;
                }
        }
        
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        location ~ .*\.php(\/.*)*$ {
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_pass  php:9000;
        }
}

docker-compose.yml构建

联合nginx和php-sqlite镜像

version: "2"
services:

  nginx:
    image: nginx:alpine
    volumes:
      - ./conf.d/:/etc/nginx/conf.d/
    volumes_from:
      - php
    links:
      - php
    environment:
      VIRTUAL_HOST: domain.cn
      ENABLE_ACME: "true"
  php:
    build: ./
    image: m0yuqi/php-sqlite
    volumes:
      - ./www:/www
      - ./php.ini:/etc/php7/php.ini

运行docker-compose up --build -d

安装typecho

下载typecho源码

cd www
git clone https://github.com/typecho/typecho.git
mv typecho/* ./ && rm -rf typecho && rm -rf tools

然后访问https://ip即可安装
若数据库连接失败,需将安装数据库的文件夹权限设置为755

Edit with markdown