因为准备帮人搭建个wp网站,想到docker更便捷且有相对隔离的环境,果断决定使用docker来快速部署。
既然使用docker部署,前提自然得有docker环境
docker快速部署wordpress准备工作
如果没有,也简单,两行搞定:
yum install -y docker
systemctl start docker
分别是安装,启动docker (linux centos 7+环境)
接着 拉取镜像:
docker pull wordpress
此操作拉取了最新版本,如果要指定版本,在wordpress后:版本号 即可
此外如果没有数据库环境,则需要在当前环境下安装数据库,或者之后连接远程数据库。
同样使用docker安装mysql或是mariadb即可,此文不再展开,建议安装mariadb,因为和5.7版mysql比,内存占用少,开销少了花费少。
docker安装wordpress
安装和操作LAMP相比简单太多,一行命令:
docker run --name Mywordpress -p 8888:80 -d wordpress
解释下:–name 后面 Mywordpress 是自定义的名称
-p 是端口映射,表示公网8888端口映射至容器80端口
-d 后台运行 wordpress,则是之前拉取的镜像名
这里使用的参数简单,当然启动参数还可以扩充
比如 --link mysqlXXX:mysql 表示数据库连接
操作完成,查看是否运行 使用docker -p。
如果列表有包含Mywordpress 的项目,则表明运行成功。
接着浏览器试试
http://xxxx:8888
回车发现并不能的打开 wordpress的安装页
那就 dockers logs MywordpressId 看看日志:
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name,
using 172.17.0.x. Set the ‘ServerName’ directive globally to suppress this message
apache 报错了
172.17.0.x 无疑是wp的容器ip了
在apache的默认配置文件内放开 ServerName IP 注释即可
具体的文件路径是:
/etc/apache2/sites-enabled/000-default.conf
因为容器没有修改命令,可安装,也可以
先把容器文件复制到宿主机
在宿主机修改此文件
在将文件复制到容器
比如我这wordpress的容器ID是 69123456789
docker cp -L 69:/etc/apache2/sites-enabled/000-default.conf /home/
修改的内容是:
放开 ServerName ,并修改后面的ip为容器ip
即
ServerName 172.17.0.x
完整文件内容:
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName 172.17.0.x
#SeverAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
修改后还原至容器内:
docker cp /home/000-default.conf 69:/etc/apache2/sites-enabled/000-default.conf
此时,再重启wordpress 容器:
docker restart CONTAINER_ID
查看运行日志,发现有报错:
Invalid command 'SeverAdmin', perhaps misspelled or defined
by a module not included in the server configuration
注释掉 上面文件中 SeverAdmin 内容
再次重启 OK,进入wordpress 安装页面。
不得不说,php的运行占用内存和java比,是真的少。启动时只有53M,下面的java项目启动都得400M内存占用。
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
69********** Mywordpress 0.01% 59.81MiB / 1.795GiB 3.25% 1.07MB / 516kB 4.87MB / 758kB
************ myxx 0.29% 458.5MiB / 1.795GiB 24.94% 633MB / 317MB 50.2MB / 20.5kB
************ mariadb 0.11% 121.5MiB / 1.795GiB 6.61% 233MB / 1.34GB 100MB / 407MB