返回
热门搜索

Docker下部署lnmp详细步骤

IT博客 运维技术 容器 点击量 322

基本信息 收藏 - 举报 - 海报

详细介绍

目录
拉取一个centos镜像基于centos生成nginx容器基于centos生成mysql容器基于centos生成php容器制作镜像用新镜像创建lnmp容器测试

拉取一个centos镜像

//下载centos镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

//查看镜像
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   2 months ago   231MB

基于centos生成nginx容器

//使用镜像启动容器并进入容器
[root@localhost ~]# docker run -it --name centos_nginx centos /bin/bash
[root@c193fcb41eae /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim which make wget
[root@c193fcb41eae /]# yum -y groups mark install "Development Tools"

//创建用户和组
[root@c193fcb41eae /]# useradd -r -M -s /sbin/nologin nginx

//创建日志存放目录
[root@c193fcb41eae /]# mkdir -p /var/log/nginx
[root@c193fcb41eae /]# chown -R nginx.nginx /var/log/nginx

//下载安装包以及解压
[root@c193fcb41eae /]# cd /usr/src/
[root@c193fcb41eae src]# wget http://nginx.org/download/nginx-1.21.4.tar.gz
[root@c193fcb41eae src]# ls
debug  kernels  nginx-1.21.4.tar.gz
[root@c193fcb41eae src]# tar xf nginx-1.21.4.tar.gz 

//编译安装
[root@c193fcb41eae src]# cd nginx-1.21.4
[root@c193fcb41eae nginx-1.21.4]# ./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@c193fcb41eae nginx-1.21.4]# make -j $(grep "processor" /proc/cpuinfo | wc -l) && make install 

[root@c193fcb41eae nginx-1.21.4]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@c193fcb41eae nginx-1.21.4]# . /etc/profile.d/nginx.sh
[root@c193fcb41eae nginx-1.21.4]# which nginx
/usr/local/nginx/sbin/nginx

[root@localhost ~]# docker exec -it nginx05  /bin/bash
[root@c193fcb41eae /]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                                       *:80 

[root@c193fcb41eae /]# vi /usr/local/nginx/conf/nginx.conf
......
        location / {
            root   html;
            index index.php index.html index.htm;		#在45行中添加index.php
        }
......
        #取消location ~ .php$ 大括号前的注释(65~71)行
        location ~ .php$ {
            root           /var/www/html;			#php测试页面目录
            fastcgi_pass   127.0.0.1:9000;			#在工作中这里要改为php服务器的地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;			#将/script改为$document_root
            include        fastcgi_params;
        }
        ......
daemon off;			#在前台运行


#下根目录下创建start.sh
[root@c193fcb41eae /]# cat >> start.sh <

基于centos生成mysql容器

//做mysql镜像并使用容器模式的网络
[root@localhost ~]# docker run -it --name mysql01 --network container:nginx05 centos:latest /bin/bash
[root@3367881fd446 /]#     #启动本地镜像centos 在里面安装MySQL --network container:nginx05 (以nginx05容器ID为共享网络)

//重新打开一个终端查看并上传mysql包
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS          PORTS                                   NAMES
466e7f22d1f3   centos:latest                       "/bin/bash"              11 seconds ago   Up 10 seconds                                           mysql01		# mysql容器正运行
c193fcb41eae   1314444/source_nginx:nginx-1.21.4   "/usr/local/nginx/sb…"   9 hours ago      Up 34 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx05

# mysql容器正运行
在这里插入代码片[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# docker cp mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mysql01:/usr/src

//MySQL容器下查看
[root@c193fcb41eae /]# ls /usr/src/
debug  kernels  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

////创建用户和组
[root@c193fcb41eae /]# useradd -r -M -s /sbin/nologin mysql
[root@c193fcb41eae /]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)

//清理环境以及安装依赖包
[root@c193fcb41eae /]# yum clean all
[root@c193fcb41eae /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs which numactl libaio

//解压
[root@c193fcb41eae /]# cd /usr/src/
[root@c193fcb41eae src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@c193fcb41eae src]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.34-linux-glibc2.12-x86_64  sbin  share  src

//创建软连接
[root@c193fcb41eae src]# cd /usr/local/
[root@c193fcb41eae local]# ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@c193fcb41eae local]# ls -l
total 0
drwxr-xr-x 2 root root   6 Nov  3  2020 bin
drwxr-xr-x 2 root root   6 Nov  3  2020 etc
drwxr-xr-x 2 root root   6 Nov  3  2020 games
drwxr-xr-x 2 root root   6 Nov  3  2020 include
drwxr-xr-x 2 root root   6 Nov  3  2020 lib
drwxr-xr-x 3 root root  17 Sep 15 14:17 lib64
drwxr-xr-x 2 root root   6 Nov  3  2020 libexec
lrwxrwxrwx 1 root root  35 Dec  3 16:18 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 129 Dec  3 16:17 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root   6 Nov  3  2020 sbin
drwxr-xr-x 5 root root  49 Sep 15 14:17 share
drwxr-xr-x 2 root root   6 Nov  3  2020 src

//修改属主属组
[root@c193fcb41eae local]# chown -R mysql.mysql /usr/local/mysql*
[root@c193fcb41eae local]# ls -l
total 0
drwxr-xr-x 2 root  root    6 Nov  3  2020 bin
drwxr-xr-x 2 root  root    6 Nov  3  2020 etc
drwxr-xr-x 2 root  root    6 Nov  3  2020 games
drwxr-xr-x 2 root  root    6 Nov  3  2020 include
drwxr-xr-x 2 root  root    6 Nov  3  2020 lib
drwxr-xr-x 3 root  root   17 Sep 15 14:17 lib64
drwxr-xr-x 2 root  root    6 Nov  3  2020 libexec
lrwxrwxrwx 1 mysql mysql  35 Dec  3 16:18 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Dec  3 16:17 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root  root    6 Nov  3  2020 sbin
drwxr-xr-x 5 root  root   49 Sep 15 14:17 share
drwxr-xr-x 2 root  root    6 Nov  3  2020 src

//添加环境变量
[root@c193fcb41eae local]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@c193fcb41eae local]# source /etc/profile.d/mysql.sh
[root@c193fcb41eae local]# which mysql
/usr/local/mysql/bin/mysql

//头文件(include)、读取lib
[root@c193fcb41eae local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@c193fcb41eae local]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
[root@c193fcb41eae local]# ldconfig

//创建数据存放目录
[root@c193fcb41eae local]# mkdir -p /opt/data
[root@c193fcb41eae local]# chown -R mysql.mysql /opt/data/
[root@c193fcb41eae local]# ls -l /opt/data/
total 0

//初始化数据库
[root@c193fcb41eae local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-12-03T16:29:44.502079Z 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:29:44.673278Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T16:29:44.703121Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T16:29:44.759550Z 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: 395770aa-5456-11ec-a2ae-0242ac110003.
2021-12-03T16:29:44.761730Z 0 [Warning] Gtid table is not ready to be used. Table "mysql.gtid_executed" cannot be opened.
2021-12-03T16:29:45.276899Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T16:29:45.631478Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

//生成配置文件
[root@c193fcb41eae local]# cat > /etc/my.cnf < set password = password("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

//测试登录
[root@c193fcb41eae local]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
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> quit
Bye

[root@c193fcb41eae src]# rm -rf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

在初始化mysql5.7的时候,报以下错误

error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

//解决方法
yum -y install numactl libaio

基于centos生成php容器

//运行一个php容器和nginx共享网络
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS             PORTS                                   NAMES
466e7f22d1f3   centos:latest                       "/bin/bash"              52 minutes ago   Up 3 minutes                                               mysql01
c193fcb41eae   1314444/source_nginx:nginx-1.21.4   "/usr/local/nginx/sb…"   10 hours ago     Up About an hour   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx05

[root@localhost ~]# docker run -it --name php8 --network container:nginx05 centos:latest  /bin/bash		# 启动本地镜像centos 在里面安装php --network container:nginx05 (以nginx05容器ID为共享网络)
[root@c193fcb41eae /]# 

//重新打开一个终端查看正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS             PORTS                                   NAMES
22ce0738d9d7   centos:latest                       "/bin/bash"              28 seconds ago   Up 27 seconds                                              php8
466e7f22d1f3   centos:latest                       "/bin/bash"              54 minutes ago   Up 5 minutes                                               mysql01
c193fcb41eae   1314444/source_nginx:nginx-1.21.4   "/usr/local/nginx/sb…"   10 hours ago     Up About an hour   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx05

//清理环境并安装epel源
[root@c193fcb41eae /]# yum clean all
[root@c193fcb41eae /]# yum -y install epel-release

//安装依赖包
[root@c193fcb41eae /]# yum install sqlite-devel libzip-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel  libjpeg-turbo libjpeg-turbo-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 gcc gcc-c++ make which --allowerasing

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

//打开另一个终端上传php包到容器内
[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  php-8.0.11.tar.gz
[root@localhost ~]# docker cp php-8.0.11.tar.gz 22ce0738d9d7:/usr/src/

//解压
[root@c193fcb41eae /]# ls /usr/src/
debug  kernels  php-8.0.11.tar.gz
[root@c193fcb41eae /]# cd /usr/src/
[root@c193fcb41eae src]# tar xf php-8.0.11.tar.gz -C /usr/local/
[root@c193fcb41eae src]# cd /usr/local/
[root@c193fcb41eae local]# ls
bin  etc  games  include  lib  lib64  libexec  php-8.0.11  sbin  share  src

//编译并安装
[root@c193fcb41eae local]# cd php-8.0.11/
[root@c193fcb41eae php-8.0.11]# ./configure --prefix=/usr/local/php8  
--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 &&
make && make install

//添加环境变量
[root@c193fcb41eae php-8.0.11]# echo "export PATH=/usr/local/php8/bin:$PATH" > /etc/profile.d/php.sh
[root@c193fcb41eae php-8.0.11]# source /etc/profile.d/php.sh
[root@c193fcb41eae php-8.0.11]# which php
/usr/local/php8/bin/php

//配置php-fpm文件
[root@c193fcb41eae php-8.0.11]# cp php.ini-production /etc/php.ini
[root@c193fcb41eae php-8.0.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@c193fcb41eae php-8.0.11]# chmod +x /etc/init.d/php-fpm
[root@c193fcb41eae php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@c193fcb41eae php-8.0.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@c193fcb41eae /]# mkdir -p /var/www/html
[root@c193fcb41eae /]#  cat > /var/www/html/index.php <
EOF

[root@c193fcb41eae /]# vi /usr/local/php8/etc/php-fpm.conf
daemonize = no   #取消注释并设置daemonize = no,让php在前台运行

[root@c193fcb41eae /]# rm -rf /usr/src/php-8.0.11.tar.gz

[root@c193fcb41eae php-8.0.11]# /usr/local/php8/sbin/php-fpm
[root@c193fcb41eae php-8.0.11]# 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                                  *:* 

//php启动脚本
[root@c193fcb41eae /]# vi /start.sh
#!/bin/bash
/usr/local/php8/sbin/php-fpm
[root@c193fcb41eae /]# chmod +x /start.sh 


[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS          PORTS                                   NAMES
22ce0738d9d7   centos:latest                       "/bin/bash"              46 minutes ago   Up 46 minutes                                           php8
466e7f22d1f3   centos:latest                       "/bin/bash"              2 hours ago      Up 51 minutes                                           mysql01
c193fcb41eae   1314444/source_nginx:nginx-1.21.4   "/usr/local/nginx/sb…"   11 hours ago     Up 2 hours      0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx05

制作镜像

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS             PORTS                                   NAMES
b8aff1ca0d45   centos:latest            "./start.sh"             4 seconds ago    Up 3 seconds                                               php8
cb81cdc671e4   centos:latest            "./start.sh"             8 minutes ago    Up 8 minutes                                               mysql01
c193fcb41eae   nginx:latest                       "/usr/local/nginx/sb…"   11 minutes ago   Up 11 minutes      0.0.0.0:8080->80/tcp, :::8080->80/tcp       nginx05

//制作nginx镜像
[root@localhost ~]# docker commit -a " hhr 123@qq.com" -c "CMD ["/usr/local/nginx/sbin/nginx"]" nginx05 1314444/lnmp:nginx-v0.1
sha256:ae3a6692a4c0c3a615bdf08ad2941a8526c0acda2b6b6b21dfb625407280ac25

//制作mysql镜像
[root@localhost ~]# docker commit -a " hhr 123@qq.com" -c "CMD ["./start.sh"]" mysql01 1314444/lnmp:mysql-v0.1
sha256:ff7d2b91b948baf5ecde42ad6783d80b00fb4c18bc3edc3e9d907f2a7b0e8b96

//制作php镜像
[root@localhost ~]# docker commit -a " hhr 123@qq.com" -c "CMD ["./start.sh"]" php8 1314444/lnmp:php8-v0.1
sha256:359cf3ac83b0faea8d17fa4ba7eb3ed149b4ab68b8f625ed20e049d73a2c78ee

[root@localhost ~]# docker images
REPOSITORY             TAG               IMAGE ID       CREATED         SIZE
1314444/lnmp           php8-v0.1         359cf3ac83b0   7 seconds ago   1.56GB
1314444/lnmp           mysql-v0.1        ff7d2b91b948   4 minutes ago   3.81GB
1314444/lnmp           nginx-v0.1        ae3a6692a4c0   7 minutes ago   551MB
centos                 latest            5d0da3dc9764   2 months ago    231MB

用新镜像创建lnmp容器

//启动nginx容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker run -d --name nginx -p 80:80 1314444/lnmp:nginx-v0.1
fad400d5145fea77e336200b915fd2321a9ee3c14d0bf8ecb9fe112f8e2b9106
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND         CREATED          STATUS         PORTS                               NAMES
a5346a4eb92b   ae3a6692a4c0   "./start.sh"    10 seconds ago   Up 9 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

[root@localhost ~]# docker exec -it nginx /bin/bash
[root@a5346a4eb92b /]# 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:* 
 

//启动mysql容器
[root@localhost ~]# docker run -d --name mysql --network container:fad400d5145f 1314444/lnmp:mysql-v0.1
cb81cdc671e4c266ea9d1537367ab509e1c50a9c29d6741fed632ee6c3ad1ddb
   
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS          PORTS                               NAMES
861eabb3a5b7   ff7d2b91b948   "./start.sh"   35 seconds ago   Up 34 seconds                                       mysql
a5346a4eb92b   ae3a6692a4c0   "./start.sh"   7 minutes ago    Up 7 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

[root@localhost ~]# docker exec -it mysql /bin/bash
[root@a5346a4eb92b /]# ss -antl
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       80                     *:3306                *:*        



//启动php容器
[root@localhost ~]# docker run -d --name php --network container:fad400d5145f 1314444/lnmp:php-v0.1
b8aff1ca0d45033ed1f20071d98678573edec8c6c9c521fc7ddf024b01c0d0f9

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS             PORTS                                   NAMES
b8aff1ca0d45   1314444/lnmp:php-v0.1              "./start.sh"             4 seconds ago    Up 3 seconds                                               php
cb81cdc671e4   1314444/lnmp:mysql-v0.1           "./start.sh"             8 minutes ago    Up 8 minutes                                               mysql
fad400d5145f   0229f7e8313f                        "./start.sh"             11 minutes ago   Up 11 minutes      0.0.0.0:80->80/tcp, :::80->80/tcp       nginx

[root@localhost ~]# docker exec -it php /bin/bash
[root@fad400d5145f /]# 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                                    *:*  

测试


到此这篇关于Docker下部署lnmp详细步骤的文章就介绍到这了,更多相关Docker部署lnmp内容请搜索IT博客社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT博客社区!

没有更多内容。

用户评价(0)

好评度100%
  • 还没有人评论此条信息!
+ 加载更多

联系方式

提示:联系我时,请说明在看到的,谢谢!
  • 联系人:
  • 地  区:
  • 电  话: 共发布信息(2004)条 所在地:未填写
看了又看
加载中
首页 首页 收藏 收藏

电话联系