基于docker部署web站点

基于docker部署web站点

目录

基于docker部署web站点

拉取centos镜像并启动容器

容器中部署Apache服务

安装apr

安装apr-utils

安装httpd

配置apache并验证服务

另外连接一个终端进行制作镜像

启动容器

导入源码

拉取centos镜像并启动容器

 
[root@localhost ~]# docker pull centos //拉取镜像

Using default tag: latest

latest: Pulling from library/centos

Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177

Status: Image is up to date for centos:latest

docker.io/library/centos:latest

[root@localhost ~]# docker run -it --name zzh centos /bin/bash //启动容器

[root@701209f82184 /]#

容器中部署Apache服务

 
[root@701209f82184 /]# rm -rf /etc/yum.repos.d/*

[root@701209f82184 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo //配置yum源

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 2495 100 2495 0 0 19341 0 --:--:-- --:--:-- --:--:-- 19341

[root@701209f82184 /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

[root@701209f82184 /]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ //安装依赖包

Failed to set locale, defaulting to C.UTF-8

CentOS-8.5.2111 - Base - mirrors.aliyun.com 392 kB/s | 4.6 MB 00:11

CentOS-8.5.2111 - Extras - mirrors.aliyun.com 42 kB/s | 10 kB 00:00

CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 258 kB/s | 8.4 MB 00:33

Dependencies resolved.

=================================================================================================================================================================================================

Package Architecture Version Repository Size

=================================================================================================================================================================================================

Installing:

expat-devel x86_64 2.2.5-4.el8 base 55 k


[root@701209f82184 /]# useradd -rMs /sbin/nologin apache/ //创建用户

安装apr

 
[root@701209f82184 /]# wget http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz

--2022-08-11 01:53:51-- http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz

Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.226, 45.253.17.212, 45.253.17.214, ...

Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.226|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 1093896 (1.0M) [application/octet-stream]

Saving to: ‘apr-1.7.0.tar.gz’


apr-1.7.0.tar.gz 100%[=======================================================================================================>] 1.04M 191KB/s in 5.6s


2022-08-11 01:53:56 (191 KB/s) - ‘apr-1.7.0.tar.gz’ saved [1093896/1093896]

[root@701209f82184 /]# tar -xf apr-1.7.0.tar.gz -C /usr/local/src/

[root@701209f82184 /]# cd /usr/local/src/apr-1.7.0/

[root@701209f82184 apr-1.7.0]# vi configure

# $RM "$cfgfile" //注释此行

[root@701209f82184 apr-1.7.0]# ./configure --prefix=/usr/local/apr

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking target system type... x86_64-pc-linux-gnu

Configuring APR library

Platform: x86_64-pc-linux-gnu

checking for working mkdir -p... yes

APR Version: 1.7.0

[root@701209f82184 apr-1.7.0]# make && make install

make[1]: Entering directory '/usr/local/src/apr-1.7.0'

/bin/sh /usr/local/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/usr/local/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/local/src/apr-1.7.0/include/arch/unix -I/usr/local/src/apr-1.7.0/include -I/usr/local/src/apr-1.7.0/include/private -I/usr/local/src/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo

/usr/local/src/apr-1.7.0/build/mkdir.sh tools

安装apr-utils

 
[root@701209f82184 apr-1.7.0]# cd

[root@701209f82184 ~]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz

--2022-08-11 01:58:11-- https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz

Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.215, 45.253.17.211, 45.253.17.212, ...

Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.215|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 554301 (541K) [application/octet-stream]

Saving to: ‘apr-util-1.6.1.tar.gz’


apr-util-1.6.1.tar.gz 100%[=======================================================================================================>] 541.31K 193KB/s in 2.8s


2022-08-11 01:58:14 (193 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]


[root@701209f82184 ~]# tar -xf apr-util-1.6.1.tar.gz -C /usr/local/src/

[root@701209f82184 ~]# cd /usr/local/src/apr-util-1.6.1/

[root@701209f82184 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking target system type... x86_64-pc-linux-gnu

checking for a BSD-compatible install... /usr/bin/install -c

checking for working mkdir -p... yes

APR-util Version: 1.6.1

checking for chosen layout... apr-util

checking for gcc... gcc

[root@701209f82184 apr-util-1.6.1]# make && make install

make[1]: Entering directory '/usr/local/src/apr-util-1.6.1'

/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_brigade.lo -c buckets/apr_brigade.c && touch buckets/apr_brigade.lo

/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets.lo -c buckets/apr_buckets.c && touch buckets/apr_buckets.lo

/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets_alloc.lo -c buckets/apr_buckets_alloc.c && touch buckets/apr_buckets_alloc.lo

安装httpd

 
[root@701209f82184 apr-util-1.6.1]# cd

[root@701209f82184 ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz

--2022-08-11 02:00:34-- https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz

Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.217, 45.253.17.212, 45.253.17.211, ...

Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.217|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 9743277 (9.3M) [application/octet-stream]

Saving to: ‘httpd-2.4.54.tar.gz’


httpd-2.4.54.tar.gz 100%[=======================================================================================================>] 9.29M 320KB/s in 34s


2022-08-11 02:01:09 (277 KB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]


[root@701209f82184 ~]# tar -xf httpd-2.4.54.tar.gz -C /usr/local/src/

[root@701209f82184 ~]# cd /usr/local/src/httpd-2.4.54/

[root@701209f82184 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

checking for chosen layout... Apache

checking for working mkdir -p... yes

checking for grep that handles long lines and -e... /usr/bin/grep

checking for egrep... /usr/bin/grep -E

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking target system type... x86_64-pc-linux-gnu

[root@701209f82184 httpd-2.4.54]# make && make install

Making all in srclib

make[1]: Entering directory '/usr/local/src/httpd-2.4.54/srclib'

make[1]: Leaving directory '/usr/local/src/httpd-2.4.54/srclib'

Making all in os

make[1]: Entering directory '/usr/local/src/httpd-2.4.54/os'

Making all in unix

make[2]: Entering directory '/usr/local/src/httpd-2.4.54/os/unix'

make[3]: Entering directory '/usr/local/src/httpd-2.4.54/os/unix'

配置apache并验证服务

 
[root@701209f82184 httpd-2.4.54]# cd

[root@701209f82184 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh

[root@701209f82184 ~]# source /etc/profile.d/apache.sh //配置环境变量

[root@701209f82184 ~]# ln -s /usr/local/apache/include/ /usr/include/apache //映射头文件

[root@701209f82184 ~]# vi /usr/local/apache/conf/httpd.conf //去除提示信息

ServerName www.example.com:80 //将此行取消注释

[root@701209f82184 ~]# httpd

[root@701209f82184 ~]# 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 ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' zzh 701209f82184/httpd:v0.1

sha256:cc5277e8330ee1ad39571614a38204e86227dd72d28081e7389165a329681034

[root@localhost ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

701209f82184/httpd v0.1 cc5277e8330e 15 seconds ago 729MB

centos latest 5d0da3dc9764 10 months ago 231MB

启动容器

 
[root@localhost ~]# docker run -d --name web -p 80:80 -v /root/html/:/usr/local/apache/htdocs 701209f82184/httpd:v0.1 //使用镜像启动容器

127a2ac156fe80947cadac58cbbc496da5516f78433c7349e9f174bc1f3c8363

[root@localhost ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

127a2ac156fe 701209f82184/httpd:v0.1 "/usr/local/apache/b…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web

701209f82184 centos "/bin/bash" 28 minutes ago Up 28 minutes zzh

//测试

[root@localhost ~]# echo "wrq" > html/index.html

[root@localhost ~]# curl 127.0.0.1

wrq

[root@localhost ~]# rm -rf html/index.html

导入源码

 
[root@localhost ~]# rm -rf html/index.html

[root@localhost ~]# ls html

'#U670d#U52a1#U5668#U4e4b#U5bb6.url' chishenme.zip img

'#U7cbe#U54c1#U514d#U8d39#U5546#U4e1a#U6e90#U7801#U4e0b#U8f7d.url' ic.ico index.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值