docker编译安装apache
基于容器编译安装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://t82gwevc.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://t82gwevc.mirror.aliyuncs.com"]
}
[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker
[root@localhost ~]#
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
[root@localhost ~]#
4.用centos生成一个容器并进去
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@5ab6d65d8281 /]#
5.在容器中修改阿里云的yum源和epel源
[root@5ab6d65d8281 /]# cd /etc/yum.repos.d/
[root@5ab6d65d8281 yum.repos.d]# rm -rf *
[root@5ab6d65d8281 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 12475 0 --:--:-- --:--:-- --:--:-- 12475
[root@5ab6d65d8281 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@5ab6d65d8281 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com 532 kB/s | 4.6 MB 00:08
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 68 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 370 kB/s | 8.4 MB 00:23
epel-release-latest-8.noarch.rpm 136 kB/s | 24 kB 00:00
Dependencies resolved.
[root@5ab6d65d8281 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@5ab6d65d8281 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@5ab6d65d8281 yum.repos.d]#
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 ~]# 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@5ab6d65d8281 yum.repos.d]# 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@5ab6d65d8281 yum.repos.d]# dnf -y install wget openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make
//创建个apache用户
[root@5ab6d65d8281 ~]# useradd -r -M -s /sbin/nologin apache
7.解压编译安装http、apr、apr-util
[root@5ab6d65d8281 ~]# cd /usr/src
[root@5ab6d65d8281 src]# tar xf apr-1.7.0.tar.gz
[root@5ab6d65d8281 src]# cd apr-1.7.0
[root@5ab6d65d8281 apr-1.7.0]# vi configure
[root@5ab6d65d8281 apr-1.7.0]#
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //注释这一行
cat <<_LT_EOF >> "$cfgfile"
[root@5ab6d65d8281 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@5ab6d65d8281 apr-1.7.0]# make && make install
解压编译apr-util
[root@5ab6d65d8281 src]# tar xf apr-util-1.6.1.tar.gz
[root@5ab6d65d8281 src]# cd apr-util-1.6.1
[root@5ab6d65d8281 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@5ab6d65d8281 apr-util-1.6.1]# make && make install
解压编译httpd
[root@5ab6d65d8281 src]# tar xf httpd-2.4.54.tar.gz
[root@5ab6d65d8281 src]# cd httpd-2.4.54
[root@5ab6d65d8281 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@5ab6d65d8281 httpd-2.4.54]# make && make install
8.配置环境变量和创建头文件
[root@5ab6d65d8281 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@5ab6d65d8281 ~]#
[root@5ab6d65d8281 ~]# source /etc/profile.d/apache.sh
[root@5ab6d65d8281 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@5ab6d65d8281 ~]#
9.制作httpd镜像
[root@localhost ~]# docker commit -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p 5ab6d65d8281 nie1001/httpd:v1
sha256:5be67efbc516b6fee625b78119c4c98495bb5939c9041d56fd32d3f9df9ea86b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nie1001/httpd v1 5be67efbc516 27 seconds ago 762MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos 8 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]#
10.对镜像进行验证并访问测试
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ab6d65d8281 centos:8 "/bin/bash" 2 hours ago Up 2 hours httpd
[root@localhost ~]# docker run -dit -p 80:80 --name web nie1001/httpd:v1 /bin/bash
7b68f52ea54b87bd004ba08f629731c0d9dc17d48902f00a52f2e77fb2c08d47
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b68f52ea54b nie1001/httpd:v1 "/bin/bash" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@localhost ~]# docker exec -it web /bin/bash
[root@7b68f52ea54b /]# apachectl start
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
[root@7b68f52ea54b /]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了