• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

使用docker部署lnmp

开发技术 开发技术 3小时前 1次浏览

使用docker 安装分布式lnmp 架构

安装前环境准备

nginx: 172.18.0.10

mysql: 172.18.0.20

php: 172.18.0.30


[root@host103 ~]# systemctl stop firewalld 
[root@host103 ~]# systemctl disable firewalld
[root@host103 ~]# setenforce 0

#自定义一个网卡,网卡名为 docker1 ,网络模式为mynetwork, ip地址池为
[root@host103 ~]# docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1"  mynetwork

[root@host103 ~]# ifconfig  docker1
[root@host103 ~]# docker  network ls

使用docker部署lnmp

2 部署nginx

#部署容器nginx(172.18.0.10)

[root@host103 opt]# mkdir /opt/nginx
[root@host103 opt]# cd /opt/nginx/

[root@host103 nginx]# ls nginx-1.12.0.tar.gz 
nginx-1.12.0.tar.gz
[root@host103 nginx]# ls wordpress-4.9.4-zh_CN.tar.gz 
wordpress-4.9.4-zh_CN.tar.gz

[root@host103 nginx]# mkdir html
[root@host103 nginx]# tar xf wordpress-4.9.4-zh_CN.tar.gz  -C html/

#创建Dockerfile文件
[root@host103 nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <zhi>
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 -j8&& make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]



#从另一台主机复制nginx的配置文件到次主机,并修改
#配置nginx 首页文件包含index.php,配置nginx支持php
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;
        #修改,配置网页的字符集编码为utf8
        charset utf8;
        location / {
            root   html;
            #修改,配合首页文件有 index.php
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        #配置nginx支持php
        location ~ .php$ {
            root           html;
            #fastcgi_pass 配置php所在容器的ip以及端口
            fastcgi_pass   172.18.0.30:9000;
            fastcgi_index  index.php;
            #配置传递脚本文件请求路径。$fastcgi_script_name 前配置的是nginx的网页文件
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}



#使用Dockerfile 创建镜像 nginx:lnmp
[root@host103 nginx]# docker build -t nginx:lnmp .

#创建并启动容器nginx
#--name 指定容器名;-p 指定宿主机端口和容器端口映射; 
#-v 指定数据卷挂载,将宿主机/opt/nginx/html	挂载到容器的/usr/local/nignx/html目录下
#--net 指定网络模式; --ip 指定容器使用的ip
[root@host103 ~]# docker run -d --name nginx -p 80:80 
-v /opt/nginx/html:/usr/local/nginx/html 
--net mynetwork --ip 172.18.0.10 nginx:lnmp

[root@host103 ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS              PORTS                                        NAMES
50752d42a7b5   nginx:lnmp   "/usr/local/nginx/sb…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tc++p, :::80->80/tc++p, 443/tc++p   nginx

使用docker部署lnmp

使用docker部署lnmp

3 部署mysql

#部署mysql(容器IP 为 172.18.0.20

#创建mysql工作目录
[root@host103 ~]# mkdir /opt/mysqld
[root@host103 ~]# cd /opt/mysqld

#将包上传到
[root@host103 mysqld]# ls mysql-boost-5.7.20.tar.gz 
mysql-boost-5.7.20.tar.gz



#编写Dockerfile文件
[root@host103 mysqld]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <zhi>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN 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_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 -j8 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
#将mysql的配置文件上传到容器
ADD my.cnf /etc/
RUN chown mysql:mysql /etc/my.cnf
#配置环境变量
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
#初始化mysql
RUN bin/mysqld 
--initialize-insecure 
--user=mysql 
--basedir=/usr/local/mysql 
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
#上传脚本到容器,使用脚本启动mysql
ADD run.sh /usr/local/src
RUN chmod 755 /usr/local/src/run.sh
RUN sh /usr/local/src/run.sh
#创建数据卷,后面会挂载使用,php会使用此目录里的mysql.sock 文件(注意volume的位置)
VOLUME [ "/usr/local/mysql" ]
CMD /sbin/init






编写run.sh脚本文件

[root@host103 mysqld]# vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld	
systemctl enable mysqld





配置mysql的主配置文件

[root@host104 mysql]# 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


#创建镜像,启动容器
[root@host103 mysqld]# docker build -t mysql:lnmp .
[root@host103 mysqld]# docker ps 
CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                                        NAMES
461d0173ec5b   mysql:lnmp   "/bin/sh -c /sbin/in…"   8 seconds ago   Up 5 seconds   3306/tc++p                                     mysql

4 部署php

#创建工作目录
[root@host103 ~]# mkdir /opt/php
[root@host103 ~]# cd /opt/php

#将软件包上传到工作目录
[root@host103 php]# ls php-7.1.10.tar.bz2 
php-7.1.10.tar.bz2

[root@host103 php]# vim Dockerfile
FROM centos:7
MAINTAINER this is php image <zhi>
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 -j8 && 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" ]

使用docker部署lnmp

#将另一台的文件复制到工作目录,并修改
[root@host103 php]# egrep -nv "^$|^;" php-fpm.conf 
13:[global]
17:pid = run/php-fpm.pid
125:include=/usr/local/php/etc/php-fpm.d/*.conf

[root@host103 php]#vim /usr/local/php/lib/php.ini
1170//mysqli.default_socket = /usr/local/mysql/mysql.sock
939//date.timezone = Asia/Shanghai

[root@host103 php]# egrep -nv "^$|^;"  www.conf 
4:[www]
23:user = nginx
24:group = nginx
36:listen = 172.18.0.30:9000
96:pm = dynamic
107:pm.max_children = 5
112:pm.start_servers = 2
117:pm.min_spare_servers = 1
122:pm.max_spare_servers = 3


#生成镜像,创建并启动容器
[root@host103 php]# docker build -t php:lnmp .
#启动容器,需要从nginx容器和mysql容器中获取数据
[root@host103 php]# docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

[root@host103 php]# docker ps 
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                        NAMES
9b8ce98467a1   php:lnmp     "/usr/local/php/sbin…"   12 seconds ago   Up 11 seconds   0.0.0.0:9000->9000/tc++p, :::9000->9000/tc++p    php
461d0173ec5b   mysql:lnmp   "/bin/sh -c /sbin/in…"   23 minutes ago   Up 23 minutes   3306/tcp                                     mysql
50752d42a7b5   nginx:lnmp   "/usr/local/nginx/sb…"   2 hours ago      Up 2 hours      0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   nginx

#进入容器,进行用户授权
[root@host103 php]# docker exec -it mysql /bin/bash
[root@461d0173ec5b mysql]# mysql -uroot -pabc123

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 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

#进行访问测试
[root@host103 ~]# firefox http://192.168.23.103/wordpress/index.php

使用docker部署lnmp


程序员灯塔
转载请注明原文链接:使用docker部署lnmp
喜欢 (0)