目录
一、基础环境准备
1、环境前期准备
二、部署nginx(容器IP 为 172.18.0.10)
1、配置Dockerfile文件
2、配置nginx.conf文件
3、构建镜像、启动镜像
三、部署mysql
1、配置Dockerfile文件
2、配置my.conf文件
3、构建镜像、启动镜像
5、验证mysql
四、PHP部署
1、配置Dockerfile
2、配置相关文件
2.1 配置php.ini文件
2.2 配置php-fpm.conf文件
2.3 配置www.conf文件
3、构建镜像、启动镜像
五、启动wordpress服务
1、登录到mysql容器中,授权
2、网页访问:http://20.0.0.55/wordpress/wp-admin/setup-config.php
一、基础环境准备
(1)公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。
(2)环境描述
容器 | 系统 | IP地址 | 软件 |
---|---|---|---|
nginx | centos 7.5 | 172.18.0.10 | wordpress-6.0.2-zh_CN.tar.gz、nginx-1.12.2.tar.gz |
mysql | centos 7.5 | 172.18.0.20 | mysql-boost-5.7.25.tar.g |
php | centos 7.5 | 172.18.0.30 | php-7.1.24.tar.gz |
(3)任务需求
(1)使用Docker构建LNMP环境并运行Wordpress网站平台。
(2)限制Nginx容器最多使用500Mb的内存和1G的Swap。
(3)限制Mysql容器写 /dev/sda 的速率为 10 MB/s。
(4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。
1、环境前期准备
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#关闭防火墙和核心防护
docker pull centos:7
#从docker Hub公共仓库下载基础镜像
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
#设置自定义网络模式,模式为bridge模式,docker1的ip地址为172.18.0.1,显示的网络名称mynetwork。
docker network ls
#查看使用的网络模式
ifconfig
二、部署nginx(容器IP 为 172.18.0.10)
1、配置Dockerfile文件
mkdir /opt/nginx
cd /opt/nginx
#此目录上传nginx包
vim Dockerfile
#编写dockerfile文件
FROM centos:7
#指定基础镜像
MAINTAINER ydq
#指定维护者信息
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#安装依赖包,安装环境
RUN useradd -M -s /sbin/nologin nginx
#创建nginx用户
ADD nginx-1.12.2.tar.gz /opt
#将本地安装包上传到指定目录(自动解压)
WORKDIR /opt/nginx-1.12.2
#进入到/opt/nginx-1.12.2目录中
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
#开始编译安装,加上&&符号,只有前面执行完成,后面才会执行
ENV PATH=/usr/local/nginx/sbin:$PATH
#将nginx执行命令放入到环境变量中
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
#在宿主机写好配置文件,然就将配置文件复制到容器中
ADD wordpress-6.0.2-zh_CN.tar.gz /usr/local/nginx/html
#上传论坛的包
RUN chmod 777 -R /usr/local/nginx/html
#给html文件所有权限
EXPOSE 80
#开放80端口
VOLUME ["/usr/local/nginx/html/"]
#挂载目录(相当于将/usr/local/nginx/html目录共享,会在宿主机上自动生成共享目录,主要目的是为了让容器之间共享这个文件)
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
#执行启动nginx命令,并且关闭nginx的后台启动,因为需要它占用前台,让docker不关闭。
2、配置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$ { #当访问以.php结尾的URL时,会连接到172.18.0.30的ip地址上
root html;
fastcgi_pass 172.18.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
3、构建镜像、启动镜像
在构建镜像前,确保一下文件压缩包都在。
docker build -t nginx:lnmp .
#构建nginx镜像(别忘记最后的点)
docker images
#查看镜像
docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.18.0.10 nginx:lnmp
#启动容器
----------------------------
-m 500: #表示该容器内存最大为500MB
--memory-swap: #表示内存+swap总共1G,那swap也是500MB
//相当于是限制了内存的使用量,避免所占资源太多
----------------------
docker ps -a
#查看容器
curl 20.0.0.55
三、部署mysql
1、配置Dockerfile文件
mkdir /opt/mysql
cd /opt/mysql
#上传mysql包到此目录下
vim Dockerfile
FROM centos:7
#基础镜像
MAINTAINER ydq
#维护人信息
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make
#安装环境
RUN useradd -M -s /sbin/nologin mysql
#创建mysql用户
ADD mysql-boost-5.7.25.tar.gz /opt
#将mysql安装包上传(自带boost)
WORKDIR /opt/mysql-5.7.25
#切换到mysql解压后的目录
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 install
#编译安装
EXPOSE 3306
#开放端口
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
#设置权限
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#设置环境变量
WORKDIR /usr/local/mysql/bin
#切换目录
RUN ./mysql \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
#初始化mysql
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
#复制mysql服务程序到启动文件中
VOLUME ["/usr/local/mysql"]
#设置共享目录
CMD ["/usr/sbin/init"]
#启动容器之后,可以使用systemctl工具(并且占用前台,保持容器不断)
2、配置my.conf文件
vim /opt/my.cnf
[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
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
3、构建镜像、启动镜像
docker build -t mysql:lnmp .
#生成镜像
docker images
#查看镜像
docker run --name mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
#生成容器
-----------------------------------------
--privileged: #让容器内拥有root权限
--device-write-bps: #限制写入到/dev/sda中每秒最大10M的速度。
-v /usr/local/mysql: #共享这个目录,其它容器可以使用--volumes-from 指定读取本容器的共享目录。然后也会生成一样的目录。
--net mynetwork: #指定--net网络模式,mynetwork为自定义网络模式。
--ip 172.18.0.20: #指定ip地址,自定义网络模式也可以指定ip地址。
-----------------------------------------
docker ps -a
#查看容器
5、验证mysql
docker exec -it mysql bash
#登录该容器
systemctl status mysqld
#查看mysql状态
四、PHP部署
1、配置Dockerfile
mkdir /opt/php
cd /opt/php
#将php包上传到该目录
vim Dockerfile
FROM centos:7
MAINTAINER ydq3
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
#下载环境,以及创建nginx用户。
ADD php-7.1.24.tar.gz /opt
#上传php包到/opt目录下
WORKDIR /opt/php-7.1.24
#进入到指定目录
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
#启动一个进程,占用前台。
2、配置相关文件
2.1 配置php.ini文件
vim /opt/php/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]
2.2 配置php-fpm.conf文件
vim /opt/php/php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
2.3 配置www.conf文件
vim /opt/php/www.conf
[www]
user = nginx
group = nginx
listen = 172.18.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
3、构建镜像、启动镜像
docker build -t php:lnmp .
#构建镜像
docker images
#查看
docker run -itd --name php --net mynetwork --ip 172.18.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
#启动容器
-----------------------------------------------
--volumes-from nginx: #表示读取nginx容器的共享信息
--volumes-from mysql: #表示读取mysql容器的共享信息
-----------------------------------------------
docker ps -a
#查看容器
五、启动wordpress服务
1、登录到mysql容器中,授权
docker exec -it mysql bash
#登录到mysql容器
mysql
#登录到容器中
create database wordpress;
#创建一个数据库
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
文章来源:https://www.toymoban.com/news/detail-715477.html
2、网页访问:http://20.0.0.55/wordpress/wp-admin/setup-config.php
文章来源地址https://www.toymoban.com/news/detail-715477.html
到了这里,关于基于Dockerfile搭建LNMP的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!