Docker构建LNMP环境并运行Wordpress平台

1.准备Nginx

上传文件

Dockerfile

FROM centos:7 as first

ADD nginx-1.24.0.tar.gz /opt/
COPY CentOS-Base.repo /etc/yum.repos.d/

RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make && \
    useradd -M -s /sbin/nologin nginx && \
    cd /opt/nginx-1.24.0/ && \
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module && \
    make && \
    make install
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

FROM centos:7
COPY --from=first /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

nginx.conf

html

准备测试文件

2.准备Mysql

上传文件

Dockerfile

FROM centos:7 as first
ADD mysql-boost-5.7.44.tar.gz /opt
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN yum -y install ncurses ncurses-devel bison cmake openssl-devel gcc gcc-c++ make && \
    yum clean all && \
    cd /opt/mysql-5.7.44/ && \
    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 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && \
    make -j 2 && make install

FROM centos:7
COPY --from=first /usr/local/mysql/ /usr/local/mysql/
COPY  my.cnf /etc/my.cnf
RUN useradd -M -s /sbin/nologin mysql &&\
    chown -R mysql:mysql /usr/local/mysql/ &&\
    chown mysql:mysql /etc/my.cnf &&\
    /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
ENV PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]

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

3.准备php

上传文件

Dockerfile

--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 2 && make install
COPY php.ini /usr/local/php/lib/php.ini
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf

FROM centos:7
COPY --from=first /usr/local/php/ /usr/local/php/
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN useradd -M -s /sbin/nologin nginx &&\
yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel &&\
yum clean all
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]

4.构建镜像

cd /opt/nginx_bian    #构建nginx镜像
docker build -t nginx:lnmp .

cd /opt/php    #构建php镜像
docker build -t php:lnmp .

docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork   #自定义网络
docker network ls 


 
docker run --name nginx -d -p 80:80 -v /opt/nginx_bian/html:/usr/local/nginx/html/ --network mynetwork --ip 172.18.0.10 nginx:lnmp
docker ps -a

访问测试页面

docker run --name php -d -p 9000:9000 --volumes-from nginx --network mynetwork --ip 172.18.0.30 php:lnnmp
 
docker ps -a

访问测试页面

cd /opt/mysql
docker build -t mysql:lnmp .  #构建mysql镜像
 
docker run --name mysql -d --privileged -v /usr/local/mysql --network mynetwork --ip 172.18.0.20 mysql:lnmp   #启动容器
docker ps -a

docker exec -it mysql bash
mysqladmin -u root -p password "abc123"  #设置密码
mysql -u root -pabc123  #登录mysql
grant all on *.* to 'root'@'%' identified by 'admin123';
quit
exit
docker stop php
docker rm php
 
docker run --name php -d -p 9000:9000 --volumes-from nginx --volumes-from mysql --network mynetwork --ip 172.18.0.30 php:lnnmp

5.运行wordpress

浏览器访问:http://192.168.9.113/wordpress/index.php

docker exec -it mysql bash
mysql -u root -pabc123
create database wordpress;
grant all on wordpress.* to 'wordpress'@'%' identified by 'admin123';
flush privileges;
quit
exit
cd /opt/nginx_bian/html
chmod 777 wordpress/
相关推荐
运维&陈同学11 分钟前
【Beats01】企业级日志分析系统ELK之Metricbeat与Heartbeat 监控
运维·elk·elasticsearch·云原生·kibana·heartbeat·metricbeat
地球资源数据云13 分钟前
全国30米分辨率逐年植被覆盖度(FVC)数据集
大数据·运维·服务器·数据库·均值算法
是店小二呀21 分钟前
【Linux】Linux开发利器:make与Makefile自动化构建详解
linux·运维·自动化
baihb10241 小时前
Jenkins 构建流水线
运维·jenkins
BUG 4041 小时前
LINUX--shell
linux·运维·服务器
菜鸟小白:长岛icetea1 小时前
Linux零基础速成篇一(理论+实操)
linux·运维·服务器
Java小白中的菜鸟4 小时前
centos7的磁盘扩容
linux·运维·服务器
黑客老陈5 小时前
面试经验分享 | 北京渗透测试岗位
运维·服务器·经验分享·安全·web安全·面试·职场和发展
橘子师兄5 小时前
如何在自己的云服务器上部署mysql
运维·服务器·mysql
@泽栖5 小时前
Docker 常用命令
运维·服务器·docker