- A+
所属分类:linux技术
在docker中编译安装apache
1.安装docker
[root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo [root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo [root@localhost yum.repos.d]# dnf -y install docker-ce
2.配置镜像加速器
[root@localhost ~]# sudo mkdir -p /etc/docker [root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF' > { > "registry-mirrors": ["https://w673ojdv.mirror.aliyuncs.com"] > } > EOF { "registry-mirrors": ["https://w673ojdv.mirror.aliyuncs.com"] } [root@localhost ~]# sudo systemctl daemon-reload [root@localhost ~]# sudo systemctl restart docker [root@localhost ~]# systemctl enable docker Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
3.先拉取一个centos8的镜像
[root@localhost ~]# docker pull centos:8 8: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:8 docker.io/library/centos:8 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 8 5d0da3dc9764 10 months ago 231MB
4.用centos生成一个容器进去
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash [root@9cb6bb1b5eb9 /]#
5.在容器中阿里云的yum源和epel源
[root@9cb6bb1b5eb9 /]# cd /etc/yum.repos.d/ [root@9cb6bb1b5eb9 yum.repos.d]# ls CentOS-Linux-AppStream.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Plus.repo CentOS-Linux-BaseOS.repo CentOS-Linux-Devel.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-PowerTools.repo CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Extras.repo CentOS-Linux-Media.repo CentOS-Linux-Sources.repo [root@9cb6bb1b5eb9 yum.repos.d]# rm -rf * [root@9cb6bb1b5eb9 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 15121 0 --:--:-- --:--:-- --:--:-- 15121 [root@9cb6bb1b5eb9 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo [root@9cb6bb1b5eb9 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm [root@9cb6bb1b5eb9 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel* [root@9cb6bb1b5eb9 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
6.下载apache的包,并复制到容器中
//新打开一个终端 [root@localhost ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz http://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz [root@localhost ~]# ls anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz [root@localhost ~]# docker cp httpd-2.4.54.tar.gz httpd:/usr/src/ [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@9cb6bb1b5eb9 ~]# ls /usr/src/ apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz debug httpd-2.4.54.tar.gz kernels //下载相关依赖包 [root@9cb6bb1b5eb9 ~]# dnf -y install wget openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make //创建一个用户 [root@9cb6bb1b5eb9 ~]# useradd -r -M -s /sbin/nologin apache [root@9cb6bb1b5eb9 ~]# id apache uid=998(apache) gid=996(apache) groups=996(apache)
7.解压并编译安装httpd,apr,apr-util
//解压编译apr [root@9cb6bb1b5eb9 ~]# cd /usr/src/ [root@9cb6bb1b5eb9 src]# tar xf apr-1.7.0.tar.gz [root@9cb6bb1b5eb9 src]# cd apr-1.7.0 [root@9cb6bb1b5eb9 apr-1.7.0]# vi configure cfgfile=${ofile}T trap "$RM "$cfgfile"; exit 1" 1 2 15 #$RM "$cfgfile" //注释这一行 [root@9cb6bb1b5eb9 apr-1.7.0]# ./configure --prefix=/usr/local/apr [root@9cb6bb1b5eb9 apr-1.7.0]# make && make install //解压编译apr-util [root@9cb6bb1b5eb9 apr-1.7.0]# cd .. [root@9cb6bb1b5eb9 src]# tar xf apr-util-1.6.1.tar.gz [root@9cb6bb1b5eb9 src]# cd apr-util-1.6.1 [root@9cb6bb1b5eb9 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@9cb6bb1b5eb9 apr-util-1.6.1]# make && make install //解压编译httpd [root@9cb6bb1b5eb9 apr-util-1.6.1]# cd .. [root@9cb6bb1b5eb9 src]# tar xf httpd-2.4.54.tar.gz [root@9cb6bb1b5eb9 src]# cd httpd-2.4.54 [root@9cb6bb1b5eb9 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-rpm=prefork [root@9cb6bb1b5eb9 httpd-2.4.54]# make && make install
8.配置环境变量和创建头文件
//配置环境变量 [root@9cb6bb1b5eb9 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh [root@9cb6bb1b5eb9 ~]# source /etc/profile.d/apache.sh //创建头文件 [root@9cb6bb1b5eb9 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
9.制作httpd镜像
[root@localhost ~]# docker commit -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p 9cb6bb1b5eb9 zxr1001/httpd:v1 sha256:937a058ecbb8b199735c75e5c1e574f17ce856c8c1a01604c2fd6c60ec3dbfc0 [root@localhost ~]# docker commit -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p 9cb6bb1b5eb9 zxr1001/httpd:v1 sha256:937a058ecbb8b199735c75e5c1e574f17ce856c8c1a01604c2fd6c60ec3dbfc0 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zxr1001/httpd v1 937a058ecbb8 34 seconds ago 762MB centos 8 5d0da3dc9764 10 months ago 231MB
10.对镜像进行验证并访问测试
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cb6bb1b5eb9 centos:8 "/bin/bash" 2 hours ago Up 2 hours httpd [root@localhost ~]# docker stop httpd httpd [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker run -dit -p 80:80 --name web zxr1001/httpd:v1 /bin/bash fdc8efe72440e8f8583fb08d715f914a45ebc06c725991fee113e4eae0d1af4e [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fdc8efe72440 zxr1001/httpd:v1 "/bin/bash" 7 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web