基于容器的方式编译安装httpd

基于容器的方式编译安装httpd

一. 编译安装httpd

1. 拉取centos镜像,使用centos镜像创建容器
[root@SYL4 ~]# 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@SYL4 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
xiaoyinguhong/httpd   2060.1    06493b8526eb   22 hours ago   1.24MB
httpd                 2.4.53    c30a46771695   6 days ago     144MB
busybox               latest    beae173ccac6   3 months ago   1.24MB
httpd                 latest    dabbfbe0c57b   4 months ago   144MB
centos                latest    5d0da3dc9764   7 months ago   231MB
[root@SYL4 ~]# 
[root@SYL4 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
xiaoyinguhong/httpd   2060.1    06493b8526eb   22 hours ago   1.24MB
httpd                 2.4.53    c30a46771695   6 days ago     144MB
busybox               latest    beae173ccac6   3 months ago   1.24MB
httpd                 latest    dabbfbe0c57b   4 months ago   144MB
centos                latest    5d0da3dc9764   7 months ago   231MB
[root@SYL4 ~]# docker run -it --name c1 centos /bin/bash
[root@6a41da7c72b8 /]# cd
[root@6a41da7c72b8 ~]# 

2. 将本地下载的包传到httpd容器
[root@SYL4 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@SYL4 ~]# mkdir muchen
[root@SYL4 ~]# mv *.gz muchen
[root@SYL4 ~]# ls
anaconda-ks.cfg  muchen
[root@SYL4 ~]# docker cp muchen c1:/usr/src/
[root@SYL4 ~]# 
3. 在httpd容器中解压
[root@6a41da7c72b8 ~]# cd /usr/src/muchen/
[root@6a41da7c72b8 muchen]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@6a41da7c72b8 muchen]# tar xf apr-1.7.0.tar.gz 
[root@6a41da7c72b8 muchen]# tar xf apr-util-1.6.1.tar.gz 
[root@6a41da7c72b8 muchen]# tar xf httpd-2.4.53.tar.gz 
[root@6a41da7c72b8 muchen]# ls
apr-1.7.0         apr-util-1.6.1         httpd-2.4.53
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@6a41da7c72b8 muchen]# 

4. 创建apache的系统用户和组
-r 创建系统用户 -M 不要家目录 -s 不允许登录
[root@6a41da7c72b8 muchen]# useradd -r -M -s /sbin/nologin apache
[root@6a41da7c72b8 muchen]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
[root@6a41da7c72b8 muchen]# 

5. 配置yum源
[root@6a41da7c72b8 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0  12794      0 --:--:-- --:--:-- --:--:-- 12794
[root@6a41da7c72b8 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@6a41da7c72b8 yum.repos.d]# ls
CentOS-Base.repo
[root@6a41da7c72b8 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@6a41da7c72b8 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@6a41da7c72b8 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@6a41da7c72b8 yum.repos.d]# yum clean all
[root@6a41da7c72b8 yum.repos.d]# yum makecache
[root@6a41da7c72b8 yum.repos.d]# yum list all
6. 安装编译器
[root@6a41da7c72b8 ~]# yum -y install vim make gcc gcc-c++
7. 编译安装apr,修改apr配置文件
安装apr
[root@6a41da7c72b8 ~]# cd /usr/src/muchen/
[root@6a41da7c72b8 muchen]# ls
apr-1.7.0         apr-util-1.6.1         httpd-2.4.53
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@6a41da7c72b8 muchen]# cd apr-1.7.0
[root@677331c49a2b apr-1.7.0]# vim configure         
[root@677331c49a2b apr-1.7.0]# cat configure|grep cfgfile
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"//将此行注释掉
[root@677331c49a2b apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@677331c49a2b apr-1.7.0]# nproc
4
[root@677331c49a2b apr-1.7.0]# make -j 4
[root@677331c49a2b apr-1.7.0]# make install
8. 编译安装apr-util
[root@677331c49a2b apr-1.7.0]# cd ..
[root@677331c49a2b src]# cd apr-util-1.6.1
[root@677331c49a2b apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@677331c49a2b apr-util-1.6.1]# make -j 4
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory//expat.h:没有这样的文件或目录 
[root@6a41da7c72b8 apr-util-1.6.1]# yum -y install expat-devel
重新编译
[root@6a41da7c72b8 muchen]# tar xf apr-util-1.6.1.tar.gz 
[root@6a41da7c72b8 muchen]# cd apr-util-1.6.1
[root@6a41da7c72b8 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr 
[root@6a41da7c72b8 apr-util-1.6.1]# make -j 4
9. 编译安装httpd
[root@6a41da7c72b8 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=worker//用worker模式
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/pcre不可用
[root@6a41da7c72b8 httpd-2.4.53]# yum -y install pcre-devel
[root@6a41da7c72b8 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=worker   //再次编译
checking for OpenSSL... checking for user-provided OpenSSL base directory... none
checking for OpenSSL version >= 0.9.8a... FAILED
[root@6a41da7c72b8 httpd-2.4.53]# yum -y install openssl-[root@6a41da7c72b8 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=worker
[root@6a41da7c72b8 httpd-2.4.53]# make -j 4
[root@6a41da7c72b8 httpd-2.4.53]# make install

二. 第一种方法 编写脚本

1. 编写启动脚本
[root@6a41da7c72b8 ~]# ss -antl
State  Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
[root@6a41da7c72b8 ~]# cd /
[root@6a41da7c72b8 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@6a41da7c72b8 /]# vim entrypoint.sh
[root@6a41da7c72b8 /]# cat entrypoint.sh 
#!/bin/bash

/usr/local/apache/bin/httpd && sleep 5d//休息5天
[root@6a41da7c72b8 /]# chmod +x entrypoint.sh 
[root@6a41da7c72b8 /]# ls -l entrypoint.sh 
-rwxr-xr-x. 1 root root 53 Apr 27 08:09 entrypoint.sh
[root@6a41da7c72b8 /]# 
[root@6a41da7c72b8 /]# ./entrypoint.sh 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd (pid 36986) already running
^C
[root@6a41da7c72b8 /]# 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@6a41da7c72b8 /]# ./entrypoint.sh 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd (pid 36986) already running

2. 访问一下
[root@SYL4 ~]# docker inspect c1 
[root@SYL4 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@SYL4 ~]# 
3. 制作镜像
[root@SYL4 ~]# docker commit -a 'xiaoyinguhong <2807689558@qq.com>' -c 'CMD ["/entrypoint.sh"]' -p c1 xiaoyinguhong/httpd:2060.2
sha256:cb99f86de5ef6bb6da6cc6cfaf65c44cf272d4956b5afee7cee9942f68d8fcd2
[root@SYL4 ~]# 
[root@SYL4 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/httpd   2060.2    cb99f86de5ef   34 seconds ago   720MB
xiaoyinguhong/httpd   2060.1    06493b8526eb   24 hours ago     1.24MB
httpd                 2.4.53    c30a46771695   6 days ago       144MB
busybox               latest    beae173ccac6   3 months ago     1.24MB
httpd                 latest    dabbfbe0c57b   4 months ago     144MB
centos                latest    5d0da3dc9764   7 months ago     231MB
4. 运行制作的镜像
[root@SYL4 ~]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:22                0.0.0.0:*                    
LISTEN     0          128                     [::]:22                   [::]:*                    
[root@SYL4 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS          PORTS     NAMES
6a41da7c72b8   centos    "/bin/bash"   3 hours ago   Up 45 minutes             c1
[root@SYL4 ~]# docker run -d --name web -p 80:80 xiaoyinguhong/httpd:2060.2
3a5a452f9df3d565cd3a3816d947778a0d97f5b2b74ac953ed6a66a11dc1bc48
[root@SYL4 ~]# docker ps -a
CONTAINER ID   IMAGE                        COMMAND            CREATED          STATUS          PORTS                               NAMES
3a5a452f9df3   xiaoyinguhong/httpd:2060.2   "/entrypoint.sh"   17 seconds ago   Up 13 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web
6a41da7c72b8   centos                       "/bin/bash"        3 hours ago      Up 45 minutes                                       c1
[root@SYL4 ~]# 
5. 访问成功

在这里插入图片描述

6. 删除镜像中不需要的文件,再次制作镜像
删除gcc make cache目录下,安装包文件
[root@SYL4 ~]# docker commit -a 'xiaoyinguhong <2807689558@qq.com>' -c 'CMD ["/entrypoint.sh"]' -p c1 xiaoyinguhong/httpd:2060.3
sha256:16acf7cb66642ea345dfad0a5066e26df7695c3f687cdd9079dfc3e11c20e71d
[root@SYL4 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/httpd   2060.3    16acf7cb6664   11 seconds ago   383MB
xiaoyinguhong/httpd   2060.2    6d21fe0560e3   23 minutes ago   720MB
xiaoyinguhong/httpd   2060.1    06493b8526eb   25 hours ago     1.24MB
httpd                 2.4.53    c30a46771695   7 days ago       144MB
busybox               latest    beae173ccac6   3 months ago     1.24MB
httpd                 latest    dabbfbe0c57b   4 months ago     144MB
centos                latest    5d0da3dc9764   7 months ago     231MB
[root@SYL4 ~]# 

7. 上传镜像
[root@SYL4 ~]# docker push xiaoyinguhong/httpd:2060.3
The push refers to repository [docker.io/xiaoyinguhong/httpd]
403b3ff115cc: Pushed 
74ddd0ec08fa: Mounted from library/centos 
2060.3: digest: sha256:b977c13ef08a9068d5c853996038d98aa224575aca112ef90c75896108ea98c4 size: 741
[root@SYL4 ~]# 

在这里插入图片描述

8. 镜像的导入
[root@SYL4 ~]# docker save -o httpd.tar xiaoyinguhong/httpd:2060.2
[root@SYL4 ~]# ls
anaconda-ks.cfg  httpd.tar  muchen
[root@SYL4 ~]# 
[root@SYL4 ~]# scp httpd.tar root@192.168.232.129:. //传到另一台虚拟机上
root@192.168.232.129's password: 
httpd.tar                                             100%  706MB  42.8MB/s   00:16    
[root@SYL4 ~]# 

9. 镜像的导出
第二台192.168.232.129
[root@SYL2 ~]# ls
httpd.tar  mysql
[root@SYL2 ~]# docker load -i httpd.tar 
74ddd0ec08fa: Loading layer  238.6MB/238.6MB
bcd1297cdc59: Loading layer  501.4MB/501.4MB
Loaded image: xiaoyinguhong/httpd:2060.2
[root@SYL2 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/httpd     2060.2    6d21fe0560e3   46 minutes ago   720MB
xiaoyinguhong/httpd     2060.1    06493b8526eb   26 hours ago     1.24MB
xiaoyinguhong/busybox   2060.1    aa5b7f970c0c   27 hours ago     1.24MB
[root@SYL2 ~]# 

三. 第二种 设置在前台运行

1. 设置在前台运行
[root@6a41da7c72b8 ~]# ss -antl 
State    Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
[root@6a41da7c72b8 ~]# /usr/local/apache/bin/httpd -DFOREGROUND
2. 访问
[root@SYL4 ~]# docker inspect c1
[root@SYL4 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@SYL4 ~]# 
3. 做镜像
[root@SYL4 ~]# docker commit -a 'xiaoyinguhong <2807689558@qq.com>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p c1 xiaoyinguhong/httpd:2008.1
sha256:ba51b986392549742d7d5385e7ae7c0e43522730b50f4e1c70671a98ff6f6379
[root@SYL4 ~]# docker run -d --name web1 -p 80:80 xiaoyinguhong/httpd:2008.1
ca7565c23c37e6c4d3fc6346657620010f9a67e67906952e45abfdeacb62f975
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS         PORTS                               NAMES
ca7565c23c37   xiaoyinguhong/httpd:2008.1   "/usr/local/apache/b…"   12 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web1
6a41da7c72b8   centos                       "/bin/bash"              4 hours ago      Up 2 hours                                         c1
[root@SYL4 ~]# 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值