Docker编译安装httpd

Docker编译安装httpd

目录

Docker编译安装httpd

拉取Centos镜像

配置yum源

安装httpd

配置文件

写service文件

删除多余文件及卸载不用的包

镜像制作

上传镜像


使用容器运行httpd可以直接拉去官方Apache的镜像,而想要在容器中实现httpd源码安装那就得拉一个Centos的镜像然后自己在Centos中进行源码安装。

当然,如果找得到已经做好了的镜像也行,这就是Docker的一大特色。

拉取Centos镜像

 [root@master ~]# docker images
 REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
 mysql        5.7       3147495b3a5c   13 days ago   431MB
 [root@master ~]# docker pull centos:7  #拉取镜像
 7: Pulling from library/centos
 2d473b07cdd5: Pull complete 
 Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
 Status: Downloaded newer image for centos:7
 docker.io/library/centos:7  
 [root@master ~]# docker run -itd --name httpd centos:7   #启动
 198be6cd882f76b6517acb7e206c0a9af6113466e78147c0663cfbe69b336b1e
 [root@master ~]# 
 [root@master ~]# docker ps #这里看到容器已经在运行了
 CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
 198be6cd882f   centos:7   "/bin/bash"   58 seconds ago   Up 57 seconds             httpd
 [root@master ~]# docker exec -it httpd /bin/bash
 #进入容器 这个命令的意思就是在httpd容器中开启一个交互模式的终端
 [root@198be6cd882f /]# ls
 anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
 bin                etc  lib   media  opt  root  sbin  sys  usr
 [root@198be6cd882f /]# 

配置yum源

 [root@198be6cd882f /]# cd /etc/yum.repos.d/
 [root@198be6cd882f yum.repos.d]# ls
 CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-fasttrack.repo
 CentOS-CR.repo    CentOS-Media.repo      CentOS-Vault.repo    CentOS-x86_64-kernel.repo
 [root@198be6cd882f yum.repos.d]# rm -rf *
 [root@198be6cd882f yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
 100  2523  100  2523    0     0   9238      0 --:--:-- --:--:-- --:--:--  9275
 #这是用的阿里源
 [root@198be6cd882f yum.repos.d]# ls
 CentOS-Base.repo
 [root@198be6cd882f yum.repos.d]# 

安装httpd

 [root@198be6cd882f yum.repos.d]# useradd -r -M -s /sbin/nologin apache      #创建用户 
 [root@198be6cd882f yum.repos.d]# yum -y install openssl-devel pcre-devel make expat-devel wget vim gcc gcc-c++ libtool          #安装需要使用的wget和httpd相关依赖包
 ​
 [root@198be6cd882f ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
 [root@198be6cd882f ~]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.gz
 [root@198be6cd882f ~]#  wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
 #ok
 [root@198be6cd882f ~]# tar -xf apr-1.6.5.tar.gz 
 [root@198be6cd882f ~]# tar -xf apr-util-1.6.1.tar.gz 
 [root@198be6cd882f ~]# tar -xf httpd-2.4.54.tar.gz 
 [root@198be6cd882f ~]# cd apr-1.6.5
 [root@198be6cd882f apr-1.6.5]# ./configure --prefix=/usr/local/apr
 [root@198be6cd882f apr-1.6.5]# make && make install
 [root@198be6cd882f ~]# cd apr-util-1.6.1
 [root@198be6cd882f apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
 [root@198be6cd882f apr-util-1.6.1]# make && make install
 [root@198be6cd882f ~]# cd httpd-2.4.54
 [root@198be6cd882f httpd-2.4.54]#  ./configure --prefix=/usr/local/apache --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 && make &&make install

配置文件

 [root@198be6cd882f ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
 [root@198be6cd882f ~]# source /etc/profile.d/apache.sh 
 [root@198be6cd882f ~]# which httpd
 /usr/local/apache/bin/httpd
 [root@198be6cd882f ~]# ln -s /usr/local/apache/include/ /usr/include/apache
 [root@198be6cd882f ~]#  ll /usr/include/|grep apache
 lrwxrwxrwx.  1 root root     26 Aug  9 01:31 apache -> /usr/local/apache/include/
 ​

写service文件

 [root@198be6cd882f ~]# cd /etc/systemd/system/
 [root@198be6cd882f system]# ls
 default.target        getty.target.wants       system-update.target.wants
 default.target.wants  multi-user.target.wants
 [root@198be6cd882f system]# cd /etc/systemd/system
 [root@198be6cd882f system]# vim httpd.service
 [root@198be6cd882f system]# cat httpd.service
 [Unit]
 Description=httpd server daemon
 Documentation=man:httpd(8)
 After=network.target 
 ​
 [Service]
 Type=forking
 ExecStart=/usr/local/apache/bin/apachectl start
 ExecStop=/usr/local/apache/bin/apachectl stop
 #上两个start和stop路径不绝对,倘若安装过程中改变了文件位置可以使用命令找一下find / -name apachectl
 /root/httpd-2.4.54/support/apachectl
 /usr/local/apache/bin/apachectl
 ​
 ExecReload=/bin/kill -HUP $MAINPID
 ​
 [Install]
 WantedBy=multi-user.target
 [root@198be6cd882f system]# 
 ​
 ​
 [root@198be6cd882f ~]# cd /usr/local/apache/
 [root@198be6cd882f apache]# ls
 bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
 [root@198be6cd882f apache]# cd htdocs/
 #这里就是httpd默认显示页面存放路径.可以改index.html中的文件内容.
 [root@198be6cd882f htdocs]# ls
 index.html
 [root@198be6cd882f htdocs]# echo "hhhhhhhh" index.html 
 hhhhhhhh index.html
 [root@198be6cd882f htdocs]# echo "hhhhhhhh" >> index.html 
 [root@198be6cd882f htdocs]# cat index.html 
 <html><body><h1>It works!</h1></body></html>
 hhhhhhhh
 [root@198be6cd882f htdocs]# 
 ​
 ---------------
 #启动服务并设置开机自启
 [root@198be6cd882f ~]# 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@198be6cd882f ~]# systemctl start httpd
 [root@198be6cd882f ~]# ss -antl
 State      Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
 LISTEN     0      128                  *:80                               *:*                  
 [root@198be6cd882f ~]# 
 ​
 写一个启动脚本,以便后面启动用
 [root@198be6cd882f bin]# vi /httpd.sh 
 [root@198be6cd882f bin]# cat /httpd.sh 
 #!/bin/bash
 /usr/local/apache/bin/apachectl
 [root@198be6cd882f bin]# chmod +x /httpd.sh 
 [root@198be6cd882f bin]# ll / |grep httpd
 -rwxr-xr-x.   1 root root    44 Aug  9 02:06 httpd.sh
 [root@198be6cd882f bin]# 
 ​

删除多余文件及卸载不用的包

保持docker 的优点,尽量做到减少资源消耗.

 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
 apr-1.6.5        apr-util-1.6.1    httpd-2.4.54
 [root@198be6cd882f ~]# rm -rf apr*
 [root@198be6cd882f ~]# rm -rf httpd*
 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg
 [root@198be6cd882f ~]# 
 [root@198be6cd882f ~]# yum  -y remove gcc make wget vim 
 ​

镜像制作

 #在创建镜像的时候,容器不能关闭,必须使其处于允许状态,所以我们必须另起一个终端.
 [root@198be6cd882f bin]# vi /httpd.sh 
 [root@198be6cd882f bin]# cat /httpd.sh 
 #!/bin/bash
 /usr/local/apache/bin/apachectl
 [root@198be6cd882f bin]# chmod +x /httpd.sh 
 [root@198be6cd882f bin]# ll / |grep httpd
 -rwxr-xr-x.   1 root root    44 Aug  9 02:06 httpd.sh
 [root@198be6cd882f bin]# 
 [root@master ~]# docker commit -a '1264218057@qq.com' -m 'Source code installation' -c 'CMD ["/httpd.sh"]' -p httpd httpd:v0.1
 #这里当时写的那个shell脚本就用上了
 sha256:fddaa12cf47117d3ccbcbb2ee63ed8c802b73985b58e85423dbc208c8c7cf269
 [root@master ~]# docker images
 REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
 httpd        v0.1      fddaa12cf471   13 seconds ago   708MB
 mysql        5.7       3147495b3a5c   13 days ago      431MB
 centos       7         eeb6ee3f44bd   10 months ago    204MB
 [root@master ~]# 
 [root@master ~]# docker ps
 CONTAINER ID   IMAGE      COMMAND       CREATED             STATUS             PORTS     NAMES
 198be6cd882f   centos:7   "/bin/bash"   About an hour ago   Up About an hour             httpd
 [root@master ~]# docker run -itd --name httpd1 -p 80:80 httpd:v0.1
 [root@master ~]# docker inspect 
 #这个命令里面找ip
 [root@master ~]# curl 172.17.0.2
 <html><body><h1>It works!</h1></body></html>
 hhhhhhhh
 ​
 [root@master ~]# docker ps
 CONTAINER ID   IMAGE        COMMAND            CREATED          STATUS          PORTS     NAMES
 30d67b240b9c   httpd:v2     "/root/httpd.sh"   3 minutes ago    Up 3 minutes              httpds
 ​

上传镜像

 [root@master ~]# docker images
 REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
 tangyuxuan1/httpd   v3        02ac9597a898   58 seconds ago   708MB
 httpd               v2        d8966f4bf114   20 minutes ago   708MB
 centos              0.1       c857bc039f0e   39 minutes ago   708MB
 httpd               v0.1      fddaa12cf471   58 minutes ago   708MB
 mysql               5.7       3147495b3a5c   13 days ago      431MB
 centos              7         eeb6ee3f44bd   10 months ago    204MB
 [root@master ~]# docker push tangyuxuan1/httpd:v3
 The push refers to repository [docker.io/tangyuxuan1/httpd]
 313a85b5213a: Pushed 
 af3cf1502ecd: Pushed 
 bd30f621044a: Pushed 
 174f56854903: Pushed 
 v3: digest: sha256:2f7663a5ab7810cf0f69f8495cd9654e7566d6bb67c0705edee77a744accca2c size: 1158
 [root@master ~]# 
 ​

使用命令:

docker run -d --name httpd12 -p 88:80 tangyuxuan1/httpd:v3

[root@master ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS         PORTS                               NAMES
d34943fb9f3f   tangyuxuan1/httpd:v3   "/root/httpd.sh -f -…"   38 minutes ago   Up 7 minutes   0.0.0.0:88->80/tcp, :::88->80/tcp   httpd12
 

 

报错:

 docker中安装完httpd服务后,使用命令systemctl start httpd.service,发现报错,错误信息:Failed to get D-Bus connection: Operation not permitted 解决方法:使用命令docker run -d -name centos7 --privileged=true centos:7 /usr/sbin/init创建容器,然后使用docker exec -it centos7 /bin/bash进入容器
 apachectl start
 AH00112: Warning: DocumentRoot [/usr/local/apache/docs/dummy-host2.example.com] does not exist
 原因:配置文件有误
 进入提示路径修改多余配置内容.
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值