一、使用 docker-compose 编排 lnmp(dockerfile) 完成 wordpress
环境准备
##修改主机名
hostnamectl set-hostname lyh
bash
[root@lyh ~]###关闭防火墙及核心防护
systemctl stop firewalld ##关闭核心防护
setenforce 0 ##关闭核心防护##安装Docker-20版本
yum install -y yum-utils device-mapper-persistent-data lvm2 ##下载依赖环境
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ##设置阿里云镜像源
yum install -y docker-ce-20.10.18 docker-ce-cli-20.10.18 containerd.io ##安装docker-20版本systemctl start docker.service ##开启docker服务
systemctl enable docker.service ##开启docker开机自启##下载centos:7镜像
docker pull centos:7 docker images
1.1、创建相关的文件夹
mkdir /opt/compose_lnmp/nginx -p
mkdir /opt/compose_lnmp/php -p
mkdir /opt/compose_lnmp/mysql -p
ls /opt/compose_lnmp/
mysql nginx php
1.2、docker-compose编排nginx
1.2.1在nginx下创建Dockerfile文件
vim Dockerfile ##编写Dockerfile文件FROM centos:7
MAINTAINER this is nginx image <lyh>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
1.2.2修改nginx.conf的配置文件
[root@lyh nginx]#ls
Dockerfile
[root@lyh nginx]#rz -E
rz waiting to receive.
[root@lyh nginx]#ls
Dockerfile nginx.conf
[root@lyh nginx]#vim nginx.conf
1.2.3上传相关压缩包
[root@lyh nginx]#ls
Dockerfile nginx.conf##上传相关压缩包
[root@lyh nginx]#ls
Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
1.2.4创建html目录
将wordpress-4.9.4-zh_CN.tar.gz压缩包解压到 html文件夹内
[root@lyh nginx]#ls
Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
[root@lyh nginx]#mkdir html
[root@lyh nginx]#cd html/
[root@lyh html]#ls
[root@lyh html]#tar xf ../wordpress-4.9.4-zh_CN.tar.gz -C /opt/compose_lnmp/nginx/html/
[root@lyh html]#ls
wordpress
[root@lyh html]#cd wordpress/
[root@lyh wordpress]#ls
index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php
readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
[root@lyh wordpress]#ls /opt/compose_lnmp/nginx/
Dockerfile html nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
1.3、docker-compose编排mysql
1.3.1准备压缩包
[root@lyh html]#pwd
/opt/compose_lnmp/nginx/html
[root@lyh html]#cd /opt/compose_lnmp/mysql/
[root@lyh mysql]#ls##上传相关文件及压缩包
mysql-boost-5.7.20.tar.gz
1.3.2编写Dockerfile文件
#vim DockerfileFROM centos:7
MAINTAINER this is mysql image <lyh1>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
1.3.3编写my.cnf文件
vim my.cnf[client]
port = 3306
socket=/usr/local/mysql/mysql.sock[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
1.4、docker-compose编排php
1.4.1准备php相关配置文件及压缩包
##上传相关压缩包及配置文件
[root@lyh php]#ls
php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
1.4.2编写Dockerfile文件
[root@docker php]# vim Dockerfile FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F
1.4.3修改php相关配置文件
1.4.3.1修改php.ini
该配置文件模板位于安装目录的php-7.1.10/php.ini-development位置
[root@lyh php]# vim php.ini#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@lyh php]# egrep -v "^;" php.ini | egrep -v "^$"
1.4.3.2修改php-fpm.conf
该配置文件模板位于/usr/local/php/etc/php-fpm.conf.default
[root@lyh php]# vim php-fpm.conf #17行,删除注释符号“;”
pid = run/php-fpm.pid[root@lyh php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
1.4.3.3修改www.conf
该配置文件模板位于usr/local/php/etc/php-fpm.d/www.conf.default
[root@lyh php]# vim www.conf #23、24行,修改用户和组
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.19.0.30:9000
[root@lyh php]# egrep -v "^;" www.conf | egrep -v "^$"
1.5、安装docker-compose
1.5.1下载或上传docker-compose
[root@lyh opt]#pwd
/opt
[root@lyh opt]#ls
compose_lnmp containerd rh
[root@lyh opt]#rz -E ##上传Docker-compose文件
rz waiting to receive.
[root@lyh opt]#ls
compose_lnmp containerd docker-compose rh
1.5.2给docker-compose添加权限
[root@lyh opt]#ls
compose_lnmp containerd docker-compose rh
[root@lyh opt]#chmod +x docker-compose
[root@lyh opt]#ls
compose_lnmp containerd docker-compose rh
1.5.3将docker-compose移动到/usr/local/bin路径下
[root@lyh opt]#ls
compose_lnmp containerd docker-compose rh
[root@lyh opt]#mv docker-compose /usr/local/bin/
[root@lyh opt]#ls
compose_lnmp containerd rh
[root@lyh opt]#ls /usr/local/bin/
docker-compose
1.5.4查看docker-compose的版本
[root@lyh opt]#ls /usr/local/bin/
docker-compose
[root@lyh opt]#docker-compose --version
docker-compose version 1.21.1, build 5a3f1a3
1.6、编写docker-compose.yml文件
如果粘贴,vim编辑器请输入 :set paste
version: '3'
services:nginx:container_name: nginx #指定容器的名称为nginx hostname: nginx #容器的主机名为nginxbuild: #构建上下文和Dockerfile的位置context: /opt/compose_lnmp/nginx/dockerfile: Dockerfileports:- 80:80- 443:443 #将宿主机的80端口映射到容器的80端口,将宿主机的443端口映射到容器的443端口#设置数据卷挂载volumes:- /opt/compose_lnmp/nginx/html:/usr/local/nginx/htmlnetworks:lnmp:ipv4_address: 172.18.0.10mysql:container_name: mysqlbuild:context: /opt/compose_lnmp/mysqldockerfile: Dockerfileports:- 3306:3306networks:lnmp:ipv4_address: 172.18.0.22
volumes:- db-data:/usr/local/mysqlprivileged: truephp:container_name: phpbuild:context: /opt/compose_lnmp/phpdockerfile: Dockerfileports:- 9000:9000networks:lnmp:ipv4_address: 172.18.0.33volumes:- db-data:/usr/local/mysql- ./nginx/html:/usr/local/nginx/htmldepends_on:- nginx- mysqlnetworks:lnmp:driver: bridgeipam:config:- subnet: 172.18.0.0/16volumes:db-data:
1.7、启动服务
##在当前路径下运行下行命令
docker-compose -f docker-compose.yml up -d
1.7.1报错
ittp://mirrors.ustc.edu.cn/centos/7.9.2009/os/x86 64/repodata/repomd.xml: [Errno 14l curl#6"Could not resolve host: mirrors,ustc.edu.cn: Unknown error.rying other mirror.##大概意思就是网络的问题,需要修改DNS以及添加路由
##修改网络及域名
vim /etc/sysconfig/network-scripts/ifcfg-ens33 DNS2=114.114.114.114##添加路由
vim /etc/sysctl.confnet.ipv4.ip_forward=1##退出后,生效路由
sysct -p##然后加载、重启网络及Docker服务systemctl daemon-reload
systemctl restart network
systemctl restart docker.service
1.8、进入容器登录mysql授权
[root@lyh php]#docker exec -it mysql /bin/bash
[root@28af5fe9d022 mysql-5.7.20]# mysql
mysql> mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
1.9、创建一个新的配置文件 wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');/** MySQL数据库密码 */define('DB_PASSWORD', 'Abc123');
1.10、验证结果
##在浏览器输入 192.168.10.100/wordpress/index.php