基于容器的方式源码编译apache并且制作镜像

一.配置docker

安装docker

[root@localhost yum.repos.d]# yum -y install wget vim    //安装wget和vim
[root@localhost yum.repos.d]# ls
CentOS-Base.repo   epel.repo                  epel-testing.repo
epel-modular.repo  epel-testing-modular.repo  redhat.repo
[root@localhost yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo   //下载docker源
[root@localhost yum.repos.d]# ls
CentOS-Base.repo   epel.repo                  redhat.repo
docker-ce.repo     epel-testing-modular.repo
epel-modular.repo  epel-testing.repo
[root@localhost ~]# dnf makecache     //建立缓存
[root@localhost ~]# dnf -y install docker-ce   //安装docker-ce

配置docker加速器

[root@localhost ~]# ls /etc/docker   //查看配置文件
ls: 无法访问'/etc/docker': 没有那个文件或目录
[root@localhost ~]# systemctl enable --now docker    //开启docker,并且加入开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vend>
   Active: active (running) since Sun 2022-04-24 20:26:50 CST
[root@localhost ~]# ls /etc/docker     //再次查看,则已经有了配置文件
key.json

[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ls
key.json
[root@localhost docker]# vim daemon.json
[root@localhost docker]# cat daemon.json 
{
          "registry-mirrors": ["https://onmth88j.mirror.aliyuncs.com"]
}
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker

二.创建容器

1.拉取contos镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    beae173ccac6   3 months ago   1.24MB
httpd        latest    dabbfbe0c57b   4 months ago   144MB
[root@localhost ~]# docker pull centos    //到镜像库中拉取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
busybox      latest    beae173ccac6   3 months ago   1.24MB
httpd        latest    dabbfbe0c57b   4 months ago   144MB
centos       latest    5d0da3dc9764   7 months ago   231MB
[root@localhost ~]# docker run -it --name httpd centos /bin/bash     //用centos镜像配置httpd容器
[root@e6c48f0765b0 /]#

2.下载Apache源码,并且复制到容器中

//开启另一台终端
[root@localhost ~]# docker ps -a  //查看所有容器
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS                          PORTS     NAMES
e6c48f0765b0   centos    "/bin/bash"          About a minute ago   Exited (0) About a minute ago             httpd

//下载源码包
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg   apr-util-1.6.1.tar.gz
apr-1.7.0.tar.gz  httpd-2.4.53.tar.gz

//复制源码包到容器中
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp httpd-2.4.53.tar.gz httpd:/usr/src/

3.准备工作

//查看复制到容器的源码包
[root@e6c48f0765b0 /]# ls /usr/src/               
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
 
//更换本地源为阿里源
[root@e6c48f0765b0 /]# cd /etc/yum.repos.d/
[root@e6c48f0765b0 yum.repos.d]# ls
CentOS-Linux-AppStream.repo          CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo              CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo             CentOS-Linux-Sources.repo
[root@e6c48f0765b0 yum.repos.d]# rm -rf *
[root@e6c48f0765b0 yum.repos.d]# ls
[root@e6c48f0765b0 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@e6c48f0765b0 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@e6c48f0765b0 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@e6c48f0765b0 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@e6c48f0765b0 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@e6c48f0765b0 yum.repos.d]# yum clean all

//下载开发工具包
[root@e6c48f0765b0 /]# yum groups mark install 'Development Tools' -y

//创建一个名为apache的系统用户,并且不生成家目录,拒绝登录
[root@e6c48f0765b0 /]# useradd -r -M -s /sbin/nologin apache
[root@e6c48f0765b0 /]# id apache 
uid=998(apache) gid=996(apache) groups=996(apache)

//下载依赖包
[root@e6c48f0765b0 /]# yum -y install openssl-devel pcre-devel expat-devel libtool make
[root@e6c48f0765b0 /]# cd /usr/src/

//解压源码包
[root@e6c48f0765b0 src]# ls
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@e6c48f0765b0 src]# tar -xf apr-1.7.0.tar.gz 
[root@e6c48f0765b0 src]# tar -xf apr-util-1.6.1.tar.gz 
[root@e6c48f0765b0 src]# tar -xf httpd-2.4.53.tar.gz   
[root@e6c48f0765b0 src]# ls
apr-1.7.0         apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
apr-1.7.0.tar.gz  debug                  kernels
apr-util-1.6.1    httpd-2.4.53

4.编译安装httpd

[root@e6c48f0765b0 src]# cd apr-1.7.0
[root@e6c48f0765b0 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@e6c48f0765b0 apr-1.7.0]# make && make install
[root@e6c48f0765b0 src]# cd apr-util-1.6.1
[root@e6c48f0765b0 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@e6c48f0765b0 apr-util-1.6.1]# make && make install
[root@e6c48f0765b0 src]# cd httpd-2.4.53
[root@e6c48f0765b0 httpd-2.4.53]# ./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
[root@e6c48f0765b0 httpd-2.4.53]# make && make install

5.配置httpd

//修改环境变量
[root@e6c48f0765b0 httpd-2.4.53]# ls /usr/local/apache/
bin    cgi-bin  error   icons    logs  manual
build  conf     htdocs  include  man   modules
[root@e6c48f0765b0 httpd-2.4.53]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@e6c48f0765b0 httpd-2.4.53]# source /etc/profile.d/apache.sh
[root@e6c48f0765b0 httpd-2.4.53]# which httpd
/usr/local/apache/bin/httpd

//设置头文件软连接
[root@e6c48f0765b0 ~]# ln -s /usr/local/apache/include/ /usr/include/apache

//取消警告信息
[root@e6c48f0765b0 ~]# cd /usr/local/apache/conf/
[root@e6c48f0765b0 conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@e6c48f0765b0 conf]# vim httpd.conf 
ServerName www.example.com:80   //取消这一行的注释

//开启Apache
[root@e6c48f0765b0 conf]# apachectl start
[root@e6c48f0765b0 conf]# ss -antl    //查看端口,默认为80端口
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 ["/bin/bash","/start.sh"]' e6c48f0765b0 yyds/httpd:v0.1  
sha256:8839d0075a14926a11d5e214cf55d0940e2508c7eae5cc10a833757fd7b893b8
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
yyds/httpd    v0.1      8839d0075a14   16 seconds ago   728MB
busybox      latest    beae173ccac6   3 months ago     1.24MB
httpd        latest    dabbfbe0c57b   4 months ago     144MB
centos       latest    5d0da3dc9764   7 months ago     231MB

//使用制作的镜像创建容器
[root@localhost ~]# docker run -dit -p 80:80 --name test yyds/httpd:v0.1 /bin/bash
846af4ffe9fd824c7cba7efcdf16bceaacec24ca335f410d90180b6ad0c11bd3

//开启容器,并且开启Apache服务
[root@localhost ~]# docker exec -it test /bin/bash
[root@846af4ffe9fd /]# apachectl start
[root@846af4ffe9fd /]# 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@846af4ffe9fd /]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE            COMMAND       CREATED         STATUS          PORTS                               NAMES
846af4ffe9fd   zsh/httpd:v0.1   "/bin/bash"   5 minutes ago   Up 5 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp   test

输入本机ip即可查看Apache默认网页

效果图:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值