docker部署LNMP

目录

  • 制作nginx镜像
  • 制作mysql镜像
  • 制作php镜像
  • 通过nginx镜像生成容器

拉取centos镜像

[root@localhost ~]# docker pull centos

制作nginx镜像

拉取centos镜像

[root@localhost ~]# docker run -it --name nginx centos

[root@aea41379dd40 /]# yum -y install vim wget make  pcre-devel openssl openssl-devel gd-devel gcc gcc-c++

[root@aea41379dd40 /]# useradd -r -M -s /sbin/nologin nginx
[root@aea41379dd40 /]#  mkdir -p /var/log/nginx
[root@aea41379dd40 /]# chown -R nginx.nginx /var/log/nginx

[root@aea41379dd40 /]# cd /usr/src/

[root@aea41379dd40 /]# wget https://nginx.org/download/nginx-1.21.4.tar.gz

[root@aea41379dd40 /]# tar xf nginx-1.21.4.tar.gz

[root@aea41379dd40 src]# cd nginx-1.21.4
[root@aea41379dd40 nginx-1.24.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@aea41379dd40 nginx-1.24.1]# make && make install


[root@aea41379dd40 nginx-1.24.1]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.php index.html index.htm;	#添加index.php
        }
        
#取消注释
        location ~ \.php$ {						
            root           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@aea41379dd40 nginx-1.24.1]# source /etc/profile.d/nginx.sh

[root@aea41379dd40 /]# echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
[root@aea41379dd40 /]# vim /usr/local/bin/start.sh
#/bin/bash

/usr/local/nginx/sbin/nginx

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/usr/local/bin/start.sh"]' -p nginx neawalke/nginx:v1.0

[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED        SIZE
neawalke/nginx   v1.21     5078a88cfd75   4 hours ago    576MB
busybox          latest    d23834f29b38   3 days ago     1.24MB
centos           latest    5d0da3dc9764   2 months ago   231MB

制作mysql镜像

[root@localhost ~]# docker run -it --name mysql  centos
       
[root@localhost ~]# docker cp mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mysql:/usr/src

[root@ee9e6ef3ff14 /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs numactl libaio* vim make wget


[root@ee9e6ef3ff14 ~]# useradd -r -M -s /sbin/nologin mysql

[root@ee9e6ef3ff14 ~]# cd /usr/src/
[root@ee9e6ef3ff14 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[root@ee9e6ef3ff14 src]# cd /usr/local/
[root@ee9e6ef3ff14 local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@ee9e6ef3ff14 local]# chown  -R mysql.mysql mysql/

[root@ee9e6ef3ff14 /]# /usr/local/mysql/bin/mysqld --initialize-insecure --user mysql --datadir /opt/data
2021-12-03T21:25:26.225644Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T21:25:26.713056Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T21:25:26.820744Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T21:25:26.845909Z 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: 8872f9fc-547f-11ec-87d7-0242ac110002.
2021-12-03T21:25:26.847186Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T21:25:27.498634Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T21:25:27.629896Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

[root@ee9e6ef3ff14 /]# vim /etc/my.cnf.d/my.cnf
[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

[root@ee9e6ef3ff14 /]#  sed -i 's!^basedir=!basedir=/usr/local/mysql!g;s!^datadir=!datadir=/opt/data!g' /usr/local/mysql/support-files/mysql.server


[root@ee9e6ef3ff14 /]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/519cd96d2862.err'.
. SUCCESS! 
[root@ee9e6ef3ff14 /]# /usr/local/mysql/bin/mysql -uroot
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@ee9e6ef3ff14 local]# vi /usr/local/bin/start.sh
#!/bin/bash

/usr/local/mysql/support-files/mysql.server start
/bin/bash

[root@ee9e6ef3ff14 local]# chmod +x /usr/local/bin/start.sh

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/usr/local/bin/start.sh"]' -p mysql neawalke/mysql:v1.0
sha256:6b2ba86009edc91d791c8534a364e1e35b6386b342dd185d7ffd3e2ce8a10ddc
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
neawalke/mysql   v1.0      6b2ba86009ed   26 seconds ago   3.81GB
neawalke/nginx   v1.0      e7bac1169059   12 minutes ago   582MB
busybox          latest    d23834f29b38   4 days ago       1.24MB
centos           latest    5d0da3dc9764   2 months ago     231MB

制作php镜像

[root@localhost ~]# docker run -it --name php centos

[root@localhost ~]# docker cp php-8.1.0.tar.gz php:/usr/src

[root@c506d7a4e141 ~]# yum -y install epel-release
[root@519cd96d2862 /]# yum -y install sqlite-devel libzip-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel  libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel oniguruma  gcc gcc-c++ make libcurl-devel

[root@519cd96d2862 /]# 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@519cd96d2862 /]# cd /usr/src/
[root@519cd96d2862 src]# ls
debug  kernels	 php-8.1.0.tar.gz
[root@519cd96d2862 src]# tar xf php-8.1.0.tar.gz

[root@519cd96d2862 src]# cd php-8.1.0
[root@519cd96d2862 php-8.1.0]# ./configure --prefix=/usr/local/php --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@519cd96d2862 php-8.1.0]# make && make install

[root@c506d7a4e141 php-8.1.0]# cp /etc/php.ini /opt/
[root@c506d7a4e141 php-8.1.0]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@c506d7a4e141 php-8.1.0]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@c506d7a4e141 php-8.1.0]# chmod +x /etc/rc.d/init.d/php-fpm
[root@c506d7a4e141 php-8.1.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@c506d7a4e141 php-8.1.0]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@c506d7a4e141 php-8.1.0]# 

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

[root@c506d7a4e141 php-8.1.0]# vi /usr/local/bin/start.sh
#!/bin/bash

/etc/init.d/php-fpm start
/bin/bash

[root@c506d7a4e141 php-8.1.0]# chmod +x /usr/local/bin/start.sh

[root@15c9f0c4fc74 ~]# mkdir -p /var/www/html/
[root@15c9f0c4fc74 ~]# vi /var/www/html/index.php
<?php
  phpinfo();
?>         

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/usr/local/bin/start.sh"]' -p php neawalke/php:v1.0

通过nginx镜像生成容器

[root@localhost ~]# docker run -it --name nginx neawalke/nginx:v1.0 


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

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/usr/local/bin/start.sh"]' -p nginx neawalke/nginx:v2.0
sha256:445123fe30f6b308427136cf34166ae9ee42f0784a4e8e952abc01506c6c8bd1
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
neawalke/nginx   v2.0      445123fe30f6   6 seconds ago    582MB
neawalke/nginx   v1.0      b4171026a37e   6 minutes ago    582MB
neawalke/php     v1.0      d27c5060a079   11 minutes ago   1.59GB
neawalke/mysql   v1.0      6b2ba86009ed   44 minutes ago   3.81GB
busybox          latest    d23834f29b38   4 days ago       1.24MB
centos           latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker run -it --name nginx -p 8080:80 neawalke/nginx:v2.0 

[root@localhost ~]# docker run -it --name mysql --network container:nginx neawalke/mysql:v1.0 
Starting MySQL SUCCESS! 
[root@fb067c714da5 /]# Logging to '/opt/data/fb067c714da5.err'.
[
root@localhost ~]# docker run -it --name php --network container:nginx neawalke/php:v1.0 
Starting php-fpm  done
[root@fb067c714da5 /]#

docker部署LNMP_第1张图片

docker部署LNMP_第2张图片

你可能感兴趣的:(docker,运维,centos)