利用docker的LNMP

目录

服务器环境 

任务需求

服务搭建

Nginx 

Mysql 

Php 

启动 wordpress 服务 


服务器环境 


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

任务需求


使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap
限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地

服务搭建


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 Dockerfile
 
FROM centos:7
MAINTAINER this is nginx image <xtc>
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.20.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}


4. 生成镜像

[root@docker nginx]# docker build -t nginx:lnmp .
[root@docker nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        lnmp      35a6404fcfa1   5 seconds ago   522MB
centos       7         eeb6ee3f44bd   4 weeks ago     204MB


5. 创建自定义网络 

[root@docker nginx]# docker network create --subnet=172.20.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
cdc7b80633abf6c1f573528234f024f6088340475acae277d9710f0d2d5dc400
[root@docker nginx]# docker network ls

[root@benet23 nginx]# ifconfig docker1
docker1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500


6. 启动镜像容器 

[root@benet23 nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.20.0.10 nginx:lnmp
24cbedd7982b1cf658cff4efd1ea75a5bfe252b6d01b2a222ed4cde63215479d
[root@benet23 nginx]# docker ps


[root@benet23 nginx]# docker inspect nginx

7.测试

curl http://192.168.90.10:80

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 
[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 install
ADD my.cnf /etc/my.cnf
EXPOSE 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 = 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. 生成镜像 
[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 /sys/fs/cgroup:/sys/fs/cgroup --net mynetwork --ip 172.20.0.20 mysql:lnmp

[root@benet23 mysql]# docker ps -a


6. 验证 mysql 
[root@benet23 mysql]# docker exec -it mysql bash
[root@e75631cda20b bin]# systemctl status mysqld

Php 


1. 建立工作目录

[root@benet23  mysql]# mkdir ../php
[root@benet23  mysql]# cd ../php/
[root@benet23  php]# rz -E
rz waiting to receive.
#传入php安装包php-7.1.10.tar.bz2

2. 编写 Dockerfile 脚本 
[root@benet23 php]# vim Dockerfile 
 
FROM centos:7
MAINTAINER this is php image <xtc>
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.1 php.ini
该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置

[root@benet23 php]# vim php.ini
 
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@benet23 php]# egrep -v "^;" php.ini | egrep -v "^$"[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]

3.2 php-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.3 www.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.20.0.30:9000
[root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.20.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

4. 生成镜像 
[root@benet23  php]# docker build -t php:lnmp .
[root@benet23 php]# docker images


5. 启动镜像容器 

[root@benet23 php]# docker run -itd --name php --net mynetwork --ip 172.20.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
38c3d71f3e4afcf060cb471ecd9c409d7f236b651bec752139cd5fd32916ee77
[root@benet23 php]# docker ps


6. 验证 php 

[root@benet23 php]# docker exec -it php bash
[root@03e251af5164 php-7.1.10]# ps -aux

启动 wordpress 服务 


1. mysql 授权
进入 mysql 容器

[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.90.10/wordpress/index.php

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/410262.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Lazada不懂英文能做吗?Lazada国内店铺好做吗?-站斧浏览器

Lazada不懂英文可以做吗&#xff1f; Lazada作为一个国际化的电商平台&#xff0c;为了方便用户来自不同国家和地区的购物需求&#xff0c;提供了多语言支持。对于不懂英文的用户来说&#xff0c;他们同样可以在Lazada上进行购物。 首先&#xff0c;Lazada平台上的界面和商品…

爬虫案例—抓取豆瓣电影的电影名称、评分、简介、评价人数

爬虫案例—抓取豆瓣电影的电影名称、评分、简介、评价人数 豆瓣电影网址&#xff1a;https://movie.douban.com/top250 主页截图和要抓取的内容如下图&#xff1a; 分析&#xff1a; 第一页的网址&#xff1a;https://movie.douban.com/top250?start0&filter 第二页的…

uniapp微信小程序投票系统实战 (SpringBoot2+vue3.2+element plus ) -关于我们页面实现

锋哥原创的uniapp微信小程序投票系统实战&#xff1a; uniapp微信小程序投票系统实战课程 (SpringBoot2vue3.2element plus ) ( 火爆连载更新中... )_哔哩哔哩_bilibiliuniapp微信小程序投票系统实战课程 (SpringBoot2vue3.2element plus ) ( 火爆连载更新中... )共计21条视频…

Shell脚本同时调用#!/bin/bash和#!/usr/bin/expect

如果你想在一个脚本中同时使用bash和expect&#xff0c;你可以将expect部分嵌入到bash脚本中。以下是一个示例&#xff1a; #!/bin/bash# 设置MySQL服务器地址、端口、用户名和密码 MYSQL_HOST"localhost" MYSQL_PORT"3306" MYSQL_USER"your_usernam…

【实战记录】 vagrant+virtualbox+docker 轻松用虚拟机集成组件

用途 最近要学一大堆组件&#xff0c;不想直接安装本机上&#xff0c;然后gpt说&#xff1a;你可以用vagrant起个虚拟机&#xff08;然后docker拉取各种组件的镜像&#xff09;&#xff1b;或者k8s 实战的整体思路 首先安装virtualbox和vagrant。然后cmd依次键入三条命令 安…

powershell脚本 判断NLB是否已安装

bat脚本&#xff1a;456.bat REM REM 解决下载的时候字符乱码的问题 chcp 65001 echo offREM 管理员运行powershell脚本 PUSHD %~DP0 & cd /d "%~dp0" %1 %2 mshta vbscript:createobject("shell.application").shellexecute("%~s0","…

通过OpenIddict设计一个授权服务器02-创建asp.net项目

在这一部分中&#xff0c;我们将创建一个ASPNET核心项目&#xff0c;作为我们授权服务器的最低设置。我们将使用MVC来提供页面&#xff0c;并将身份验证添加到项目中&#xff0c;包括一个基本的登录表单。 创建一个空的asp.net core项目 正如前一篇文章中所说&#xff0c;授权…

docker-compose和docker compose的区别

在docker实际使用中&#xff0c;经常会搭配Compose&#xff0c;用来定义和运行多个 Docker 容器。使用时会发现&#xff0c;有时候的指令是docker-compose&#xff0c;有时候是docker compose&#xff0c;下面给出解释。 docker官方文档&#xff1a;https://docs.docker.com/c…

抖音矩阵云混剪系统源码(免授权版)多平台多账号一站式管理,附带系统搭建教程

搭建教程 MySQL 5.6 PHP 7.2 Apache 数据库名称 juzhen Nginx环境切换伪静态 1、解压安装包到项目根目录&#xff0c;找到application/database.php 更换自己的数据库密码 2、阿里云现有的配置不要动 其他按照文档进行添加 3、项目访问目录&#xff1a;public 4、域名…

MATLAB中simulink中scope同时显示两个输入信号

在使用scope时&#xff0c;需要两个输入信号的设置方法 1.点开scope图标 2 点击设置按钮&#xff0c; 然后弹出configuration properties&#xff1a;scope配置图&#xff0c;在Main选项下&#xff0c;在Number of input ports&#xff1a;1这里面更改数字&#xff0c;需要几…

RTSP协议实现发送ACC音频数据

一.AAC音频格式介绍 AAC音频格式&#xff1a;Advanced Audio Coding&#xff08;高级音频解码&#xff09;&#xff0c;是一种由MPEG—4标准定义的有损音频压缩格式。音频压缩编码的输出码流&#xff0c;以音频帧的形式存在。每个音频帧包含若干个音频采样的压缩数据&#xff0…

CSS 雷达监测效果

<template><view class="center"><view class="loader"><view></view></view></view></template><script></script><style>/* 设置整个页面的背景颜色为深灰色 */body {background-col…