使用docker-compose编排lnmp(dockerfile)完成wordpress

文章目录

使用docker-compose编排lnmp(dockerfile)完成wordpress

1、服务器环境

容器 操作系统 IP地址 主要软件
nginx CentOS 7.9 x86_64 172.20.0.10 Docker-Nginx
mysql CentOS 7.9 x86_64 172.20.0.20 Docker-Mysql
php CentOS 7.9 x86_64 172.20.0.30 Docker-php

2、Docker、Docker-Compose环境安装

bash 复制代码
[root@liuyanfen13 ~]#systemctl disable --now firewalld
[root@liuyanfen13 ~]#setenforce 0
#关闭防火墙和核心防护
2.1 安装Docker环境
bash 复制代码
[root@liuyanfen13 ~]#yum install -y yum-utils device-mapper-persistent-data lvm2
#安装依赖包
[root@liuyanfen13 ~]#yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#设置阿里云镜像源
[root@liuyanfen13 ~]#yum install -y docker-ce docker-ce-cli containerd.io
#安装Docker-CE(社区版)
[root@liuyanfen13 ~]#systemctl enable --now docker.service
#开机自启并立即启动
[root@liuyanfen13 ~]#systemctl status docker.service
#查看状态
2.2 安装Docker-Compose
bash 复制代码
[root@liuyanfen13 compose_lnmp]#curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#下载安装
[root@liuyanfen13 compose_lnmp]#chmod +x /usr/local/bin/docker-compose
#添加权限
  • 需要的文件和安装包
bash 复制代码
[root@liuyanfen13 compose_lnmp]#pwd
/opt/compose_lnmp
[root@liuyanfen13 compose_lnmp]#ls
docker-compose.yml  mysql  nginx  php
[root@liuyanfen13 compose_lnmp]#cd mysql/
[root@liuyanfen13 mysql]#ls
Dockerfile  my.cnf  mysql-boost-5.7.20.tar.gz
[root@liuyanfen13 mysql]#cd ../nginx/
[root@liuyanfen13 nginx]#ls
Dockerfile  html  nginx-1.12.0.tar.gz  nginx.conf  wordpress-4.9.4-zh_CN.tar.gz
[root@liuyanfen13 nginx]#cd ../php/
[root@liuyanfen13 php]#ls
Dockerfile  php-7.1.10.tar.bz2  php-fpm.conf  php.ini  www.conf

3、nginx

3.1 新建目录,上传安装包
bash 复制代码
[root@liuyanfen13 opt]#mkdir /opt/compose_lnmp
[root@liuyanfen13 opt]#cd compose_lnmp/
[root@liuyanfen13 compose_lnmp]#ll
总用量 0
[root@liuyanfen13 compose_lnmp]#mkdir nginx mysql php
[root@liuyanfen13 compose_lnmp]#ll
总用量 0
drwxr-xr-x. 2 root root 6 5月   3 09:48 mysql
drwxr-xr-x. 2 root root 6 5月   3 09:48 nginx
drwxr-xr-x. 2 root root 6 5月   3 09:48 php

#上传nginx安装包nginx-1.12.0.tar.gz
#上传wordpress服务包wordpress-4.9.4-zh_CN.tar.gz
[root@liuyanfen13 nginx]#ls
nginx-1.12.0.tar.gz  nginx.conf  wordpress-4.9.4-zh_CN.tar.gz
3.2 编辑Dockerfile脚本
bash 复制代码
#编辑dockerfile脚本
[root@liuyanfen13 nginx]#vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <lnmp>
RUN yum clean all
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN 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
VOLUME ["/usr/local/nginx/html"]
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
3.3 准备nginx.conf配置文件
bash 复制代码
[root@liuyanfen13 nginx]#egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
#查看nginx.conf配置文件内容
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.20.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
3.4 论坛服务
bash 复制代码
[root@liuyanfen13 compose_lnmp]#cd nginx/
[root@liuyanfen13 nginx]#ls
Dockerfile  html  nginx-1.12.0.tar.gz  nginx.conf  wordpress-4.9.4-zh_CN.tar.gz
[root@liuyanfen13 nginx]#tar xf wordpress-4.9.4-zh_CN.tar.gz -C /opt/compose_lnmp/nginx/html/
#将wordpress解压到共享目录下
[root@liuyanfen13 nginx]#ll html/
总用量 8
-rw-r--r--. 1 root   root         5 5月   3 16:44 index.html
drwxr-xr-x. 5 nobody nfsnobody 4096 2月   8 2018 wordpress

4、MySQL

4.1 上传安装包
bash 复制代码
[root@liuyanfen13 nginx]#cd ../mysql/
[root@liuyanfen13 mysql]#ls
[root@liuyanfen13 mysql]#ls
mysql-boost-5.7.20.tar.gz
#在mysql目录下上传mysql安装包
4.2 编辑Dockerfile脚本
bash 复制代码
#编辑dockerfile脚本
[root@liuyanfen13 mysql]#vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum clean all
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 -j4 && 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
VOLUME ["/usr/local/mysql"]
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
4.3 准备my.cnf配置文件
bash 复制代码
#编辑mysql配置文件
[root@liuyanfen13 mysql]#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 = 1

sql_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

5、PHP

5.1 上传安装包
bash 复制代码
[root@liuyanfen13 mysql]#cd ../php/
[root@liuyanfen13 php]#ls
[root@liuyanfen13 php]#ls
php-7.1.10.tar.bz2  php-fpm.conf  php.ini  www.conf
#在php目录下上传php安装包
5.2 编辑dockerfile脚本
bash 复制代码
[root@liuyanfen13 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 -j4 ; 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
5.3 准备php配置文件
bash 复制代码
vim php.ini
date.timezone = Asia/Shanghai
#939行,取消注释,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
#1170行,修改
bash 复制代码
#修改配置文件
vim php-fpm.conf 
pid = run/php-fpm.pid
#17行,删除注释符号";"

#查看配置文件生效内容
[root@liuyanfen13 php]#egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
bash 复制代码
#修改配置文件
vim www.conf
user = nginx
group = nginx
#23、24行,修改用户和组
listen = 172.18.0.30:9000
#36行,修改监听IP和端口为容器IP:9000端口
listen.allowed_clients = 127.0.0.1,172.18.0.10
#62行,修改客户端地址

#查看配置文件生效的内容
[root@liuyanfen13 php]#egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.20.0.30:9000
listen.allowed_clients = 127.0.0.1,172.20.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

6、编辑docker-compose.yml配置文件

bash 复制代码
#编辑docker-compose.yml配置文件
vim docker-compose.yml
version: "2"
services:
  nginx:
    container_name: nginx
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 80:80
    networks:
      vae:
        ipv4_address: 172.20.0.10
    volumes:
      - ./nginx/nginx.conf:/usr/local/nginx/nginx.conf
      - ./nginx/html:/usr/local/nginx/html
      #共享目录(将宿主机的目录挂载到容器)
  mysql:
    container_name: mysql
    hostname: mysql
    build:
      context: ./mysql
      dockerfile: Dockerfile
    ports:
      - 3306:3306
    networks:
      vae:
        ipv4_address: 172.20.0.20
    volumes:
      - /usr/local/mysql
      #共享目录
  php:
    container_name: php
    hostname: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - 9000:9000
    networks:
      vae:
        ipv4_address: 172.20.0.30
    volumes_from:
    #数据卷
      - nginx
      - mysql
    depends_on:
    #依赖关系,php容器在nginx、mysql容器之后启动
      - nginx
      - mysql
networks:
#设置自定义网络
  vae:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

7、生成镜像容器

bash 复制代码
[root@liuyanfen13 compose_lnmp]#docker-compose -f docker-compose.yml up -d
#生成镜像容器
[root@liuyanfen13 compose_lnmp]#docker images
#显示所有镜像
[root@liuyanfen13 compose_lnmp]#docker ps -a
#显示所有容器

8、验证登录

bash 复制代码
http://192.168.10.13/wordpress/index.php
#浏览器访问验证

9、启动wordpress服务

9.1 mysql授权
bash 复制代码
[root@liuyanfen13 nginx]#docker exec -it mysql bash
#进入mysql容器
[root@mysql mysql-5.7.20]# mysql -u root -p
#登录mysql
Enter password:
#初始密码为空,直接回车
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> create database wordpress;
Query OK, 1 row affected (0.00 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 '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
#授权
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#刷新
9.2 访问登录
  • 修改wp-config.php配置文件
bash 复制代码
[root@liuyanfen13 compose_lnmp]#docker exec -it nginx bash
#进入nginx容器
[root@nginx nginx-1.12.0]# cd /usr/local/nginx/html/
#切换到挂载目录
[root@nginx html]# cd wordpress/
[root@nginx 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@nginx wordpress]# cp wp-config-sample.php wp-config.php
[root@nginx wordpress]# vi wp-config.php
[root@nginx wordpress]# sed -n '23p;26p;29p;32p' wp-config.php
#显示修改的wordpress网站配置文件
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'localhost');


相关推荐
wuxingge9 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
志凌海纳SmartX10 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总10 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes
BUG弄潮儿10 小时前
k8s 集群安装
云原生·容器·kubernetes
意疏10 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant10 小时前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker
Code_Artist10 小时前
Docker镜像加速解决方案:配置HTTP代理,让Docker学会科学上网!
docker·云原生·容器
颜淡慕潇12 小时前
【K8S系列】kubectl describe pod显示ImagePullBackOff,如何进一步排查?
后端·云原生·容器·kubernetes
wanmei00212 小时前
Dockerfile复制目录进入镜像里
docker
inter_peng12 小时前
[Docker-显示所有容器IP] 显示docker-compose.yml中所有容器IP的方法
tcp/ip·docker·eureka