docker容器部署lnmp

1. 编译安装nginx

环境说明:
系统为centos

软件软件版本
nginx1.20.1
MySQL5.7.34
PHP8.0.12
// 拉取镜像
[root@localhost ~]# docker pull centos

// 编写脚本
[root@95da02b0b036 init.d]# pwd
/etc/init.d


[root@95da02b0b036 init.d]# cat php.sh 
#!/bin/bash
route=/usr/local
data=/opt/data


if [ ! -d $route/nginx-1.20.1 ];then
	tar xf packages/nginx-1.20.1.tar.gz -C $route
fi

if [ ! -d $route/mysql-5.7.34-linux-glibc2.12-x86_64 ];then
	tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C $route
	ln -s $route/mysql-5.7.34-linux-glibc2.12-x86_64 /usr/local/mysql
fi

if [ ! -d $route/php-8.0.12 ];then
	tar xf packages/php-8.0.12.tar.gz -C $route
fi


if [ ! -f /etc/yum.repos.d/CentOS-Base.repo ];then
	curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
fi

yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel ncurses-compat-libs perl ncurses-devel cmake epel-release libxml2 libxml2-devel  bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel

if [ ! -f ./oniguruma-devel-6.8.2-2.el8.x86_64.rpm  ];then
wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
fi

yum -y install oniguruma-devel-6.8.2-2.el8.x86_64.rpm
id nginx &>/dev/null
if [ $? -ne 0 ];then
	useradd -r -M -s /sbin/nologin nginx
fi

mkdir -p /var/log/nginx
chown -R nginx.nginx /var/log/nginx


cd $route/nginx-1.20.1
if [ ! -d $route/nginx ];then
	./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 && \
		make && make install
fi

echo 'export PATH=/usr/local/nginx/sbin:$PATH' >/etc/profile.d/nginx.sh

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx 
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx.service

将容器打包成镜像

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS          PORTS                               NAMES
eeaeea239234   ba3cecc5fc1a   "/sbin/init"   8 minutes ago    Up 8 minutes                                        php
4c86bd783fc1   b4b4b2f436eb   "/sbin/init"   9 minutes ago    Up 9 minutes                                        mysql
2ec46ede5939   dc970ba3d804   "/sbin/init"   11 minutes ago   Up 11 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
95da02b0b036   centos         "/bin/bash"    17 hours ago     Up 2 hours                                          gallant_yalow
6b6c1db159af   centos         "/bin/bash"    19 hours ago     Up 2 hours                                          compassionate_northcutt

[root@localhost ~]# docker commit -p 2ec46ede5939 dockerimages123/nginx:v4.0  //打包成镜像

2. 编译安装MySQL

[root@6b6c1db159af init.d]# pwd
/etc/init.d

[root@6b6c1db159af init.d]# cat mysql.sh 
#!/bin/bash
route=/usr/local
data=/opt/data
yum -y install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake libaio numactl

id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi

if [ ! -d $route/mysql ];then
ln -s $route/mysql-5.7.34-linux-glibc2.12-x86_64/ $route/mysql
chown -R mysql.mysql $route/mysql
fi

echo 'export PATH=$route/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

mkdir -p $data
chown -R mysql.mysql $data


look=$( ls $data | wc -l)
if [ $look -lt 2 ];then
$route/mysql/bin/mysqld --initialize-insecure --user mysql --datadir $data/
fi

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

sed -ri "s#^(basedir=).*#\1$route/mysql#g" $route/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$data#g" $route/mysql/support-files/mysql.server

cat > /usr/lib/systemd/system/mysql.service << EOF
[Unit]
Description=mysql server daemon
After=network.target

[Service]
Type=forking
ExecStart=$route/mysql/support-files/mysql.server start
ExecStop=$route/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable --now mysql

将容器打包成镜像

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS          PORTS                               NAMES
eeaeea239234   ba3cecc5fc1a   "/sbin/init"   8 minutes ago    Up 8 minutes                                        php
4c86bd783fc1   b4b4b2f436eb   "/sbin/init"   9 minutes ago    Up 9 minutes                                        mysql
2ec46ede5939   dc970ba3d804   "/sbin/init"   11 minutes ago   Up 11 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

[root@localhost ~]# docker commit -p 4c86bd783fc1 dockerimages123/mysql:v2.0

3. 编译安装PHP

[root@95da02b0b036 init.d]# pwd
/etc/init.d


[root@95da02b0b036 init.d]# cat php.sh 
#!/bin/bash
path=/usr/src
route=/usr/local

yum -y install libxml2 libxml2-devel  bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libzip-devel epel-release openssl openssl-devel make sqlite-devel libsqlite3x.x86_64 --allowerasing 

if [ ! -f ./oniguruma-devel-6.8.2-2.el8.x86_64.rpm ];then
wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
yum -y install oniguruma-devel-6.8.2-2.el8.x86_64.rpm
fi

cd $path/php-8.0.12

if [ ! -d $route/php8 ];then
	./configure --prefix=$route/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 &>/dev/null
fi


echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh

cd $path/php-8.0.12
cp php.ini-production /etc/php.ini

cd $path/php-8.0.12/sapi/fpm
cp init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/rc.d/init.d/php-fpm

cp $route/php8/etc/php-fpm.conf.default $route/php8/etc/php-fpm.conf
cp $route/php8/etc/php-fpm.d/www.conf.default $route/php8/etc/php-fpm.d/www.conf

cat > /usr/lib/systemd/system/php-fpm.service << EOF
[Unit]
Description=php server daemon
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now php-fpm

将容器打包成镜像

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS          PORTS                               NAMES
eeaeea239234   ba3cecc5fc1a   "/sbin/init"   8 minutes ago    Up 8 minutes                                        php
4c86bd783fc1   b4b4b2f436eb   "/sbin/init"   9 minutes ago    Up 9 minutes                                        mysql
2ec46ede5939   dc970ba3d804   "/sbin/init"   11 minutes ago   Up 11 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

[root@localhost ~]# docker commit -p eeaeea239234 dockerimages123/php:v2.0 

访问不了PHP界面,需要做以下操作

// 在PHP的容器里做以下操作
[root@2ec46ede5939 ~]# mkdir -p /usr/local/nginx/html

[root@2ec46ede5939 html]# cat index.php 
<?php
  phpinfo();
?>

[root@2ec46ede5939 html]# systemctl restart php-fpm.service  //重启PHP
[root@2ec46ede5939 conf]# systemctl restart nginx.service  //在nginx容器中重启nginx

当我们使用systemctl启动服务时会报错

System has not been booted with systemd as init system (PID 1). Can't operate.

启动容器,并使用systemctl控制容器的服务

[root@localhost ~]# docker commit -p 2ec46ede5939 dockerimages123/nginx:v4.0 
sha256:4e037bbbe3caf984626b60f946a587b19c4b274dab035ba95a5a35c0ca2a04d2
[root@localhost ~]# docker commit -p 4c86bd783fc1 dockerimages123/mysql:v2.0 
sha256:4e86b74d59aecf14a9f2c34382a471bc55e318d8c740a60b5a35c177608ffb72
[root@localhost ~]# docker commit -p eeaeea239234 dockerimages123/php:v2.0 
sha256:2cf524c85690fd9054eea287b7f80d574a4fbbb3543bc583c52ce1151c003778

[root@localhost ~]# docker images 
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
dockerimages123/php     v2.0      2cf524c85690   44 seconds ago   1.62GB
dockerimages123/mysql   v2.0      4e86b74d59ae   49 seconds ago   3.58GB
dockerimages123/nginx   v4.0      4e037bbbe3ca   57 seconds ago   679MB

// 用下面的方法就可以使用systemctl控制服务了。
[root@localhost ~]# docker run -itd --privileged=true --name nginx -p 80:80 4e037bbbe3ca /sbin/init  //这个地方的4e037bbbe3ca是刚生成的nginx镜像的id

[root@localhost ~]# docker run -itd --privileged=true --name mysql --network container:2ec46ede5939 4e86b74d59ae /sbin/init  //这两个id分别是刚生成的nginx镜像id和MySQL的id

[root@localhost ~]# docker run -itd --privileged=true --name php --network container:2ec46ede5939 2cf524c85690 /sbin/init  //这两个id分别是nginx镜像id和PHP镜像的id

可以通过下面的命令去获取镜像

docker pull dockerimages123/nginx:v4.0  //获取nginx镜像
docker pull dockerimages123/php:v2.0 //获取PHP镜像
docker pull dockerimages123/mysql:v2.0  // 获取MySQL镜像
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值