WordPress是使用PHP語言開發的博客平台,用戶可以在支持PHP和MySQL數據庫的服務器上架設屬於自己的網站。也可以把 WordPress當作一個內容管理系統(CMS)來使用。
WordPress是一款個人博客系統,並逐步演化成一款內容管理系統軟件,它是使用PHP語言和MySQL數據庫開發的。用戶可以在支持 PHP 和 MySQL數據庫的服務器上使用自己的博客。
WordPress有許多第三方開發的免費模板,安裝方式簡單易用。不過要做一個自己的模板,則需要你有一定的專業知識。比如你至少要懂的標准通用標記語言下的一個應用HTML代碼、CSS、PHP等相關知識。
基於docker構建wordpress博客網站平台
1.自定義網絡
# 創建docker網絡lnmp [root@VM_0_10_centos bin]# docker network create lnmp 3f84590bd82650c47405da35dc0d41700c1b35215fd57d5d7097aeed0387cbbc # 查看docker網絡信息(默認只顯示前12位id) [root@VM_0_10_centos bin]# docker network ls NETWORK ID NAME DRIVER SCOPE 88010dbd06c0 bridge bridge local 221f15f2552e host host local 3f84590bd826 lnmp bridge local ebe64efe6b83 none null local
2.創建mysql數據容器
# 先從倉庫拉取mysql的鏡像 [root@VM_0_10_centos ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql d599a449871e: Pull complete f287049d3170: Pull complete 08947732a1b0: Pull complete 96f3056887f2: Pull complete 871f7f65f017: Pull complete 1dd50c4b99cb: Pull complete 5bcbdf508448: Pull complete a59dcbc3daa2: Pull complete 13e6809ab808: Pull complete 2148d51b084d: Pull complete 93982f7293d7: Pull complete e736330a6d9c: Pull complete Digest: sha256:c93ba1bafd65888947f5cd8bd45deb7b996885ec2a16c574c530c389335e9169 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest # 查看鏡像 [root@VM_0_10_centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest d435eee2caa5 10 days ago 456MB # 運行mysql容器 -itd以守護進程運行在后台 --network 指定mysql容器的網絡 --mount指定了數據卷 -e設置mysql密碼 -p 3307對外暴露的端口,3306為容器內部的端口 [root@VM_0_10_centos ~]# docker run -itd --name lnmp_mysql --network lnmp -p 3307:3306 --mount src=mysql-vol,dst=/var/lib/ mysql -e MYSQL_ROOT_PASSWORD=mysql密碼 mysql --character-set-server=utf89b7666d5089713fc135c4a695be1725c17a22bf826e46eafe2c52151fdf20948 # 查看數據卷 [root@VM_0_10_centos ~]# docker volume ls DRIVER VOLUME NAME local 6d34aaf134e3298590eab44809adf751d0ca17856bafe57135cf3295230dab6d local d18337c811e3cc0ec89552243255bea8b662e18a0b7ecb2a0f95073aceaac702 local mysql-vol # mysql數據均放在這個數據卷目錄下 [root@VM_0_10_centos ~]# ls /var/lib/docker/volumes/mysql-vol/_data/ # 查看mysql日志 [root@VM_0_10_centos ~]# docker logs lnmp_mysql # 查看mysql容器的進程 [root@VM_0_10_centos ~]# docker top lnmp_mysql
3.創建需要的數據庫
# 進入mysql容器創建數據庫 [root@VM_0_10_centos ~]# docker exec -it lnmp_mysql bash root@9b7666d50897:/# mysql -uroot -p # 回車,輸入密碼 # 創建wordpress數據庫wp mysql> create database wp;
或直接創建
# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database wp"'
退出容器,查看mysql容器的ip地址,通過本地登錄mysql容器
# 查看容器ip
[root@VM_0_10_centos ~]# docker inspect -f '{{.NetworkSettings.Networks.lnmp.IPAddress}}' lnmp_mysql
lnmp_mysql
172.18.0.2
# 本地訪問mysql容器 [root@VM_0_10_centos ~]# mysql -h172.18.0.2 -uroot -p Enter password: # 出現報錯 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_p assword.so: cannot open shared object file: No such file or directory
可能是版本問題
[root@VM_0_10_centos ~]# docker exec -it lnmp_mysql bash root@9b7666d50897:/# mysql -uroot -p Enter password: Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> alter user 'root'@'%' identified with mysql_native_password by 'root'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) # 退出容器,本地登錄mysql容器 [root@VM_0_10_centos ~]# mysql -h172.18.0.2 -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wp | +--------------------+ 5 rows in set (0.00 sec)
4.創建PHP環境容器
# 先搜索是否有這個鏡像 # docker search nginx-php-fpm # 拉取鏡像 [root@VM_0_10_centos ~]# docker pull richarvey/nginx-php-fpm
# 運行鏡像==》容器# 先創建/app/wwwroot目錄,不然運行文件指定的目錄不存在會報錯
[root@VM_0_10_centos ~]# mkdir -p /app/wwwroot
# --network lnmp 需要和鏈接的mysql容器的網絡是同一個,不然不能連接mysql哈
[root@VM_0_10_centos wordpress]# docker run -itd --name lnmp_nginxphpfpm -p 88:80 --network lnmp --link lnmp_mysql:mysql --mount type=bind,src=/app/wwwroot,dst=/var/www/html richarvey/nginx-php-fpm
# 創建index文件 [root@VM_0_10_centos ~]# vi /app/wwwroot/index.html <h1>Hello World!!!</h1>
# 這時通過瀏覽器http://ip:88會出現剛剛index.html文件中的內容
5.搭建wordpress博客系統
# 切換到源碼安裝的目錄 [root@VM_0_10_centos ~]# cd /usr/local/src/ # 下載wordpress(好像瀏覽器請求報錯429,后面我是通過朋友幫忙下載的,然后上傳到服務器/usr/local/src目錄下。也不知道原因為什么我這邊下載不了)
https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
# 將wordpress解壓到/app/wwwroot/目錄下
[root@VM_0_10_centos src]# pwd
/usr/local/src
[root@VM_0_10_centos src]# tar -zxvf wordpress-4.7.4-zh_CN.tar.gz -C /app/wwwroot/
[root@VM_0_10_centos src]# ls /app/wwwroot/
index.html wordpress
通過瀏覽器訪問:http://ip:88/wprdpress即可
點擊現在就開始,進行數據庫配置
PS:在這里出現了數據庫連接錯誤,經過排查是在潤興wordpress容器時沒有與mysql數據庫進行連接導致。上面潤興容器代碼已更改。數據庫配置完成后,會在/app/wwwroot/wordpress/目錄生成一個wp-config.php配置文件
點擊進行安裝,填寫wordpress信息
點擊登錄,使用剛剛創建的用戶名和密碼進行登錄
登錄之后進入主頁
至此,部署完成。