CentOS 7 使用 docker 安装 typecho 博客系统
in 码农技术宅 with 4 comments

CentOS 7 使用 docker 安装 typecho 博客系统

in 码农技术宅 with 4 comments

前言

我的博客 https://savokiss.com 用的是 typecho,一直使用的是 阿里云ECS 直接安装的 MySQL 和 PHP,由于买的时间比较早,当时用的是 CentOS 6.5。后来想玩 docker,发现 docker 只支持 CentOS 7+,加上之前的系统上东西太乱了,所以这次有时间就将数据库和 typecho 源码备份了一下,然后换了一个纯净的 CentOS 7.6 的镜像。由于我买的 ECS 是 1CPU 1GB 内存,之前还一直担心跑不起来 docker,这次升级完之后发现完全没问题,内存用了一半都不到哈~于是记录下这个过程,说不定可以帮到其他小伙伴。

升级系统,建议备份好数据,由于我的 ECS 上面主要就一个博客,所以直接用全新的镜像,安装完后啥都木有,当然也可以直接将快照创建为自定义镜像,然后升级系统的时候选择即可该快照即可。

本人也是 docker 小白,如果对 docker 不熟悉,可以先看笔者的另一篇文章:

写给前端工程师的 docker 入门

下面开搞:

配置新用户

如果想要用非 root 用户执行 docker 命令,请参考此步骤。

新增的用户名叫 savokiss。
下面的命令使用 root 执行。

添加用户

useradd savokiss

修改密码

passwd savokiss

加入 sudo 权限

visudo

找到下面两行,将新用户写入,如:

## Allow root to run any commands anywhere
root ALL=(ALL) ALL
savokiss ALL=(ALL) ALL

创建 docker 用户组

groupadd docker

将新用户加入 docker 组

usermod -aG docker savokiss

让变更生效:

newgrp docker

然后用户 savokiss 执行 docker 命令时就不用加 sudo 了

安装 docker

以下命令均以 root 用户执行

下载安装依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

添加国内 yum 源

yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

更新缓存,安装 docker-ce

yum makecache fast
yum install docker-ce

启动 docker

systemctl enable docker
systemctl start docker

docker run hello-world

hello-world 是官方的测试镜像。

国内镜像源加速

如果启动失败的话可以更换为 daocloud 的 docker 源,然后重新 run:

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

也可以直接修改 /etc/docker/daemon.json 中的 registry(文件不存在就新建一个)

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}

修改后重启服务

systemctl daemon-reload
systemctl restart docker

如果能看到以下界面,就说明 hello world 成功咯:

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

配置容器

这里主要获取 nginx@1.16.1、mysql@5.7、php@7.2 三个镜像,如有需要可以自行修改版本号。

获取 MySQL 镜像

docker pull mysql:5.7

创建容器 main_mysql

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name main_mysql mysql:5.7

参数说明:

获取 PHP 镜像

docker pull php:7.2-fpm

创建容器 main_phpfpm

docker run -d -v /home/savokiss/www:/var/www/html -p 9000:9000 --link main_mysql:mysql --name main_phpfpm php:7.2-fpm 

参数说明:

测试目录映射

先进到容器内部:

docker exec -it main_phpfpm /bin/bash

这句话简单来说就是将容器中的 /bin/bash 连接到你当前的命令行,相当于进入容器中执行命令。

执行完后会进入容器的 /var/www/html
然后来创建个文件:

touch test.php
exit

然后在宿主机中的 /home/savokiss/www 目录下就会发现一个 test.php,说明映射目录成功啦~

PHP 扩展安装

由于 typecho 需要使用 mysql pdo。再次用上面的命令进入 main_phpfpm 容器,然后执行:

docker-php-ext-install pdo_mysql

然后执行 php -m 就可以看到已经安装的扩展

获取 nginx 镜像

docker pull nginx:1.16.1

创建 nginx 容器

docker run -d -p 80:80 -p 443:443 --name main_nginx -v /home/savokiss/www:/var/www/html -v /home/savokiss/conf/nginx:/etc/nginx/conf.d --link main_phpfpm:phpfpm --name main_nginx nginx:1.16.1

这里由于网站配置了 https,所以需要打开 443 端口,并且除了挂载网站目录,也将 nginx 的 conf.d 目录挂载到了宿主机

然后到 /home/savokiss/conf/nginx 目录下,

  1. 新建 savokiss.com.conf 文件,内容如下,已经开启了 https 和伪静态:
server {
    listen 443 ssl http2 reuseport;
    server_name savokiss.com www.savokiss.com;
    root /var/www/html/savokiss.com;
    index index.php;
    include /etc/nginx/conf.d/ssl.config;

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

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

    location ~ .*\.php(\/.*)*$ {
        include        fastcgi_params;
        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;
        fastcgi_index  index.php;

        fastcgi_pass   phpfpm:9000;
    }
}
server {
    listen       80;
    server_name  savokiss.com  www.savokiss.com;
    rewrite ^(.*) https://savokiss.com$1 permanent;
}
  1. 新建 ssl.config 文件
    ssl_certificate /etc/nginx/conf.d/cert/perm.pem;
    ssl_certificate_key /etc/nginx/conf.d/cert/perm.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;
  1. 创建 cert 文件夹,将 https 证书 perm.pem, perm.key 放进去

如果不需要 https,上面的配置文件就不需要后面这两步,同时 conf 文件内容适当删减即可。

注意:上面的配置文件中的路径都是对于容器内部来说的。

提示:如果启动失败,可以使用 docker logs main_nginx 查看错误日志,启动成功后是可以进入到容器内部的。修改配置文件后可能需要进入容器内部执行 nginx -s reload,或者直接在宿主机 docker restart main_nginx 即可。

部署 typecho

由于笔者是迁移,typecho 源码都在 github 上,所以直接 git clone 到 /home/savokiss/www/ 中即可。然后用工具连接数据库将 sql 导入就完成啦。

后记

本文主要参考了文末的第一篇文章,主要是为了记录折腾的过程,当然目前搭建完成还有几个问题可以优化,如:

  1. 多个网站是否应使用同一个 nginx 容器
  2. 容器如何在挂掉后自动重启
  3. 换成 docker-compose 编排会不会更好

第一条等遇到了再考虑一下,第二条可以通过 run 命令指定 --restart 即可。
第三条其实可以另写一篇文章了~

参考文章

CentOS 7 使用 docker 搭建基本的 lnmp 环境
Manage Docker as a non-root user
Get Docker Engine - Community for CentOS
DaoClound 镜像站
MySQL Image
PHP Image
Nginx Image

Responses / Cancel Reply
  1. 腾讯云加速:
    ```json
    {
    "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com"
    ]
    }
    ```

    Reply
  2. 你好,请问照你的方法部署上去之后,访问域名提示502是什么原因呢?我尝试过开启防火墙9000端口,仍然无效...

    Reply
    1. @青禾

      赞~

      Reply
    2. @青禾

      已经解决。

      Reply
京ICP备15030655号-1