一、docker部署lnmp
l linux
n nginx 1.22 172.111.0.10 docker–nginx
m mysql 8.0.30 172.111.0.20 docker–mysql
p php 8.1.27 172.111.0.30 docker–php
docker:单节点部署,在一台机器上部署,跨了机器容器无法通信,做高可用只能在节点上增加同一服务的容器数量。
1、docker 容器编译nginx
[root@docker1 opt]# mkdir nginx mysql php
[root@docker1 opt]# ls
mysql nginx php
[root@docker1 opt]# cd nginx
[root@docker1 nginx]# ls
nginx-1.22.0.tar.gz wordpress-6.5.2-zh_CN.zip
[root@docker1 nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx <lnmp>
RUN rm -rf /etc/yum.repos.d/*
ADD http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/Centos-7.repo
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
ADD nginx-1.22.0.tar.gz /usr/local/src
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.22.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
COPY nginx.conf /usr/local/nginx/conf
ADD wordpress-6.4.2-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;"]
[root@docker1 nginx]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
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.111.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@docker1 nginx]# docker build -t nginx:lnmp .
[root@docker1 nginx]# docker network create --subnet="172.111.0.0/16" --opt "com.docker.network.bridge.name"="docker1" mynetwrk ##创建自定义网络
[root@docker1 nginx]# docker run -itd --name nginx -p 80:80 -m 512m -v /opt/html:/opt/nginx --net mynetwrk --ip 172.111.0.10 nginx:lnmp
[root@docker1 nginx]# docker exec -it nginx bash
[root@351ddcb73a6a nginx-1.22.0]# ls
CHANGES LICENSE README conf contrib man src
CHANGES.ru Makefile auto configure html objs
[root@351ddcb73a6a nginx-1.22.0]# yum -y install net-tools
[root@351ddcb73a6a nginx-1.22.0]# netstat -antp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
[root@dbdc336a4949 nginx]# cd /usr/local/nginx/
[root@dbdc336a4949 nginx]# cd html/
[root@dbdc336a4949 html]# ls
50x.html index.html wordpress
[root@dbdc336a4949 html]# echo 123 > index.html
[root@dbdc336a4949 html]# cat index.html
123
[root@dbdc336a4949 html]# pwd
/usr/local/nginx/html
2、docker容器编译mysql
[root@docker1 opt]# cd mysql/
[root@docker1 mysql]# rz -E
rz waiting to receive.
[root@docker1 mysql]# ls
mysql-8.0.30-el7-x86_64.tar.gz
[root@docker1 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 = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[root@docker1 mysql]# ls
Dockerfile my.cnf mysql-8.0.30-el7-x86_64.tar.gz
[root@docker1 mysql]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql<lnmp>
RUN rm -rf /etc/yum.repos.d/*
ADD http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/Centos-7.repo
RUN yum -y install libaio numactl-libs pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-8.0.30-el7-x86_64.tar.gz /usr/local
RUN mv /usr/local/mysql-8.0.30-el7-x86_64 /usr/local/mysql
COPY my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
VOLUME ["/usr/local/mysql"]
ENTRYPOINT ["./mysqld"]
[root@docker1 mysql]# docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwrk --ip 172.111.0.20 mysql:lnmp
[root@docker1 mysql]# docker exec -it mysql bash
[root@aca49a030c7f bin]# mysql -u root -p
Enter password:
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER 'wordpress'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
3、docker容器编译php
[root@docker1 opt]# cd php/
[root@docker1 php]# ls
[root@docker1 php]# rz -E
rz waiting to receive.
[root@docker1 php]# ls
php-8.1.27.tar.gz
[root@docker1 php]# rz -E
rz waiting to receive.
[root@docker1 php]# rz -E
rz waiting to receive.
[root@docker1 php]# rz -E
rz waiting to receive.
[root@docker1 php]# ls
php-8.1.27.tar.gz php-fpm.conf php.ini www.conf
[root@docker1 php]# vim Dockerfile
ADD php-8.1.27.tar.gz /usr/local/src/
WORKDIR /usr/local/src/php-8.1.27
--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-pdo-mysql \
--with-openssl \
--with-sqlite-devel \
--with-oniguruma-devel \
--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
COPY php.ini /usr/local/php/lib/
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm","-F"]
[root@docker1 php]# docker build -t php:lnmp .
[root@docker1 php]# docker run -itd --name php --net mynetwrk --ip 172.111.0.30 --volumes-from nginx --volumes-from mysql php:lnmp
[root@docker1 php]# docker exec -it php bash
[root@f3473dee8383 php-8.1.27]# cd /usr/local/
[root@f3473dee8383 local]# ls
bin games lib libexec nginx sbin src
etc include lib64 mysql php share
[root@f3473dee8383 local]# cd mysql/
[root@f3473dee8383 mysql]# ls
LICENSE bin docs lib mysql.sock mysqld.pid support-files
README data include man mysql.sock.lock share
[root@f3473dee8383 mysql]# cd ..
[root@f3473dee8383 local]# cd nginx/
[root@f3473dee8383 nginx]# ls
html
[root@f3473dee8383 nginx]# cd html/
[root@f3473dee8383 html]# ls
50x.html index.html wordpress
[root@f3473dee8383 html]# yum -y install net-tools
[root@f3473dee8383 html]# netstat -antp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1/php-fpm: master p
[root@docker1 mysql]# docker exec -it nginx bash
[root@dbdc336a4949 nginx-1.22.0]# tail -f /usr/local/nginx/logs/error.log
2024/08/16 04:57:10 [error] 7#0: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.168.168, server: localhost, request: "GET /wordpress/ HTTP/1.1", upstream: "fastcgi://172.111.0.30:9000", host: "192.168.168.71"
[root@docker1 opt]# cd php/
[root@docker1 php]# ls
Dockerfile php-8.1.27.tar.gz php-fpm.conf php.ini www.conf
[root@docker1 php]# vim www.conf
listen = 172.111.0.30:9000
[root@docker1 php]# docker build -t php:lnmp .
[root@docker1 php]# docker run -itd --name php --net mynetwrk --ip 172.111.0.30 --volumes-from nginx --volumes-from mysql php:lnmp