目录
前言
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 |
关闭设备的防火墙和核心防护:
systemctl disable --now firewalld
setenforce 0
2、Nginx
2.1 建立工作目录并上传相关安装包
mkdir -p /opt/lnmp/nginx/html
cd /opt/lnmp/nginx #需在此路径上传 nginx 安装包
cd /opt/lnmp/nginx/html #需在此路径上传 wordpress 服务包
2.2 编写 Nginx Dockerfile 脚本
cd /opt/lnmp/nginx
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <lnmp>
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 -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD html/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/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
2.3 准备 nginx.conf 配置文件
#需上传nginx的主配置文件到/opt/lnmp/nginx目录下,再过滤查看其生效的配置
egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
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.30.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
3、Mysql
3.1 建立工作目录并上传相关安装包
mkdir -p /opt/lnmp/mysql
cd /opt/lnmp/mysql #需在此路径上传 mysql 安装包
3.2 编写 Mysql Dockerfile 脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <lnmp>
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 -j 4 && 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"]
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
[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
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
4、PHP
4.1 建立工作目录并上传相关安装包
mkdir -p /opt/lnmp/php
cd /opt/lnmp/php #需在此路径上传 php 安装包
4.2 编写 PHP Dockerfile 脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum -y install 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
RUN 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
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 位置
vim php.ini
#第939行,取消注释,修改
date.timezone = Asia/Shanghai
#第1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@localhost php]#egrep -v "^;" php.ini | egrep -v "^$" #过滤php.ini文件里生效的配置
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
4.3.2 修改 php-fpm.conf 配置文件
该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
vim php-fpm.conf
#第17行,删除注释符号“;”
pid = run/php-fpm.pid
[root@localhost php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
#过滤php-fpm.conf配置文件中生效的配置
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
4.3.3 修改www.conf 配置文件
该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default
vim www.conf
#第23、24行,修改用户和组
user = nginx
group = nginx
#第36行,修改监听IP和端口为容器IP:9000端口
listen = 172.30.0.30:9000
#第62行,修改被允许监听的客户端容器地址为回环地址和nginx容器地址
listen.allowed_clients = 127.0.0.1,172.30.0.10
[root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.30.0.30:9000
listen.allowed_clients = 127.0.0.1,172.30.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
5、Docker-Compose环境安装
5.1 安装docker-compose
#下载
cd /opt
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /opt
#安装:下载到本地并添加权限就能直接使用
mv /opt/docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version
5.2 docker-compose.yml 文件配置
cd /opt/lnmp
vim docker-compose.yml
version: '2'
services:
nginx:
container_name: nginx #指定容器的名称为nginx
hostname: nginx-host #容器的主机名为nginx-host
image: nginx:lnmp #使用的镜像为nginx:lnmp
build: #构建上下文和Dockerfile的位置
context: ./nginx
dockerfile: Dockerfile
ports:
#将宿主机的80端口映射到容器的80端口,将宿主机的443端口映射到容器的443端口
- 80:80
- 443:443
volumes: #将/usr/local/nginx/html目录设为共享挂载点
- /usr/local/nginx/html
networks: #将该服务连接到名为lnmp的网络,并指定了容器的IPv4地址
lnmp:
ipv4_address: 172.30.0.10
mysql:
container_name: mysql #容器的名称为mysql
hostname: mysql-host #容器的主机名为mysql-host
image: mysql:lnmp #使用的镜像为mysql:lnmp
privileged: true #使容器内的root拥有真正的root权限
build: #构建上下文和Dockerfile的位置
context: ./mysql
dockerfile: Dockerfile
ports: #将宿主机的3306端口映射到容器的3306端口
- 3306:3306
volumes: #创建容器共享挂载目录/usr/local/mysql
- /usr/local/mysql
networks:
lnmp:
ipv4_address: 172.30.0.20
php:
container_name: php #容器的名称为php
hostname: php-host #容器的主机名为php-host
image: php:lnmp #使用的镜像为php:lnmp
build: #指定了构建上下文和Dockerfile的位置
context: ./php
dockerfile: Dockerfile
ports: #将宿主机的9000端口映射到容器的9000端口
- 9000:9000
depends_on: #声明了该服务依赖于nginx和mysql服务
- nginx
- mysql
volumes_from: #从nginx和mysql服务中挂载卷
- nginx
- mysql
networks:
lnmp:
ipv4_address: 172.30.0.30
networks: #定义了一个叫做lnmp的网络,使用了bridge驱动
lnmp:
driver: bridge
ipam:
config: #指定了IP地址池的子网范围
- subnet: 172.30.0.0/16
5.3 lnmp Docker-Compose 文件结构
yum install -y tree
tree /opt/lnmp
5.4 启动 Docker 容器
cd /opt/lnmp
docker-compose -f docker-compose.yml up -d
#-f, --file FILE:使用特定的 compose 模板文件,默认为 docker-compose.yml
#-p, --project-name NAME:指定项目名称,默认使用目录名称
#up: Docker Compose 要启动配置文件中定义的服务
#-d:在后台运行
docker images #查看所有镜像
docker ps #查看当前正在运行中的容器
cd /opt/lnmp
docker-compose ps #必须在docker-compose.yml所在目录执行此命令
#用于显示当前Docker Compose项目中正在运行的服务的状态
docker inspect nginx #查看 nginx 容器的ip地址等信息
docker inspect mysql #查看 mysql 容器的ip地址等信息
docker inspect php #查看 php 容器的ip地址等信息
6、启动 wordpress 服务
6.1 mysql授权
[root@localhost mysql]#docker exec -it mysql /bin/bash #进入到 mysql 容器内
[root@mysql-host mysql-5.7.20]#mysql -u root -p
Enter password:
#初始密码为空,回车即可
mysql> create database wordpress;
#创建wordpress数据库
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
#授予一个名为 wordpress 的数据库中的用户 wordpress 在任何主机上('%')的所有权限,并使用密码 123456 进行身份验证
#将所有权限授予用户 wordpress,允许其从任何主机连接到 wordpress 数据库,并使用密码 123456 进行身份验证
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予用户名为 root,从任何主机连接到 MySQL 数据库服务器的用户,对所有数据库中的所有表具有全部权限,并设置密码为 abc123
#这样的设置允许用户 root 从任何地方以密码 abc123 连接到 MySQL 数据库,并具有对所有数据库的全部权限
mysql> flush privileges;
#刷新权限
mysql> exit #退出 mysql 数据库
[root@mysql-host mysql-5.7.20]# exit #退出 mysql 容器
6.2 浏览器访问安装wordpress
浏览器访问 http://172.16.12.15/wordpress/index.php
报错页面:
解决方法:
docker exec -it nginx /bin/bash #进入到nginx容器内 cd /usr/local/nginx/html/wordpress/ #切换目录 cp wp-config-sample.php wp-config.php #手动创建wp-config.php文件 vim wp-config.php #第23行,将'database_name_here'改为'wordpress' define('DB_NAME', 'wordpress'); #第26行,将'username_here'改为'wordpress' define('DB_USER', 'wordpress'); #第29行,将'password_here'改为'123456' define('DB_PASSWORD', '123456'); exit #退出 nginx 容器 systemctl restart docker #重启docker服务 docker start nginx;docker start mysql;docker start php #重启docker容器 docker ps #查看当前正在运行中的容器,必须得有nginx、mysql、php