當你看到這篇文章的時候,表明你已經有docker的基礎知識了,或者可以看上一篇文章 Docker 入門教程。
傳統的使用wordpress搭建網站,意味著你需要搭建以下四個環境:
- php;
- apache / nginx;
- mysql;
- wordpress;
這裡面主要是php的搭建真心麻煩,各種依賴,版本不相容,然後還有php跟mysql的外掛,我是吃了它很大的苦,尋求讓我快樂的方法,知道我發現了它。使用docker容器技術5分鐘快速搭建wordpress,相信我,真的是五分鐘。
一臺linux伺服器
你需要一臺linux或者ubuntu伺服器,阿里雲或者騰訊雲的都可以。
新建並啟動 MySQL 容器
$ docker container run \
-d \
--rm \
--name wordpressdb \
--env MYSQL_ROOT_PASSWORD=123456 \
--env MYSQL_DATABASE=wordpress \
mysql:5.7
上面的命令會基於 MySQL 的 image 檔案(5.7版本)新建一個容器。該命令的五個命令列引數的含義如下。
- --rm:停止執行後,自動刪除容器檔案。
- --name wordpress:容器的名字叫做wordpress。
- --env MYSQL_ROOT_PASSWORD=123456:向容器程序傳入一個環境變數MYSQL_ROOT_PASSWORD,該變數會被用作 MySQL 的根密碼。
- --env MYSQL_DATABASE=wordpress:向容器程序傳入一個環境變數MYSQL_DATABASE,容器裡面的 MySQL 會根據該變數建立一個同名數據庫(本例是WordPress)。
新建並啟動 WordPress 容器
基於官方的 WordPress image,新建並啟動 WordPress 容器。
docker container run \
-d \
-p 8080:80 \
--rm \
--name wordpress \
--env WORDPRESS_DB_PASSWORD=123456 \
--link wordpressdb:mysql \
--volume "$PWD/wordpress":/var/www/html \
wordpress
該命令執行完後,會在當前目錄下生成一個wordpress目錄。
-p 8080:80:將容器的 80 埠對映到當前物理機的8080埠。
--volume "$PWD/wordpress":/var/www/html:將容器的`/var/www/html`目錄對映到當前目錄的wordpress子目錄,操作$pwd/wordpress目錄,相當於操作容器裡面的/var/www/html目錄了。
這個時候你想瀏覽器輸入公網ip : 8080埠,其實是訪問不了的,有兩個原因:
- 有的人可能沒有這個問題,比如我用的是阿里雲的伺服器,8080埠預設沒有加入安全組,需要去阿里雲控制檯新建安全組,把8080埠加進去;
- 容器裡面的wordpress資料庫配置檔案沒有修改,這裡通過$pwd/wordpress對映,修改。
修改WordPress配置檔案
目前有兩個容器
[root@iZbp180j96p8y98l1s1oucZ ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f03c15abfe54 wordpress "docker-entrypoint.s…" 17 hours ago Up 17 hours 172.24.49.41:8080->80/tcp wordpress
f6274d57162a mysql:5.7 "docker-entrypoint.s…" 18 hours ago Up 18 hours 3306/tcp, 33060/tcp wordpressdb
進入wordpress容器,通過names名為wordpress,或者container id為f03c15abfe54,都可以進入容器,命令格式為:
[root@iZbp180j96p8y98l1s1oucZ ~]# docker exec -it f03c15abfe54 /bin/bash
root@f03c15abfe54:/var/www/html# cd wp-content/
root@f03c15abfe54:/var/www/html/wp-content# ll
bash: ll: command not found
通過exit命令即可退出當前容器環境,返回到linux命令列。
可以看到,在容器裡面操作及其不方便,由於前面將容器的/var/www/html目錄對映到當前目錄的wordpress子目錄,我們可以直接操作當前目錄下的wordpress目錄。
[root@iZbp180j96p8y98l1s1oucZ ~]# ll
total 4
drwxr-xr-x 5 33 tape 4096 May 27 12:35 wordpress
[root@iZbp180j96p8y98l1s1oucZ ~]# vim wordpress/wp-config.php
需要修改下面三個
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') );
/** MySQL database username */
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'root') );
/** MySQL database password */
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', '123456') );
然後瀏覽器輸入公網ip:8080,即可進去wordpress安裝介面,跟著嚮導,幾分鐘就完成了。
到這裡,如果不出意外的話,你的網站應該已經可以訪問了,接下來就是繫結到域名以及寫文章去豐富內容了。
如果想進入mysql容器,也是同樣類似的命令:
[root@iZbp180j96p8y98l1s1oucZ ~]# docker exec -it f6274d57162a /bin/bash
root@f6274d57162a:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 812
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit;
Bye
root@f6274d57162a:/# exit
exit