当前位置:   article > 正文

基于 docker-compose 部署 LNMP 架构_centos7 dockerfile和docker-compose搭建lnmp

centos7 dockerfile和docker-compose搭建lnmp

目录

前言

1、任务要求

2、Nginx

2.1 建立工作目录并上传相关安装包

2.2 编写 Nginx Dockerfile 脚本

2.3 准备 nginx.conf 配置文件

3、Mysql

3.1 建立工作目录并上传相关安装包

3.2 编写 Mysql Dockerfile 脚本

3.3 编写 my.cnf 配置文件

4、PHP

4.1 建立工作目录并上传相关安装包

4.2 编写 PHP Dockerfile 脚本

4.3 准备 php.ini、php-fpm.conf、www.conf 配置文件

4.3.1 修改php.ini 配置文件

4.3.2 修改 php-fpm.conf 配置文件

4.3.3 修改www.conf 配置文件

5、Docker-Compose环境安装

5.1 安装docker-compose

5.2 docker-compose.yml 文件配置

5.3 lnmp Docker-Compose 文件结构

5.4 启动 Docker 容器

6、启动 wordpress 服务

6.1 mysql授权

6.2 浏览器访问安装wordpress


前言

LNMP架构是一种流行的Web服务架构,由Linux、Nginx、MySQL和PHP组成

它提供了一个稳定、高性能的平台,适用于各种Web应用程序的部署

本文将介绍如何使用docker-compose来部署LNMP架构,以简化部署流程,并提高整个架构的可管理性和可移植性

  • Linux:作为操作系统,提供了稳定的运行环境
  • Nginx:作为Web服务器,具有高性能和高并发处理能力,可以作为反向代理服务器,负责接收用户的请求并将请求转发给后端的应用服务器
  • MySQL:作为关系型数据库,提供了数据存储和管理的功能
  • PHP(或Python、Perl):作为服务器端脚本语言,用于处理用户的请求,生成动态的网页内容

1、任务要求

需要使用 docker-compose 编排 lnmp(dockerfile) ,完成并运行Wordpress 网站平

宿主机

操作系统

IP地址

主要软件

配置要求

Docker

CentOS 7

172.16.12.15

Docker 20.10.17

4核8G以上

容器

 操作系统

IP地址

主要软件

nginx

CentOS 7

172.30.0.10

Docker-Nginx

mysql

CentOS 7

172.30.0.20

Docker-Mysql

php

CentOS 7

172.30.0.30

Docker-php

关闭设备的防火墙和核心防护

  1. systemctl disable --now  firewalld
  2. setenforce 0

2、Nginx

2.1 建立工作目录并上传相关安装包

  1. mkdir -p /opt/lnmp/nginx/html
  2. cd /opt/lnmp/nginx         #需在此路径上传 nginx 安装包
  3. cd /opt/lnmp/nginx/html   #需在此路径上传 wordpress 服务包

2.2 编写 Nginx Dockerfile 脚本

  1. cd /opt/lnmp/nginx
  2. vim Dockerfile
  3. FROM centos:7
  4. MAINTAINER this is nginx image <lnmp>
  5. RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  6. RUN useradd -M -s /sbin/nologin nginx
  7. ADD nginx-1.12.0.tar.gz /usr/local/src/
  8. WORKDIR /usr/local/src/nginx-1.12.0
  9. RUN ./configure \
  10. --prefix=/usr/local/nginx \
  11. --user=nginx \
  12. --group=nginx \
  13. --with-http_stub_status_module;make -j 4 && make install
  14. ENV PATH /usr/local/nginx/sbin:$PATH
  15. ADD nginx.conf /usr/local/nginx/conf/
  16. ADD html/wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
  17. RUN chmod 777 -R /usr/local/nginx/html/
  18. EXPOSE 80
  19. EXPOSE 443
  20. VOLUME [ "/usr/local/nginx/html/" ]
  21. CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

2.3 准备 nginx.conf 配置文件

  1. #需上传nginx的主配置文件到/opt/lnmp/nginx目录下,再过滤查看其生效的配置
  2. egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
  3. worker_processes  1;
  4. events {
  5.     worker_connections  1024;
  6. }
  7. http {
  8.     include       mime.types;
  9.     default_type  application/octet-stream;
  10.     sendfile        on;
  11.     keepalive_timeout  65;
  12.     server {
  13.         listen       80;
  14.         server_name  localhost;
  15.         charset utf-8;
  16.         location / {
  17.             root   html;
  18.             index  index.html index.php;
  19.         }
  20.         error_page   500 502 503 504  /50x.html;
  21.         location = /50x.html {
  22.             root   html;
  23.         }
  24.         location ~ \.php$ {
  25.             root           html;
  26.             fastcgi_pass   172.30.0.30:9000;
  27.             fastcgi_index  index.php;
  28.             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
  29.             include        fastcgi_params;
  30.         }
  31.     }
  32. }​

3、Mysql

3.1 建立工作目录并上传相关安装包

  1. mkdir -p /opt/lnmp/mysql
  2. cd /opt/lnmp/mysql   #需在此路径上传 mysql 安装包

3.2 编写 Mysql Dockerfile 脚本

  1. vim Dockerfile
  2. FROM centos:7
  3. MAINTAINER this is mysql image <lnmp>
  4. RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
  5. ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
  6. WORKDIR /usr/local/src/mysql-5.7.20/
  7. RUN cmake \
  8. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  9. -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  10. -DSYSCONFDIR=/etc \
  11. -DSYSTEMD_PID_DIR=/usr/local/mysql \
  12. -DDEFAULT_CHARSET=utf8 \
  13. -DDEFAULT_COLLATION=utf8_general_ci \
  14. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  15. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  16. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  17. -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
  18. -DMYSQL_DATADIR=/usr/local/mysql/data \
  19. -DWITH_BOOST=boost \
  20. -DWITH_SYSTEMD=1 && make -j 4 && make install
  21. ADD my.cnf /etc/
  22. ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
  23. RUN useradd -M -s /sbin/nologin mysql
  24. RUN chown mysql:mysql /etc/my.cnf
  25. RUN chown -R mysql:mysql /usr/local/mysql/
  26. RUN /usr/local/mysql/bin/mysqld \
  27. --initialize-insecure \
  28. --user=mysql \
  29. --basedir=/usr/local/mysql \
  30. --datadir=/usr/local/mysql/data
  31. EXPOSE 3306
  32. CMD ["/usr/local/mysql/bin/mysqld"]

3.3 编写 my.cnf 配置文件

  1. vim my.cnf
  2. [client]
  3. port = 3306
  4. socket = /usr/local/mysql/mysql.sock
  5. [mysql]
  6. port = 3306
  7. socket = /usr/local/mysql/mysql.sock
  8. [mysqld]
  9. user = mysql
  10. basedir = /usr/local/mysql
  11. datadir = /usr/local/mysql/data
  12. port = 3306
  13. character_set_server=utf8
  14. pid-file = /usr/local/mysql/mysqld.pid
  15. socket = /usr/local/mysql/mysql.sock
  16. server-id = 1
  17. 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

4、PHP

4.建立工作目录并上传相关安装包

  1. mkdir -p /opt/lnmp/php
  2. cd /opt/lnmp/php     #需在此路径上传 php 安装包

4.2 编写 PHP Dockerfile 脚本

 

  1. vim Dockerfile
  2. FROM centos:7
  3. MAINTAINER this is php image <lnmp>
  4. RUN yum -y install gd \
  5. libjpeg libjpeg-devel \
  6. libpng libpng-devel \
  7. freetype freetype-devel \
  8. libxml2 libxml2-devel \
  9. zlib zlib-devel \
  10. curl curl-devel \
  11. openssl openssl-devel \
  12. gcc gcc-c++ make pcre-devel
  13. RUN useradd -M -s /sbin/nologin nginx
  14. ADD php-7.1.10.tar.bz2 /usr/local/src/
  15. WORKDIR /usr/local/src/php-7.1.10
  16. RUN ./configure \
  17. --prefix=/usr/local/php \
  18. --with-mysql-sock=/usr/local/mysql/mysql.sock \
  19. --with-mysqli \
  20. --with-zlib \
  21. --with-curl \
  22. --with-gd \
  23. --with-jpeg-dir \
  24. --with-png-dir \
  25. --with-freetype-dir \
  26. --with-openssl \
  27. --enable-fpm \
  28. --enable-mbstring \
  29. --enable-xml \
  30. --enable-session \
  31. --enable-ftp \
  32. --enable-pdo \
  33. --enable-tokenizer \
  34. --enable-zip && make -j 4 && make install
  35. ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
  36. ADD php.ini /usr/local/php/lib/
  37. ADD php-fpm.conf /usr/local/php/etc/
  38. ADD www.conf /usr/local/php/etc/php-fpm.d/
  39. EXPOSE 9000
  40. ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]

4.3 准备 php.ini、php-fpm.conf、www.conf 配置文件

4.3.1 修改php.ini 配置文件

该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置

  1. vim php.ini
  2. #第939行,取消注释,修改
  3. date.timezone = Asia/Shanghai
  4. #第1170行,修改
  5. mysqli.default_socket = /usr/local/mysql/mysql.sock

  1. [root@localhost php]#egrep -v "^;" php.ini | egrep -v "^$" #过滤php.ini文件里生效的配置
  2. [PHP]
  3. engine = On
  4. short_open_tag = Off
  5. precision = 14
  6. output_buffering = 4096
  7. zlib.output_compression = Off
  8. implicit_flush = Off
  9. unserialize_callback_func =
  10. serialize_precision = -1
  11. disable_functions =
  12. disable_classes =
  13. zend.enable_gc = On
  14. expose_php = On
  15. max_execution_time = 30
  16. max_input_time = 60
  17. memory_limit = 128M
  18. error_reporting = E_ALL
  19. display_errors = On
  20. display_startup_errors = On
  21. log_errors = On
  22. log_errors_max_len = 1024
  23. ignore_repeated_errors = Off
  24. ignore_repeated_source = Off
  25. report_memleaks = On
  26. track_errors = On
  27. html_errors = On
  28. variables_order = "GPCS"
  29. request_order = "GP"
  30. register_argc_argv = Off
  31. auto_globals_jit = On
  32. post_max_size = 8M
  33. auto_prepend_file =
  34. auto_append_file =
  35. default_mimetype = "text/html"
  36. default_charset = "UTF-8"
  37. doc_root =
  38. user_dir =
  39. enable_dl = Off
  40. file_uploads = On
  41. upload_max_filesize = 2M
  42. max_file_uploads = 20
  43. allow_url_fopen = On
  44. allow_url_include = Off
  45. default_socket_timeout = 60
  46. [CLI Server]
  47. cli_server.color = On
  48. [Date]
  49. date.timezone = Asia/Shanghai
  50. [filter]
  51. [iconv]
  52. [intl]
  53. [sqlite3]
  54. [Pcre]
  55. [Pdo]
  56. [Pdo_mysql]
  57. pdo_mysql.cache_size = 2000
  58. pdo_mysql.default_socket=
  59. [Phar]
  60. [mail function]
  61. SMTP = localhost
  62. smtp_port = 25
  63. mail.add_x_header = On
  64. [SQL]
  65. sql.safe_mode = Off
  66. [ODBC]
  67. odbc.allow_persistent = On
  68. odbc.check_persistent = On
  69. odbc.max_persistent = -1
  70. odbc.max_links = -1
  71. odbc.defaultlrl = 4096
  72. odbc.defaultbinmode = 1
  73. [Interbase]
  74. ibase.allow_persistent = 1
  75. ibase.max_persistent = -1
  76. ibase.max_links = -1
  77. ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
  78. ibase.dateformat = "%Y-%m-%d"
  79. ibase.timeformat = "%H:%M:%S"
  80. [MySQLi]
  81. mysqli.max_persistent = -1
  82. mysqli.allow_persistent = On
  83. mysqli.max_links = -1
  84. mysqli.cache_size = 2000
  85. mysqli.default_port = 3306
  86. mysqli.default_socket = /usr/local/mysql/mysql.sock
  87. mysqli.default_host =
  88. mysqli.default_user =
  89. mysqli.default_pw =
  90. mysqli.reconnect = Off
  91. [mysqlnd]
  92. mysqlnd.collect_statistics = On
  93. mysqlnd.collect_memory_statistics = On
  94. [OCI8]
  95. [PostgreSQL]
  96. pgsql.allow_persistent = On
  97. pgsql.auto_reset_persistent = Off
  98. pgsql.max_persistent = -1
  99. pgsql.max_links = -1
  100. pgsql.ignore_notice = 0
  101. pgsql.log_notice = 0
  102. [bcmath]
  103. bcmath.scale = 0
  104. [browscap]
  105. [Session]
  106. session.save_handler = files
  107. session.use_strict_mode = 0
  108. session.use_cookies = 1
  109. session.use_only_cookies = 1
  110. session.name = PHPSESSID
  111. session.auto_start = 0
  112. session.cookie_lifetime = 0
  113. session.cookie_path = /
  114. session.cookie_domain =
  115. session.cookie_httponly =
  116. session.serialize_handler = php
  117. session.gc_probability = 1
  118. session.gc_divisor = 1000
  119. session.gc_maxlifetime = 1440
  120. session.referer_check =
  121. session.cache_limiter = nocache
  122. session.cache_expire = 180
  123. session.use_trans_sid = 0
  124. session.sid_length = 26
  125. session.trans_sid_tags = "a=href,area=href,frame=src,form="
  126. session.sid_bits_per_character = 5
  127. [Assertion]
  128. zend.assertions = 1
  129. [COM]
  130. [mbstring]
  131. [gd]
  132. [exif]
  133. [Tidy]
  134. tidy.clean_output = Off
  135. [soap]
  136. soap.wsdl_cache_enabled=1
  137. soap.wsdl_cache_dir="/tmp"
  138. soap.wsdl_cache_ttl=86400
  139. soap.wsdl_cache_limit = 5
  140. [sysvshm]
  141. [ldap]
  142. ldap.max_links = -1
  143. [mcrypt]
  144. [dba]
  145. [opcache]
  146. [curl]
  147. [openssl]

4.3.2 修改 php-fpm.conf 配置文件

该配置文件位于 /usr/local/php/etc/php-fpm.conf.default

  1. vim php-fpm.conf
  2. #第17行,删除注释符号“;”
  3. pid = run/php-fpm.pid

  1. [root@localhost php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
  2. #过滤php-fpm.conf配置文件中生效的配置
  3. [global]
  4. pid = run/php-fpm.pid
  5. include=/usr/local/php/etc/php-fpm.d/*.conf

4.3.3 修www.conf 配置文件

该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default

  1. vim www.conf
  2. #第23、24行,修改用户和组
  3. user = nginx
  4. group = nginx
  5. #第36行,修改监听IP和端口为容器IP:9000端口
  6. listen = 172.30.0.30:9000
  7. #第62行,修改被允许监听的客户端容器地址为回环地址和nginx容器地址
  8. listen.allowed_clients = 127.0.0.1,172.30.0.10

  1. [root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
  2. [www]
  3. user = nginx
  4. group = nginx
  5. listen = 172.30.0.30:9000
  6. listen.allowed_clients = 127.0.0.1,172.30.0.10
  7. pm = dynamic
  8. pm.max_children = 5
  9. pm.start_servers = 2
  10. pm.min_spare_servers = 1
  11. pm.max_spare_servers = 3

5、Docker-Compose环境安装

5.1 安装docker-compose

  1. #下载
  2. cd /opt
  3. curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /opt
  4. #安装:下载到本地并添加权限就能直接使用
  5. mv /opt/docker-compose /usr/local/bin/
  6. chmod +x /usr/local/bin/docker-compose
  7. #查看版本
  8. docker-compose --version

5.2 docker-compose.yml 文件配置

  1. cd /opt/lnmp
  2. vim docker-compose.yml
  3. version: '2'
  4. services:
  5. nginx:
  6. container_name: nginx #指定容器的名称为nginx
  7. hostname: nginx-host #容器的主机名为nginx-host
  8. image: nginx:lnmp #使用的镜像为nginx:lnmp
  9. build: #构建上下文和Dockerfile的位置
  10. context: ./nginx
  11. dockerfile: Dockerfile
  12. ports:
  13. #将宿主机的80端口映射到容器的80端口,将宿主机的443端口映射到容器的443端口
  14. - 80:80
  15. - 443:443
  16. volumes: #将/usr/local/nginx/html目录设为共享挂载点
  17. - /usr/local/nginx/html
  18. networks: #将该服务连接到名为lnmp的网络,并指定了容器的IPv4地址
  19. lnmp:
  20. ipv4_address: 172.30.0.10
  21. mysql:
  22. container_name: mysql #容器的名称为mysql
  23. hostname: mysql-host #容器的主机名为mysql-host
  24. image: mysql:lnmp #使用的镜像为mysql:lnmp
  25. privileged: true #使容器内的root拥有真正的root权限
  26. build: #构建上下文和Dockerfile的位置
  27. context: ./mysql
  28. dockerfile: Dockerfile
  29. ports: #将宿主机的3306端口映射到容器的3306端口
  30. - 3306:3306
  31. volumes: #创建容器共享挂载目录/usr/local/mysql
  32. - /usr/local/mysql
  33. networks:
  34. lnmp:
  35. ipv4_address: 172.30.0.20
  36. php:
  37. container_name: php #容器的名称为php
  38. hostname: php-host #容器的主机名为php-host
  39. image: php:lnmp #使用的镜像为php:lnmp
  40. build: #指定了构建上下文和Dockerfile的位置
  41. context: ./php
  42. dockerfile: Dockerfile
  43. ports: #将宿主机的9000端口映射到容器的9000端口
  44. - 9000:9000
  45. depends_on: #声明了该服务依赖于nginx和mysql服务
  46. - nginx
  47. - mysql
  48. volumes_from: #从nginx和mysql服务中挂载卷
  49. - nginx
  50. - mysql
  51. networks:
  52. lnmp:
  53. ipv4_address: 172.30.0.30
  54. networks: #定义了一个叫做lnmp的网络,使用了bridge驱动
  55. lnmp:
  56. driver: bridge
  57. ipam:
  58. config: #指定了IP地址池的子网范围
  59. - subnet: 172.30.0.0/16

5.3 lnmp Docker-Compose 文件结构

  1. yum install -y tree
  2. tree /opt/lnmp

5.4 启动 Docker 容器

  1. cd /opt/lnmp
  2. docker-compose -f docker-compose.yml up -d
  3. #-f, --file FILE:使用特定的 compose 模板文件,默认为 docker-compose.yml
  4. #-p, --project-name NAME:指定项目名称,默认使用目录名称
  5. #up: Docker Compose 要启动配置文件中定义的服务
  6. #-d:在后台运行

  1. docker images #查看所有镜像
  2. docker ps #查看当前正在运行中的容器

  1. cd /opt/lnmp
  2. docker-compose ps #必须在docker-compose.yml所在目录执行此命令
  3. #用于显示当前Docker Compose项目中正在运行的服务的状态

  1. docker inspect nginx #查看 nginx 容器的ip地址等信息
  2. docker inspect mysql #查看 mysql 容器的ip地址等信息
  3. docker inspect php #查看 php 容器的ip地址等信息

6、启动 wordpress 服务

6.1 mysql授权

  1. [root@localhost mysql]#docker exec -it mysql /bin/bash #进入到 mysql 容器内
  2. [root@mysql-host mysql-5.7.20]#mysql -u root -p
  3. Enter password:
  4. #初始密码为空,回车即可
  5. mysql> create database wordpress;
  6. #创建wordpress数据库
  7. mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
  8. #授予一个名为 wordpress 的数据库中的用户 wordpress 在任何主机上('%')的所有权限,并使用密码 123456 进行身份验证
  9. #将所有权限授予用户 wordpress,允许其从任何主机连接到 wordpress 数据库,并使用密码 123456 进行身份验证
  10. mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
  11. #授予用户名为 root,从任何主机连接到 MySQL 数据库服务器的用户,对所有数据库中的所有表具有全部权限,并设置密码为 abc123
  12. #这样的设置允许用户 root 从任何地方以密码 abc123 连接到 MySQL 数据库,并具有对所有数据库的全部权限
  13. mysql> flush privileges;
  14. #刷新权限
  15. mysql> exit #退出 mysql 数据库
  16. [root@mysql-host mysql-5.7.20]# exit #退出 mysql 容器

6.2 浏览器访问安装wordpress

浏览器访问 http://172.16.12.15/wordpress/index.php

报错页面: 

解决方法:

  1. docker exec -it nginx /bin/bash #进入到nginx容器内
  2. cd /usr/local/nginx/html/wordpress/   #切换目录
  3. cp wp-config-sample.php wp-config.php
  4. #手动创建wp-config.php文件
  5. vim wp-config.php 
  6. #第23行,将'database_name_here'改为'wordpress'
  7. define('DB_NAME', 'wordpress');
  8. #第26行,将'username_here'改为'wordpress'
  9. define('DB_USER', 'wordpress');
  10. #第29行,将'password_here'改为'123456'
  11. define('DB_PASSWORD', '123456');
  12. exit #退出 nginx 容器
  13. systemctl restart docker   #重启docker服务
  14. docker start nginx;docker start mysql;docker start php   #重启docker容器
  15. docker ps   #查看当前正在运行中的容器,必须得有nginx、mysql、php

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/829079
推荐阅读
相关标签
  

闽ICP备14008679号