使用 docker-compose 在 CentOS 7 上编写并部署 LNMP (Linux, Nginx, MySQL, PHP) 环境的 YAML
文章目录
- 部署步骤:
- 1. 安装 Docker 和 Docker Compose
- 1.1安装 Docker:
- 1.2安装 Docker Compose:
- 2.创建目录结构
- 3.编写docker-compose.yml
- 4.nginx
- 4.1编写nginx的Dockerfile
- 4.2配置ngin.conf
- 4.3将下列文件放入/opt/lnmp/nginx目录
- 4.4创建html目录解压wordpress
- 5.mysql
- 5.1编写mysql的Dockerfile
- 5.2配置my.cnf文件
- 5.3将下列文件放入/opt/lnmp/mysql目录
- 6.php
- 6.1编写php的Dockerfile
- 6.2配置www.conf
- 6.3配置vim php.ini
- 6.4vim php-fpm.conf
- 6.5将下列文件放入目录/opt/lnmp/php
- 7.上传docker-compose文件至/usr/local/bin目录下
- 8.启动
- 9.1进入数据库
- 9.2数据库配置
- 9.3验证配置
- 10.wordpress网页登录配置
部署步骤:
1. 安装 Docker 和 Docker Compose
在 CentOS 7 上,首先安装 Docker 和 Docker Compose。
1.1安装 Docker:
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce-20.10.18 docker-ce-cli-20.10.18 containerd.io-20.10.18
systemctl enable --now docker
1.2安装 Docker Compose:
cd /usr/local/bin
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
2.创建目录结构
创建目录以存储 配置文件和网站内容:
cd /opt
mkdir -p /lnmp/nginx /lnmp/mysql /lnmp/php
3.编写docker-compose.yml
cd /opt/lnmp
vim /opt/docker-compose.yml
version: '3'
services:
nginx:
container_name: nginx
hostname: nginx
build:
context: /opt/lnmp/nginx/
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- /opt/lnmp/nginx/html:/usr/local/nginx/html
networks:
lnmp:
ipv4_address: 172.18.0.10
version: '3'
services:
nginx:
container_name: nginx
hostname: nginx
build:
context: /opt/lnmp/nginx/
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- /opt/lnmp/nginx/html:/usr/local/nginx/html
networks:
lnmp:
ipv4_address: 172.18.0.10
mysql:
container_name: mysql
build:
context: /opt/lnmp/mysql
dockerfile: Dockerfile
ports:
- "3306:3306"
networks:
lnmp:
ipv4_address: 172.18.0.22
volumes:
- db-data:/usr/local/mysql
php:
container_name: php
build:
context: /opt/lnmp/php
dockerfile: Dockerfile
ports:
- "9000:9000"
networks:
lnmp:
ipv4_address: 172.18.0.30
volumes:
- db-data:/usr/local/mysql
- /opt/lnmp/nginx/html:/usr/local/nginx/html
depends_on:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
volumes:
db-data:
4.nginx
4.1编写nginx的Dockerfile
cd /myproject/nginx
vim Dockerfile
FROM centos:7
MAINTAINER <ztw-nginx>
ADD CentOS-Base.repo /etc/yum.repos.d/
RUN yum clean all && yum makecache && \
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 -j4 && 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 443
ENTRYPOINT ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
4.2配置ngin.conf
cd /opt/lnmp/nginx
vim nginx.conf
location ~ \.php$ {
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;
}
4.3将下列文件放入/opt/lnmp/nginx目录
nginx-1.12.0.tar.gz
wordpress-4.9.4-zh_CN.tar.gz
CentOS-Base.repo
4.4创建html目录解压wordpress
mkdir -p /opt/lnmp/nginx/html
tar zxvf /opt/lnmp/nginx/wordpress-4.9.4-zh_CN.tar.gz -C /opt/lnmp/nginx/html
5.mysql
5.1编写mysql的Dockerfile
cd /opt/lump/mysql
vim Dockerfile
FROM centos:7
MAINTAINER <ztw-php>
FROM centos:7
ADD CentOS-Base.repo /etc/yum.repos.d/
RUN yum clean all && yum makecache fast && \
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make perl wget openssl openssl-devel libaio libaio-devel
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 -j$(nproc) && make install
ADD my.cnf /etc/my.cnf
ENV PATH="/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH"
RUN useradd -M -s /sbin/nologin mysql && \
chown mysql:mysql /etc/my.cnf && \
chown -R mysql:mysql /usr/local/mysql/RUN /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld", "--user=mysql", "--console"]
5.2配置my.cnf文件
cd /opt/lnmp/mysql
vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
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
5.3将下列文件放入/opt/lnmp/mysql目录
mysql-boost-5.7.20.tar.gz
CentOS-Base.repo
my.cnf
6.php
6.1编写php的Dockerfile
ADD CentOS-Base.repo /etc/yum.repos.d/
RUN yum clean all && yum makecache fast && \
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 && \
yum clean all && rm -rf /var/cache/yum
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"]
6.2配置www.conf
cd /opt/lnmp/php
vim www.conf
user = nginx
group = nginx
listen = 172.19.0.30:9000
6.3配置vim php.ini
cd /opt/lnmp/php
vim php.ini
date.timezone = Asia/Shanghai
mysqli.default_socket = /usr/local/mysql/mysql.sock
6.4vim php-fpm.conf
cd /opt/lnmp/php
vim php-fpm.conf
pid = run/php-fpm.pid
6.5将下列文件放入目录/opt/lnmp/php
CentOS-Base.repo
php-7.1.10.tar.bz2
php-fpm.conf
php.ini
www.conf
7.上传docker-compose文件至/usr/local/bin目录下
cd /usr/local/bin
chmod +x docker-compose
8.启动
cd /opt/lnmp
docker-compose -f docker-compose.yml up -d
9.进入容器数据库设置mysql密码和权限
9.1进入数据库
docker ps -a
docker exec -it b690cce7f45d /bin/bash
9.2数据库配置
mysql
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
CREATE DATABASE wordpress;
FLUSH PRIVILEGES;
exit;
9.3验证配置
mysql -u root -p123456
show databases;
exit
mysql -u root -p888888
10.wordpress网页登录配置
192.168.88.90/wordpress/index.php
参考步骤7
https://blog.csdn.net/2301_80613136/article/details/142905803?fromshare=blogdetail&sharetype=blogdetail&sharerId=142905803&sharerefer=PC&sharesource=2301_80613136&sharefrom=from_link