基于容器搭建lnmp架构

基于容器搭建lnmp架构

构建nginx镜像

基于centos镜像创建一个新容器命名为nginx
[root@localhost ~]# docker run --name nginx -itd centos


在容器中编译安装nginx
[root@d580d5ac72a4 /]# useradd -r -M -s /sbin/nologin nginx
[root@d580d5ac72a4 /]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@d580d5ac72a4 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@localhost ~]# docker cp /usr/src/nginx-1.20.1.tar.gz d580d5ac72a4:/usr/src

[root@d580d5ac72a4 /]# mkdir -p /var/log/nginx
[root@d580d5ac72a4 /]# chown -R nginx.nginx /var/log/nginx/
[root@d580d5ac72a4 /]# cd /usr/src/
[root@d580d5ac72a4 src]# ls
debug  kernels  nginx-1.20.1.tar.gz
[root@d580d5ac72a4 src]# tar xf nginx-1.20.1.tar.gz 
[root@d580d5ac72a4 src]# ls   
debug  kernels  nginx-1.20.1  nginx-1.20.1.tar.gz
[root@d580d5ac72a4 src]# cd nginx-1.20.1
[root@d580d5ac72a4 nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@d580d5ac72a4 nginx-1.20.1]# make && make install
[root@d580d5ac72a4 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@d580d5ac72a4 nginx-1.20.1]# source /etc/profile.d/nginx.sh
[root@d580d5ac72a4 nginx-1.20.1]# nginx
[root@d580d5ac72a4 nginx-1.20.1]# ss -anlt
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*    

[root@localhost ~]# curl 172.17.0.2:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


[root@d580d5ac72a4 ~]# cd /
[root@d580d5ac72a4 /]# vi start.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/bin/bash
[root@d580d5ac72a4 /]# chmod +x start.sh

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/start.sh"]' -p nginx syblyw0806/nginx:v7.0.24
sha256:1e15f0ae852ed3fcec874d8f5ac3590ffea8b47d0200ea5f0a812215673ea838

构建mysql镜像

基于centos镜像创建一个新容器命名为mysql
[root@localhost ~]# docker run --name mysql -itd centos /bin/bash
e37b3b65bf280028c679cd384563ad536d2ec5514f27f0eab3243f47d5ba62dd
[root@localhost ~]# docker exec -it mysql /bin/bash
[root@e37b3b65bf28 /]# 

在容器中编译安装mysql
[root@e37b3b65bf28 /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs libaio*
[root@e37b3b65bf28 /]# useradd -r -M -s /sbin/nologin mysql 
[root@e37b3b65bf28 /]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)
[root@localhost ~]# docker cp /usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz e37b3b65bf28:/usr/src/

[root@e37b3b65bf28 src]# ls
debug  kernels  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@e37b3b65bf28 src]# 
[root@e37b3b65bf28 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@e37b3b65bf28 src]# cd /usr/local/
[root@e37b3b65bf28 local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql 
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/'
[root@e37b3b65bf28 local]# chown -R mysql.mysql /usr/local/mysql*
[root@e37b3b65bf28 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@e37b3b65bf28 ~]# . /etc/profile.d/mysql.sh
[root@e37b3b65bf28 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[root@e37b3b65bf28 ~]# mkdir -p /opt/data
[root@e37b3b65bf28 ~]# chown -R mysql.mysql /opt/data/
[root@e37b3b65bf28 ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
2021-12-03T16:40:23.840042Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T16:40:24.073972Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T16:40:24.112686Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T16:40:24.171768Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b6760149-5457-11ec-af6c-0242ac110003.
2021-12-03T16:40:24.172504Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T16:40:24.730235Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T16:40:24.793577Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

[root@e37b3b65bf28 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@e37b3b65bf28 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@e37b3b65bf28 ~]# ldconfig
[root@e37b3b65bf28 ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
> EOF

[root@e37b3b65bf28  /]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@e37b3b65bf28  /]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server


[root@e37b3b65bf28 ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/e37b3b65bf28.err'.
 SUCCESS! 
 [root@e37b3b65bf28 ~]# ss -anlt
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      80                  *:3306              *:*    
[root@e37b3b65bf28 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@e37b3b65bf28 ~]# cd /
[root@e37b3b65bf28 /]# touch /scripts/start.sh
[root@e37b3b65bf28 /]# vi /scripts/start.sh
#!/bin/bash
/usr/local/mysql/support-files/mysql.server start
/bin/bash


再开一个终端制作mysql镜像并用该镜像运行容器测试
[root@localhost ~]# docker commit -c 'CMD ["/scripts/start.sh"]' -p mysql syblyw/mysql:v1
sha256:a317f1d303af8d2b7c9d50571c4b76d37753c54e882226cc2c30b3c5840bfc4a
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED             SIZE
syblyw/mysql       v1        a317f1d303af   2 minutes ago       3.81GB
syblyw0806/nginx   v7.0.24   1e15f0ae852e   About an hour ago   549MB
centos             latest    5d0da3dc9764   2 months ago        231MB 

[root@localhost ~]# docker run --name mysql3 -itd --network=container:nginx-1 f648053d2c4d
a561818370cf06753cade23ec14057f19b48f1c55ffb6e4f913aa432949a50d2

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                 CREATED             STATUS          PORTS                               NAMES
a561818370cf   f648053d2c4d          "/scripts/start.sh"     15 minutes ago      Up 15 minutes                                       mysql3
e37b3b65bf28   centos                "/bin/bash"             About an hour ago   Up 25 minutes                                       mysql
2603b3feda52   syblyw0806/php:v2     "/bin/bash /start.sh"   About an hour ago   Up 9 minutes                                        php-1
49d914985d0a   syblyw0806/nginx:v2   "/bin/bash /start.sh"   About an hour ago   Up 27 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-1
5482864be145   centos                "/bin/bash"             5 hours ago         Up 15 minutes                                       php

构建php镜像

基于centos镜像创建一个新容器命名为php
[root@localhost ~]# docker run -d --name php -it centos
5482864be145bc2ea4f39acde7caab0a1396a13d5bf84f2edb9e37856e4a7d75
[root@localhost ~]# docker cp /usr/src/php-7.4.24.tar.gz 5482864be145:/usr/src


在容器中编译安装php
[root@5482864be145 /]# yum -y install epel-release
[root@5482864be145 /]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel libsqlite3x-devel php-mysqlnd  libzip-devel

[root@5482864be145 /]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

[root@5482864be145 /]# yum -y install gcc gcc-c++ make

[root@5482864be145 /]# cd /usr/src/
[root@5482864be145 src]# ls
debug  kernels  php-7.4.24.tar.gz
[root@5482864be145 src]# tar xf php-7.4.24.tar.gz -C /usr/local/       
[root@5482864be145 src]# cd /usr/local/
[root@5482864be145 local]# ls
bin  games    lib    libexec     sbin   src
etc  include  lib64  php-7.4.24  share
[root@5482864be145 local]# cd php-7.4.24/
[root@5482864be145 php-7.4.24]# ./configure --prefix=/usr/local/php7  --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif  --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix

[root@5482864be145 ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php.sh
[root@5482864be145 ~]# source /etc/profile.d/php.sh

[root@5482864be145 ~]# cd /usr/local/     
[root@5482864be145 local]# ls
bin  games    lib    libexec  php-7.4.24  share
etc  include  lib64  php7     sbin        src
[root@5482864be145 local]# cd php-7.4.24/
[root@5482864be145 php-7.4.24]# cp php.ini-production  /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@5482864be145 php-7.4.24]# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
[root@5482864be145 php-7.4.24]# chmod +x /etc/rc.d/init.d/php-fpm
[root@5482864be145 php-7.4.24]# cd /usr/local/php7/etc/
[root@5482864be145 etc]# cp php-fpm.conf.default  php-fpm.conf
[root@5482864be145 etc]# cd php-fpm.d/
[root@5482864be145 php-fpm.d]# cp www.conf.default www.conf

[root@5482864be145 ~]# /usr/local/php7/sbin/php-fpm 
[root@5482864be145 ~]# ss -anlt
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128         127.0.0.1:9000        0.0.0.0:*    

[root@5482864be145 /]# vi /usr/local/php7/etc/php-fpm.conf
daemonize = no

[root@5482864be145 ~]# cd /
[root@5482864be145 /]# vi start.sh
#!/bin/bash
/usr/local/php7/sbin/php-fpm
/bin/bash
[root@5482864be145 /]# chmod +x /start.sh

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/start.sh"]' -p php syblyw0806/php7:v7.0.24
sha256:265300694746a0c1eb33a583f6de0d2823f585e68ae9c514abea3bdc19ea0206
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
syblyw0806/mysql   v1        2c593331b208   44 seconds ago   3.81GB
syblyw0806/nginx   v7.0.24   1e15f0ae852e   17 minutes ago   549MB
syblyw0806/php7    v7.0.24   265300694746   19 minutes ago   1.47GB
centos             latest    5d0da3dc9764   2 months ago     231MB

组合

修改nginx配置文件,制作v2版本

[root@d580d5ac72a4 /]# vi /usr/local/nginx/conf/nginx.conf
...
        location / {
            root   html;
            index  index.php index.html index.htm;			//加入index.php
        }
...
        location ~ \.php$ {
            root           /var/www/html;		//修改
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;		//修改
            include        fastcgi_params;
        }
...

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/start.sh"]' -p nginx syblyw0806/nginx:v2
sha256:cdaaf9dfbcdf948327acf703c31dd9850d98a62eb19d7becd306384c43518f33
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
syblyw0806/mysql   v1        2c593331b208   44 seconds ago   3.81GB
syblyw0806/nginx   v2        cdaaf9dfbcdf   6 minutes ago    549MB
syblyw0806/nginx   v7.0.24   1e15f0ae852e   17 minutes ago   549MB
syblyw0806/php7    v7.0.24   265300694746   19 minutes ago   1.47GB
centos             latest    5d0da3dc9764   2 months ago     231MB

制作v2版本php镜像

[root@49d914985d0a /]# mkdir  -p /var/www/html
[root@49d914985d0a /]# cd /var/www/html/
[root@49d914985d0a html]# vi index.php
<?php
  phpinfo();
?>
[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/start.sh"]' -p php syblyw0806/php:v2
sha256:a053763cd7b8c3a6df39e4d5ae2b3070aa66bcfae04d4e08754ec0e5e5347525

[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED             SIZE
syblyw/mysql       v1        a317f1d303af   49 minutes ago      3.81GB
syblyw0806/php     v2        a053763cd7b8   About an hour ago   1.47GB
syblyw0806/nginx   v2        cdaaf9dfbcdf   2 hours ago         549MB
syblyw0806/nginx   v7.0.24   1e15f0ae852e   2 hours ago         549MB
syblyw0806/php7    v7.0.24   265300694746   2 hours ago         1.47GB
centos             latest    5d0da3dc9764   2 months ago        231MB

使用container模式使容器间通信

[root@localhost ~]# docker run -itd --name nginx-1 -p 80:80 syblyw0806/nginx:v2
49d914985d0a775a6096f4c8fd00f566307ec4edb403606e4ddae38e8ab2668a
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                 CREATED         STATUS         PORTS                               NAMES
49d914985d0a   syblyw0806/nginx:v2   "/bin/bash /start.sh"   5 seconds ago   Up 3 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-1
5482864be145   centos                "/bin/bash"             3 hours ago     Up 3 hours                                         php
d580d5ac72a4   centos                "/bin/bash"             5 hours ago     Up 4 hours                                         nginx
[root@localhost ~]# docker run -d --name php-1 --network container:nginx-1 syblyw0806/php:v2
2603b3feda524068278a825ddb5ff77914a24758fec1aee263d022831cd2ce38

[root@localhost ~]# ss -anlt
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port 
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*    
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*    
LISTEN 0      128              [::]:80             [::]:*    
LISTEN 0      128              [::]:22             [::]:*    
[root@localhost ~]# docker exec -it  php-1 /bin/bash
[root@49d914985d0a /]# 
[root@49d914985d0a /]# 
[root@49d914985d0a /]# ss -anlt
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port        Process        
LISTEN        0             128                         0.0.0.0:80                        0.0.0.0:*                          
LISTEN        0             128                       127.0.0.1:9000                      0.0.0.0:*                          
LISTEN        0             80                                *:3306                            *:*                          

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值