CentOS 7 使用 docker 安裝 typecho 博客系統

前言

我的博客 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/

配置容器

這裏主要獲取 [email protected][email protected][email protected] 三個鏡像,如有需要可以自行修改版本號。

獲取 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

參數說明:

  • -d 代表 daemon,即後臺運行
  • -p 是設置宿主機和容器的端口號
  • -e 設置 mysql 密碼
  • --name 給當前 container 起一個名字

獲取 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 

參數說明:

  • -v 代表 volumes,即掛載宿主機和容器的目錄映射
  • --link 代表鏈接外部 container,本例中即 main_mysql

測試目錄映射

先進到容器內部:

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

歡迎關注我的公衆號:碼力全開
codingonfire.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章