Apache编译安装制作镜像

1.利用centos镜像创建容器

//拉取centos镜像,默认拉取tag为latest的镜像,一般来说是最新的镜像
[root@localhost ~]# docker pull centos
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   7 months ago    144MB
centos       latest    5d0da3dc9764   10 months ago   231MB

//创建容器
[root@localhost ~]# docker run -it -d --name httpd centos
ea09fe54956090f82e0da0f907e0cbdb07e500133b610803d12277bc8995a70e
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
ea09fe549560   centos    "/bin/bash"   3 seconds ago   Up 3 seconds             httpd

//进入容器,并配置yum仓库
[root@localhost ~]# docker exec -it httpd /bin/bash
[root@ea09fe549560 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@ea09fe549560 /]# cd /etc/yum.repos.d/
[root@ea09fe549560 yum.repos.d]# rm -f *
[root@ea09fe549560 yum.repos.d]# ls
[root@ea09fe549560 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  10942      0 --:--:-- --:--:-- --:--:-- 10942
[root@ea09fe549560 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ea09fe549560 yum.repos.d]# dnf clean all
[root@ea09fe549560 yum.repos.d]# dnf list all

2.准备所需依赖包和安装包

[root@ea09fe549560 /]# cd ~
[root@ea09fe549560 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ vim make wget
[root@ea09fe549560 ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@ea09fe549560 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@ea09fe549560 ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz

3.编译apr

[root@ea09fe549560 ~]# tar xf apr-1.7.0.tar.gz 
[root@ea09fe549560 ~]# cd apr-1.7.0/
[root@ea09fe549560 apr-1.7.0]# vim configure
cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"	//将此行注释或删除
[root@ea09fe549560 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@ea09fe549560 apr-1.7.0]# make
[root@ea09fe549560 apr-1.7.0]# make install

4.编译apr-util

[root@ea09fe549560 ~]# tar xf apr-util-1.6.1.tar.gz 
[root@ea09fe549560 ~]# cd apr-util-1.6.1/
[root@ea09fe549560 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@ea09fe549560 apr-util-1.6.1]# make
[root@ea09fe549560 apr-util-1.6.1]# make install

5.编译httpd

[root@ea09fe549560 ~]# tar xf httpd-2.4.54.tar.gz 
[root@ea09fe549560 ~]# cd httpd-2.4.54/
[root@ea09fe549560 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
[root@ea09fe549560 httpd-2.4.54]# make
[root@ea09fe549560 httpd-2.4.54]# make install

//配置全局环境变量
[root@ea09fe549560 httpd-2.4.54]# cd /usr/local/apache/
[root@ea09fe549560 apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	man  manual  modules
[root@ea09fe549560 apache]# echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@ea09fe549560 apache]# source /etc/profile.d/httpd.sh 
[root@ea09fe549560 apache]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/apache/bin
[root@ea09fe549560 apache]# which apachectl 
/usr/local/apache/bin/apachectl

//配置头文件
[root@ea09fe549560 apache]# ln -sv /usr/local/apache/include/ /usr/include/httpd
'/usr/include/httpd' -> '/usr/local/apache/include/'
[root@ea09fe549560 apache]# ls -l /usr/include/httpd
lrwxrwxrwx. 1 root root 26 Aug  9 01:51 /usr/include/httpd -> /usr/local/apache/include/

//将主配置文件里的'#ServerName'行的注释去掉
[root@ea09fe549560 apache]# sed -i '/#ServerName/s/#//g' conf/httpd.conf

//启动httpd
[root@ea09fe549560 apache]# apachectl    
[root@ea09fe549560 apache]# 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:*

6.将容器提交为镜像

[root@localhost ~]# docker commit -c 'CMD ["/usr/local/apache/bin/httpd","-DFOREGROUND"] -p httpd ziczhou/httpd:v1.1
sha256:c1eb5e6a1d69ac79f912909c1fc7d38ab61c1194f3419e127b9ec6b3e1e3d588
[root@localhost ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
ziczhou/httpd   v1.1      c1eb5e6a1d69   4 minutes ago   745MB
httpd           latest    dabbfbe0c57b   7 months ago    144MB
centos          latest    5d0da3dc9764   10 months ago   231MB

//登录docker账号
[root@localhost ~]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ziczhou
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

//将镜像推送到docker官方网站的私有仓库
[root@localhost ~]# docker push ziczhou/httpd:v1.1 
The push refers to repository [docker.io/ziczhou/apache]
d2194aabf814: Pushed 
74ddd0ec08fa: Mounted from library/centos 
v1.1: digest: sha256:d7c2ca4a4648c3caeaac4a57f6c45819cafc15a6082f6ca9de3b153c01c29ed6 size: 742

7.利用提交好的镜像跑一个容器,并访问

[root@localhost ~]# docker run -it -d --name web -p 80:80 ziczhou/httpd:v1.1 
762ef10dfe61e8b8ea39d144120fe0da8e5760754bc92a91fe3cc2903b6ddc98
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                COMMAND         CREATED         STATUS         PORTS                               NAMES
762ef10dfe61   ziczhou/httpd:v1.1   "/usr/local/apache/b…"   2 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web
ea09fe549560   centos               "/bin/bash"     7 hours ago     Up 7 hours                                         httpd


[root@localhost ~]# curl 192.168.169.139
<html><body><h1>It works!</h1></body></html>

进行访问

posted @ 2022-08-09 11:50  Zic师傅  阅读(85)  评论(0编辑  收藏  举报