目录
一、项目的介绍
1、项目需求
2、服务器环境
3、任务需求
二、Linux系统基础镜像
三、部署Nginx
1、建立工作目录
2、编写Dockerfile
3、准备nginx.conf配置文件
4、设置自定义网段和创建镜像和容器
5、启动镜像容器
6、验证nginx
三、Mysql
1、建立工作目录
2、编写Dockerfile
3、准备my.cnf文件
4、生成镜像
5、启动镜像容器
6、验证mysql
四、PHP
1、建立工作目录
2、编写Dockerfile脚本
3、准备php.ini、php-fpm.conf、www.conf配置文件
3.1php.ini
3.2php-fpm.conf
3.3www.conf
3.4生产镜像
4、启动镜像容器
5、验证php
6、启动wordpress服务
1、mysql授权
2、浏览器访问测试
五、故障处理
1、在/opt/nginx/html下创建wp-config.php文件将下面内容粘贴进去,然后保存退出,刷新网页就解决了。
2、注意:粘贴内容时要在最后面加上 ?> ,复制的内容里面最后没有?>
3、然后正常登录就行了。
一、项目的介绍
1、项目需求
在生产环境中使用docker在一台主机上创建LNMP服务并且运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。
2、服务器环境
容器 | 操作系统 | IP地址 | 主要软件 |
nginx | CentOS 7 | 172.111.0.10 | Docker-Nginx |
mysql | CentOS 7 | 172.111.0.20 | Docker-Mysql |
php | CentOS 7 | 172.111.0.30 | Docker-php |
3、任务需求
①使用Docker构建LNMP环境并运行Wordpress网站平台。
②限制Nginx容器最多使用500Mb的内存和1G的Swap。
③限制Mysql容器的写/dev/sda的速率为10Mb/s。
④将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。
二、Linux系统基础镜像
[root@docker ~]# systemctl disable --now firewalld
[root@docker ~]# setenforce 0
setenforce: SELinux is disabled
[root@docker ~]# docker pull centos
#从公有仓库中下载 centos7 作为系统基础镜像
[root@docker ~]# docker images
三、部署Nginx
1、建立工作目录
[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 nginx 安装包 nginx-1.12.0.tar.gz
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz
2、编写Dockerfile
[root@docker nginx]# vim DockerfileFROM centos:7
MAINTAINER this is nginx image <lnmp>
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 -j 4 && 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
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
3、准备nginx.conf配置文件
[root@docker nginx]# ls
Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
[root@docker 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.111.0.30:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;include fastcgi_params;}}
}
4、设置自定义网段和创建镜像和容器
docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetworkdocker network ls
查看
[root@docker nginx]# docker build -t nginx:lnmp .
[root@docker nginx]# docker images
5、启动镜像容器
[root@benet23 nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.111.0.10 nginx:lnmp
24cbedd7982b1cf658cff4efd1ea75a5bfe252b6d01b2a222ed4cde63215479d
[root@benet23 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24cbedd7982b nginx:lnmp "/usr/local/nginx/sb…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx······[root@benet23 nginx]# docker inspect nginx"Networks": {"mynetwork": {"IPAMConfig": {"IPv4Address": "172.111.0.10"
6、验证nginx
[root@docker nginx]# curl http://192.168.170.111:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
三、Mysql
1、建立工作目录
[root@benet23 nginx]# mkdir /opt/mysql
[root@benet23 nginx]# cd /opt/mysql/
[root@benet23 mysql]# rz -E
rz waiting to receive.
#传入mysql安装包mysql-boost-5.7.20.tar.gz
2、编写Dockerfile
2. [root@docker mysql]# vim Dockerfile FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
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_EXTRA_CHARSETS=all \
-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 installADD my.cnf /etc/my.cnfEXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]
3、准备my.cnf文件
[root@docker mysql]# vim my.cnf[client]
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
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
4、生成镜像
[root@docker mysql]# docker build -t mysql:lnmp .
[root@docker mysql]# docker images
5、启动镜像容器
[root@benet23 mysql]# docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp
e75631cda20bf2b2da5537699b006e446abba355f39ef39b2c8d2d98582f5bea
[root@benet23 mysql]# docker ps -a
6、验证mysql
[root@benet23 mysql]# docker exec -it mysql bash
[root@e75631cda20b bin]# systemctl status mysqld
6. ● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: activating (start) since Sun 2023-01-01 04:35:03 UTC; 1min 49s agoDocs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlProcess: 70 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)Process: 50 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)CGroup: /docker/e75631cda20bf2b2da5537699b006e446abba355f39ef39b2c8d2d98582f5bea/system.slice/mysqld.service└─73 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid
四、PHP
1、建立工作目录
[root@benet23 mysql]# mkdir ../php
[root@benet23 mysql]# cd ../php/
[root@benet23 php]# rz -E
rz waiting to receive.
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
3、准备php.ini、php-fpm.conf、www.conf配置文件
3.1php.ini
该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
[root@docker php]# vim php.ini#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@docker php]# egrep -v "^;" php.ini | egrep -v "^$"
3.2php-fpm.conf
该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
[root@docker php]# vim php-fpm.conf #17行,删除注释符号“;”
pid = run/php-fpm.pid
[root@docker php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
3.3www.conf
该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default
[root@docker php]# vim www.conf #23、24行,修改用户和组
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.111.0.30:9000
[root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
3.4生产镜像
[root@benet23 php]# docker build -t php:lnmp .
[root@benet23 php]# docker images
4、启动镜像容器
[root@benet23 php]# docker run -itd --name php --net mynetwork --ip 172.111.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
03e251af5164d3e7392af541f0f2964badbd653db15e7be5d38caabb210c367d
[root@benet23 php]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03e251af5164 php:lnmp "/bin/sh -c '/usr/lo…" 7 seconds ago Up 5 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
e75631cda20b mysql:lnmp "/usr/sbin/init" 10 hours ago Up 10 hours 3306/tcp mysql
24cbedd7982b nginx:lnmp "/usr/local/nginx/sb…" 7 days ago Up 7 days 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
5、验证php
[root@benet23 php]# docker exec -it php bash
[root@03e251af5164 php-7.1.10]# ps -aux
6、启动wordpress服务
1、mysql授权
docker exec -it mysql /bin/bash
[root@e75631cda20b bin]# mysql -u root -p
Enter password:
#初始密码为空
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.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、浏览器访问测试
访问http://192.168.170.111/wordpress/index.php
五、故障处理
在登录过程中可能会出现以下问题
安装WordPress时遇到的问题:抱歉,我不能写入wp-config.php文件。您可以手工创建wp-config.php文件,并将以下文字粘贴于其中。