Docker部署lnmp 实战 (多注意配置文件,不管访问试试换个浏览器)
阅读原文时间:2023年07月08日阅读:4

Docker部署LNMP环境

关闭防火墙,设置自定义网络

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

部署nginx(容器IP 为 172.18.0.10)

mkdir /opt/nginx
cd /opt/nginx/

上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html

1 vim Dockerfile
2
3 FROM centos:7
4 MAINTAINER this is nginx image
5 RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
6 RUN useradd -M -s /sbin/nologin nginx
7 ADD nginx-1.12.0.tar.gz /usr/local/src/
8 WORKDIR /usr/local/src/nginx-1.12.0
9 RUN ./configure \
10 --prefix=/usr/local/nginx \
11 --user=nginx \
12 --group=nginx \
13 --with-http_stub_status_module && make && make install
14 ENV PATH /usr/local/nginx/sbin:$PATH
15 ADD nginx.conf /usr/local/nginx/conf/
16 ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
17 RUN chmod 777 -R /usr/local/nginx/html/
18 EXPOSE 80
19 EXPOSE 443
20 ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

docker build -t nginx:lnmp .

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

修改nginx配置文件

部署mysql(容器IP 为 172.18.0.20)

mkdir /opt/mysqld
cd /opt/mysqld

上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中

1 vim Dockerfile
2
3 FROM centos:7
4 MAINTAINER this is mysql image
5 RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
6 RUN useradd -M -s /sbin/nologin mysql
7 ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
8 WORKDIR /usr/local/src/mysql-5.7.20/
9 RUN cmake \
10 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
11 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
12 -DSYSCONFDIR=/etc \
13 -DSYSTEMD_PID_DIR=/usr/local/mysql \
14 -DDEFAULT_CHARSET=utf8 \
15 -DDEFAULT_COLLATION=utf8_general_ci \
16 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
17 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
18 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
19 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
20 -DMYSQL_DATADIR=/usr/local/mysql/data \
21 -DWITH_BOOST=boost \
22 -DWITH_SYSTEMD=1 && make -j4 && make install
23 RUN chown -R mysql:mysql /usr/local/mysql/
24 ADD my.cnf /etc/my.cnf
25 RUN chown mysql:mysql /etc/my.cnf
26 ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
27 WORKDIR /usr/local/mysql/
28 RUN bin/mysqld \
29 --initialize-insecure \
30 --user=mysql \
31 --basedir=/usr/local/mysql \
32 --datadir=/usr/local/mysql/data
33 RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
34 EXPOSE 3306
35 RUN systemctl enable mysqld
36 VOLUME [ "/usr/local/mysql" ]
37 CMD /usr/sbin/init

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

1 vim run.sh
2 #!/bin/bash
3 /usr/local/mysql/bin/mysqld
4 systemctl enable mysqld
5
6
7 docker build -t mysql:lnmp .
8
9 docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp /usr/sbin/init

部署php(容器IP 为 172.18.0.30)

1 mkdir /opt/php
2 cd /opt/php
3
4 上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

vim Dockerfile

FROM centos:7
MAINTAINER this is php image
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 && 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" ]

1 docker build -t php:lnmp .
2 docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
3
4 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
5 docker exec -it mysql /bin/bash

mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'xcc123'
grant all privileges on *.* to 'root'@'%' identified by 'xcc123';
flush privileges;

浏览器访问:

http://20.0.0.40/wordpress/index.php