容器编译apache,部署web

本文档指导如何在Linux环境中利用Docker容器编译Apache HTTP服务器,并详细介绍了从制作镜像到部署网站的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

部署httpd

[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data/
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
--2022-08-11 09:12:01--  https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.213, 45.253.17.211, 45.253.17.215, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.213|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073556 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.6.5.tar.gz’

apr-1.6.5.tar.gz      100%[======================>]   1.02M   204KB/s    in 5.1s    

2022-08-11 09:12:06 (204 KB/s) - ‘apr-1.6.5.tar.gz’ saved [1073556/1073556]
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
--2022-08-11 09:12:28--  https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.216, 45.253.17.215, 45.253.17.226, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.216|: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.gz 100%[======================>] 541.31K   205KB/s    in 2.6s    

2022-08-11 09:12:31 (205 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]
[root@localhost data]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
--2022-08-11 09:12:48--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.217, 45.253.17.211, 45.253.17.215, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7434530 (7.1M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.bz2’

httpd-2.4.54.tar.bz2  100%[======================>]   7.09M   209KB/s    in 35s     

2022-08-11 09:13:24 (206 KB/s) - ‘httpd-2.4.54.tar.bz2’ saved [7434530/7434530]

[root@localhost ~]# docker pull centos //拉取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@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB
//启动centos系统容器,用-v将容器/data目录挂载到宿主机/data目录,-it分配伪终端打开标注输入
[root@localhost ~]# docker run -it --name web1 -v /data:/data centos /bin/bash
[root@34d59eb5d199 /]# ls /data/
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.bz2
[root@34d59eb5d199 /]# 
//官方源替换成阿里云源
[root@34d59eb5d199 /]# cd /etc/yum.repos.d/
[root@34d59eb5d199 yum.repos.d]# rm -rf *
[root@34d59eb5d199 yum.repos.d]# ls -l
total 0
[root@34d59eb5d199 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   7316      0 --:--:-- --:--:-- --:--:--  7316
[root@34d59eb5d199 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装开发工具包,httpd服务所需的依赖包,创建apache用户与组
[root@34d59eb5d199 ~]# dnf -y  groups mark install "Development Tools"
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com          299 kB/s | 4.6 MB     00:15    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com         92 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com     260 kB/s | 8.4 MB     00:33    
Dependencies resolved.
=====================================================================================
 Package            Architecture      Version               Repository          Size
=====================================================================================
Installing Groups:
 Development Tools                                                                  

Transaction Summary
=====================================================================================

Complete!
[root@34d59eb5d199 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:35 ago on Thu Aug 11 01:35:06 2022.
Dependencies resolved.
=====================================================================================
 Package                Arch   Version                               Repo       Size
=====================================================================================
Installing:
 expat-devel            x86_64 2.2.5-4.el8                           base       55 k
 libtool                x86_64 2.4.6-25.el8                          AppStream 709 k
 libxml2-devel          x86_64 2.9.7-9.el8_4.2                       AppStream 1.0 M
[root@34d59eb5d199 ~]# useradd -Mrs /bin/nologin apache
[root@34d59eb5d199 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
//解压
[root@34d59eb5d199 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@34d59eb5d199 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@34d59eb5d199 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src
//编译安装apr
[root@34d59eb5d199 apr-util-1.6.1]# cd /usr/local/src/apr-1.6.5/
[root@34d59eb5d199 apr-1.6.5]# vi configure
#    $RM "$cfgfile"    //把这一行注释掉或删掉
[root@34d59eb5d199 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@34d59eb5d199 apr-1.6.5]# make && make install
//编译安装apr-util
[root@34d59eb5d199 ~]# cd /usr/local/src/apr-util-1.6.1/
[root@34d59eb5d199 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
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
APR-util Version: 1.6.1
checking for chosen layout... apr-util
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
Applying apr-util hints file rules for x86_64-pc-linux-gnu
checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.
[root@34d59eb5d199 apr-util-1.6.1]# make && make install
//编译安装httpd
[root@34d59eb5d199 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@34d59eb5d199 httpd-2.4.54]# make && make install

//配置httpd全局变量,头文件映射,启动httpd服务
[root@34d59eb5d199 ~]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh
[root@34d59eb5d199 ~]# source /etc/profile.d/apache.sh
[root@34d59eb5d199 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@34d59eb5d199 ~]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
[root@34d59eb5d199 ~]# httpd
[root@34d59eb5d199 ~]# ss -anlt
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@34d59eb5d199 ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>
  

制作镜像

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS     NAMES
53884a3a32ec   5d0da3dc9764   "/bin/bash"   48 seconds ago   Up 47 seconds             vigorous_jemison
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' 53884a3a32ec raylight2002/httpd:v0.1
sha256:77cbdeefb8d20180cf4e859685ef1275be629c58b7aaa1f25885b303dc78e564
[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
raylight2002/httpd   v0.1      77cbdeefb8d2   8 seconds ago   231MB
//上传
[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: raylight2002
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
[root@localhost ~]# docker push raylight2002/httpd:v0.1
The push refers to repository [docker.io/raylight2002/httpd]
74ddd0ec08fa: Mounted from library/centos 
v0.1: digest: sha256:ba2246de4ddc0294e88834efa5c39514a5650142d883ee9f463f251295c7248e size: 529
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@localhost ~]# 

部署网站

[root@localhost ~]# docker run -itd --name test2 --restart always -p 80:80 -v /html:/usr/local/apache/htdocs raylight2002/httpd:v0.1
a8480f220961c09590e8f96adf83a5d1d4f68028f5bfd4524d405572c6cea11a
[root@localhost html]# ls
win8fgbd-v2.1.zip
[root@localhost html]# unzip win8fgbd-v2.1.zip
Archive:  win8fgbd-v2.1.zip
  inflating: eat.html                
   creating: img/
  inflating: img/bg.jpg              
  inflating: img/css.css             
 extracting: img/logo.png            
  inflating: img/logo.psd            
  inflating: img/logo_eat.png        
 extracting: img/logo_music.png      
 extracting: img/logo_radio.png      
 extracting: img/logo_vod.png        
 extracting: img/logo_xiu.png        
  inflating: img/videobg.png         
  inflating: index.html              
  inflating: music.html              
  inflating: radio.html              
  inflating: vod.html                
  inflating: xiu.html                
说明.htm:  mismatching "local" filename (шп┤цШО.htm),
         continuing with "central" filename version
  inflating: 说明.htm              
[root@localhost html]# ls
eat.html  img  index.html  music.html  radio.html  vod.html  win8fgbd-v2.1.zip  xiu.html  说明.htm

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值