個人博客 https://savokiss.com 用的是 typecho,一直使用的是 阿里雲ECS 直接安裝的 MySQL 和 PHP,因爲買的時間比較早,當時用的是 CentOS 6.5。後來想玩 docker,發現 docker 只支持 CentOS 7+,加上以前的系統上東西太亂了,因此此次有時間就將數據庫和 typecho 源碼備份了一下,而後換了一個純淨的 CentOS 7.6 的鏡像。因爲我買的 ECS 是 1CPU 1GB 內存,以前還一直擔憂跑不起來 docker,此次升級完以後發現徹底沒問題,內存用了一半都不到哈~因而記錄下這個過程,說不定能夠幫到其餘小夥伴。php
升級系統,建議備份好數據,因爲個人 ECS 上面主要就一個博客,因此直接用全新的鏡像,安裝完後啥都木有,固然也能夠直接將快照建立爲自定義鏡像,而後升級系統的時候選擇便可該快照便可。
本人也是 docker 小白,若是對 docker 不熟悉,能夠先看筆者的另外一篇文章:html
下面開搞:mysql
若是想要用非 root 用戶執行 docker 命令,請參考此步驟。
新增的用戶名叫 savokiss。
下面的命令使用 root 執行。
useradd savokiss
passwd savokiss
visudo
找到下面兩行,將新用戶寫入,如:linux
## Allow root to run any commands anywhere root ALL=(ALL) ALL savokiss ALL=(ALL) ALL
groupadd docker
usermod -aG docker savokiss
讓變動生效:nginx
newgrp docker
而後用戶 savokiss 執行 docker 命令時就不用加 sudo 了git
如下命令均以 root 用戶執行
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum makecache fast yum install docker-ce
systemctl enable docker systemctl start docker docker run hello-world
hello-world 是官方的測試鏡像。github
若是啓動失敗的話能夠更換爲 daocloud 的 docker 源,而後從新 run:sql
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
也能夠直接修改 /etc/docker/daemon.json
中的 registry(文件不存在就新建一個)docker
{ "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.一、mysql@5.七、php@7.2 三個鏡像,若有須要能夠自行修改版本號。
docker pull mysql:5.7
建立容器 main_mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name main_mysql mysql:5.7
參數說明:
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,說明映射目錄成功啦~
因爲 typecho 須要使用 mysql pdo。再次用上面的命令進入 main_phpfpm 容器,而後執行:
docker-php-ext-install pdo_mysql
而後執行 php -m
就能夠看到已經安裝的擴展
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
目錄下,
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; }
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;
cert
文件夾,將 https 證書 perm.pem
, perm.key
放進去若是不須要 https,上面的配置文件就不須要後面這兩步,同時 conf 文件內容適當刪減便可。
注意:上面的配置文件中的路徑都是對於容器內部來講的。提示:若是啓動失敗,可使用
docker logs main_nginx
查看錯誤日誌,啓動成功後是能夠進入到容器內部的。修改配置文件後可能須要進入容器內部執行nginx -s reload
,或者直接在宿主機docker restart main_nginx
便可。
因爲筆者是遷移,typecho 源碼都在 github 上,因此直接 git clone 到 /home/savokiss/www/
中便可。而後用工具鏈接數據庫將 sql 導入就完成啦。
本文主要參考了文末的第一篇文章,主要是爲了記錄折騰的過程,固然目前搭建完成還有幾個問題能夠優化,如:
第一條等遇到了再考慮一下,第二條能夠經過 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
歡迎關注個人公衆號:碼力全開