docker中编译httpd


基于容器创建编译安装httpd

开启docker

[root@localhost ~]# systemctl  start docker.service 

拉取镜像

[root@localhost ~]# 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

基于centos镜像创建一个名为apaqi的容器并开启他

[root@localhost ~]# docker run --name apaqi -it centos

进入到centos镜像中然后拉取阿里源并进行备份

[root@7b2bedbda2f9 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
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     100  2495  100  2495    0     0   8910      0 --:--:-- --:--:-- --:--:--  8910
[root@7b2bedbda2f9 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
安装epel源
[root@7b2bedbda2f9 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  4.1 MB/s | 4.6 MB     00:01    
CentOS-8.5.2111 - Extras - mirrors.aliyun.co  47 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun 3.9 MB/s | 8.4 MB     00:02    
epel-release-latest-8.noarch.rpm             142 kB/s |  24 kB     00:00    
Dependencies resolved.
=============================================================================
 Package             Architecture  Version         Repository           Size
=============================================================================
将repo配置中的地址替换成aliyun镜像站地址
[root@7b2bedbda2f9 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@7b2bedbda2f9 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

httpd基础环境准备

[root@7b2bedbda2f9 yum.repos.d]# dnf -y install gcc gcc-c++ make wget
Last metadata expiration check: 0:01:43 ago on Mon 08 Aug 2022 01:55:03 PM UTC.
Package gcc-8.5.0-4.el8_5.x86_64 is already installed.
Package gcc-c++-8.5.0-4.el8_5.x86_64 is already installed.
Package make-1:4.2.1-10.el8.x86_64 is already installed.
Package wget-1.19.5-10.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

下载httpd所需的依赖包

[root@7b2bedbda2f9 yum.repos.d]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
[root@7b2bedbda2f9 yum.repos.d]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
Last metadata expiration check: 0:05:05 ago on Mon 08 Aug 2022 01:55:03 PM UTC.
Dependencies resolved.
=============================================================================
 Package                Arch   Version                       Repo       Size
=============================================================================
Installing:
 expat-devel            x86_64 2.2.5-4.el8                   base       55 k
............
  xz-devel-5.2.4-3.el8.x86_64                                                
  zlib-devel-1.2.11-17.el8.x86_64                                            

Complete!

创建一个系统名为apache的系统用户用来管理apache

[root@7b2bedbda2f9 yum.repos.d]# useradd -r apache

下载apr,apr-utils和httpd

[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# wget https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
--2022-08-08 14:09:36--  https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 180.101.197.240, 58.49.248.227, 58.49.248.233, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|180.101.197.240|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1093896 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.7.0.tar.gz.1’

apr-1.7.0.tar.gz.1  100%[================>]   1.04M  3.70MB/s    in 0.3s    

2022-08-08 14:09:36 (3.70 MB/s) - ‘apr-1.7.0.tar.gz.1’ saved [1093896/1093896]

[root@7b2bedbda2f9 src]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz                                      
--2022-08-08 14:11:47--  https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 180.101.197.244, 58.42.55.236, 180.101.199.239, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|180.101.197.244|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 554301 (541K) [application/octet-stream]
Saving to: ‘apr-util-1.6.1.tar.gz’

apr-util-1.6.1.tar. 100%[================>] 541.31K  3.06MB/s    in 0.2s    

2022-08-08 14:11:47 (3.06 MB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]


[root@7b2bedbda2f9 src]#  wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz |head -5
--2022-08-08 14:07:12--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 58.49.248.230, 58.49.248.232, 180.101.197.238, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|58.49.248.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9743277 (9.3M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.gz’

httpd-2.4.54.tar.gz 100%[================>]   9.29M  4.87MB/s    in 1.9s    

2022-08-08 14:07:14 (4.87 MB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]
[root@7b2bedbda2f9 src]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  debug	httpd-2.4.54.tar.gz  kernels

解压apr并进入其目录

[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf apr-1.7.0.tar.gz 
[root@7b2bedbda2f9 src]# cd apr-1.7.0

进入configure注释或删除此行

[root@7b2bedbda2f9 apr-1.7.0]# vim configure 
# $RM "$cfgfile"

生成makefile

[root@7b2bedbda2f9 apr-1.7.0]# ./configure --help|head
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:

将apr编译到/usr/local/apr目录下

[root@7b2bedbda2f9 apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
...........
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands

对apr进行编译并安装

[root@7b2bedbda2f9 apr-1.7.0]# make && make install
make[1]: Entering directory '/usr/src/apr-1.7.0'
/bin/sh /usr/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I./include -I/usr/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/src/apr-1.7.0/include/arch/unix -I/usr/src/apr-1.7.0/include -I/usr/src/apr-1.7.0/include/private -I/usr/src/apr-1.7.0/include/private  -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
..............
    /usr/bin/install -c -m 644 /usr/src/apr-1.7.0/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config

进入apr的安装目录下对其库文件与头部文件进行配置

[root@7b2bedbda2f9 apr-1.7.0]# cd /usr/local/apr/
[root@7b2bedbda2f9 apr]# ls
bin  build-1  include  lib
库文件设置
[root@7b2bedbda2f9 apr]# vim /etc/ld.so.conf.d/apr.conf
[root@7b2bedbda2f9 apr]# cat /etc/ld.so.conf.d/apr.conf
/usr/local/apr/lib
[root@7b2bedbda2f9 apr]# ldconfig 
头部文件设置
[root@7b2bedbda2f9 apr]# ln -s /usr/local/apr/include/ /usr/include/apr

解压apr-util并进入其目录

[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf apr-util-1.6.1.tar.gz 
[root@7b2bedbda2f9 src]# cd apr-util-1.6.1

配置相关的配置并生成makefile

[root@7b2bedbda2f9 apr-util-1.6.1]# ./configure --help |head -5
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as

将apr-util编译到/usr/local/apr-util目录下 //注意因为apr-util依赖与apr所以要添加--with-apr=/usr/local/apr(apr的安装目录)

[root@7b2bedbda2f9 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu

编译安装apr-util

[root@7b2bedbda2f9 apr-1.7.0]# make && make install
make[1]: Entering directory '/usr/src/apr-1.7.0'
/bin/sh /usr/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I./include -I/usr/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/src/apr-1.7.0/include/arch/unix -I/usr/src/apr-1.7.0/include -I/usr/src/apr-1.7.0/include/private -I/usr/src/apr-1.7.0/include/private  -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
.........
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config

进入到apr-util的安装目录下配置其库文件与头部文件

[root@7b2bedbda2f9 apr-util-1.6.1]# cd /usr/local/apr-util/
[root@7b2bedbda2f9 apr-util]# ls
bin  include  lib
[root@7b2bedbda2f9 apr-util]# vim /etc/ld.so.conf.d/apr-util.conf
[root@7b2bedbda2f9 apr-util]# cat /etc/ld.so.conf.d/apr-util.conf
/usr/local/apr-util/lib
[root@7b2bedbda2f9 apr-util]# ln -s /usr/local/apr-util/include/ /usr/include/apr-util
[root@7b2bedbda2f9 apr-util]#  ldconfig 

解压httpd并进入其目录

[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf httpd-2.4.54.tar.gz 
cd [root@7b2bedbda2f9 src]# cd httpd-2.4.54
[root@7b2bedbda2f9 httpd-2.4.54]# 

生成makefile

[root@7b2bedbda2f9 httpd-2.4.54]# ./configure  --help|head
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:

将httpd编译到/usr/local/httpd目录下

[root@7b2bedbda2f9 httpd-2.4.54]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --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
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
..........
configure: summary of build options:

    Server Version: 2.4.54
    Install prefix: /usr/local/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

编译并安装httpd

[root@7b2bedbda2f9 httpd-2.4.54]# make && make install
Making all in srclib
make[1]: Entering directory '/usr/src/httpd-2.4.54/srclib'
make[1]: Leaving directory '/usr/src/httpd-2.4.54/srclib'
Making all in os
.........
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: Leaving directory '/usr/src/httpd-2.4.54'

进入httpd安装目录并配置头部文件

[root@7b2bedbda2f9 httpd]# cd /usr/local/httpd/
[root@7b2bedbda2f9 httpd]# ls
bin    cgi-bin	error	icons	 logs  manual
build  conf	htdocs	include  man   modules
[root@7b2bedbda2f9 src]# ln -s /usr/local/httpd/include/ /usr/include/httpd/

设置环境变量

[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# echo 'export PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh 
[root@7b2bedbda2f9 src]# source /etc/profile.d/httpd.sh
[root@7b2bedbda2f9 src]# which httpd
/usr/local/httpd/bin/httpd
[root@7b2bedbda2f9 src]# which apachectl
/usr/local/httpd/bin/apachectl
[root@7b2bedbda2f9 ~]# 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@localhost ~]# docker commit -p apaqi
sha256:5e09e5c4730f1e57bf0ace9d08983695389f6f2b7c222192b3011902863fc6ea
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    5e09e5c4730f   11 seconds ago   788MB
centos       latest    5d0da3dc9764   10 months ago    231MB
[root@localhost ~]# docker tag 5e09e5c4730f jiranna/apaqi:httpd2.4.54
[root@localhost ~]# docker images
REPOSITORY            TAG           IMAGE ID       CREATED         SIZE
jiranxshjsjja/apaqi   httpd2.4.54   5e09e5c4730f   2 minutes ago   788MB
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: jiranna
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

将apache上传到jiranna这个账户下

[root@localhost ~]# docker push jiranna/apaqi:httpd2.4.54
The push refers to repository [docker.io/jiranna/apaqi]
e49b6ac70572: Pushed 
74ddd0ec08fa: Mounted from library/centos 
httpd2.4.54: digest: sha256:9e746b43090795e71be4d586c91f60a9e545ac09eaa8d832deb7f111aa2aa0ce size: 742

在docker中查看,已经生成了

此时使用docker inspect命令查看apache容器启动的默认进程是什么

[root@localhost ~]# docker inspect apaqi
[
    {
        "Id": "7b2bedbda2f93fe5119db87a2e113401df3209edc2afb950bfb0314f10f2c5b2",
        "Created": "2022-08-08T13:47:08.296485372Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
..........
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

重新生成镜像并上传

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","-f","-h","data"]' -p apaqi jiranna/apaqi:httpd2.4.54
sha256:0a22a78fc67d7a2bf2e441af0199f096f03384b348a544e8ba0814a78089c9a9
[root@localhost ~]# docker push jiranna/apaqi:httpd2.4.54
The push refers to repository [docker.io/jiranna/apaqi]
e49b6ac70572: Layer already exists 
74ddd0ec08fa: Layer already exists 
httpd2.4.54: digest: sha256:c31b205bf92757a2a5336f149f758c9872b9fa8e5a312bdd3c0095aa61ecb828 size: 742

此时仓库中已经又这个镜像了


将这个镜像重新拉取生成容器

[root@YL ~]# docker pull jiranna/apaqi:httpd2.4.54
httpd2.4.54: Pulling from jiranna/apaqi
a1d0c7532777: Pull complete 
f9a8e5329f1c: Pull complete 
Digest: sha256:6d4ddd2e07e2205ae6f59e90636f361cca65212e75f2629de3671717e3a7f51e
//做端口映射让其能够访问
[root@YL ~]# docker run --name yang -d -p 80:80 jiranna/apaqi:httpd2.4.54
48e2f25fc94f918576fc3a8e9ad378af5842f83e7337c6bec6e0a540272b16c2

成功访问

posted @ 2022-08-09 13:15  Tqing  阅读(160)  评论(0)    收藏  举报