容器里源码安装apache
0.0 安装docker
[root@yyx1 ~]# cd /etc/yum.repos.d/ //进入路径
[root@yyx1 yum.repos.d]# rm -f *
[root@yyx1 yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo //配置源
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1919 100 1919 0 0 14648 0 --:--:-- --:--:-- --:--:-- 14648
[root@yyx1 yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@yyx1 yum.repos.d]#yum -y install docker-ce //安装docker
1.安装镜像
[root@yyx1 ~]# systemctl start docker //启动docker
[root@yyx1 ~]# docker search centos //查找centos镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7115 [OK]
centos/systemd systemd enabled base container. 108 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 93
[root@yyx1 ~]# 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
[root@yyx1 ~]# docker images //查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 7 months ago 231MB
[root@yyx1 ~]# docker run -itd --name web --privileged=true centos /sbin/init //创建并后台运行镜像
[root@yyx1 ~]# docker ps //查看
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcdcbef5e022 centos "/sbin/init" 59 seconds ago Up 58 seconds web
dcdcbef5e022c3c28863efbefbaa5f0d334c83efdb8c51ee84e02fef55972e2d
[root@yyx1 ~]#docker exec -it web /bin/bash //进入镜像
2.安装apache
2.1 配置yum源
[root@dcdcbef5e022 /]# cd /etc/yum.repos.d/
[root@dcdcbef5e022 yum.repos.d]# rm -f *
[root@dcdcbef5e022 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@dcdcbef5e022 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@dcdcbef5e022 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@dcdcbef5e022 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
2.2 下载安装包
[root@dcdcbef5e022 ~]# useradd -r -M -s /sbin/nologin apache //创建用户
[root@dcdcbef5e022 ~]# yum -y install openssl-devel pcre-devel make expat-devel wget vim gcc gcc-c++ libtool //安装依赖包
[root@dcdcbef5e022 ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz //下载apr
[root@dcdcbef5e022 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz //下载apr-util
[root@dcdcbef5e022 ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz //下载httpd
注: linux系统自带.gz格式解压工具,如果是其他格式需要自行安装相应工具
//解压
[root@dcdcbef5e022 ~]# tar xf apr-1.7.0.tar.gz
[root@dcdcbef5e022 ~]# tar xf apr-util-1.6.1.tar.gz
[root@dcdcbef5e022 ~]# tar xf httpd-2.4.53.tar.gz
[root@dcdcbef5e022 ~]# ls
anaconda-ks.cfg apr-1.7.0 apr-util-1.6.1 httpd-2.4.53 original-ks.cfg
anaconda-post.log apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz
2.3安装httpd
[root@dcdcbef5e022 ~]# cd apr-1.7.0
[root@dcdcbef5e022 apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM "$cfgfile"; exit 1" 1 2 15
# $RM "$cfgfile" //找到这一行并且注释掉或者删除
[root@dcdcbef5e022 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@dcdcbef5e022 apr-1.7.0]#make && make install
[root@dcdcbef5e022 apr-util-1.6.1]#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@dcdcbef5e022 apr-util-1.6.1]#makr && make install
[root@dcdcbef5e022 apr-util-1.6.1]# cd /root/httpd-2.4.53
[root@dcdcbef5e022 httpd-2.4.53]# ./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@dcdcbef5e022 httpd-2.4.53]# make && make install
//安装成功
2.4配置文件
[root@dcdcbef5e022 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/apache.sh
[root@dcdcbef5e022 ~]# source /etc/profile.d/apache.sh
[root@dcdcbef5e022 ~]# which httpd
/usr/local/apache/bin/httpd
[root@dcdcbef5e022 ~]# which apachectl
/usr/local/apache/bin/apachectl
[root@dcdcbef5e022 ~]# ln -s /usr/local/apache/include /usr/include/apache
2.5 设置apache
[root@dcdcbef5e022 ~]# cd /usr/lib/systemd/system //进入路径
[root@dcdcbef5e022 system]# vim 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
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
//配置网页文件
[root@dcdcbef5e022 system]# cd /usr/local/apache/htdocs/
[root@dcdcbef5e022 htdocs]# mkdir web.com
[root@dcdcbef5e022 htdocs]# ls
index.html web.com
[root@dcdcbef5e022 htdocs]# cd web.com
[root@dcdcbef5e022 web.com]# echo 'hello!' > index.html
[root@dcdcbef5e022 web.com]# cat index.html
hello!
[root@dcdcbef5e022 web.com]# cd /usr/local/apache/conf/extra/ //进入文件
[root@dcdcbef5e022 extra]# vim httpd-vhosts.conf //更改文件为以下内容
DocumentRoot "/usr/local/apache/htdocs/web.com"
ServerName web.com
ErrorLog "logs/web.com-error_log"
CustomLog "logs/web.com-access_log" common
[root@dcdcbef5e022 extra]# vim /usr/local/apache/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf //取消这一行的注释
2.6 启动apache
[root@dcdcbef5e022 extra]# apachectl start //启动apache
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@dcdcbef5e022 extra]# systemctl start httpd //启动httpd
[root@dcdcbef5e022 extra]# systemctl status httpd //查看httpd状态
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-04-26 14:18:09 UTC; 1s ago
Docs: man:httpd(8)
Process: 32421 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 32424 (httpd)
Tasks: 6 (limit: 4724)
Memory: 4.1M
CGroup: /docker/dcdcbef5e022c3c28863efbefbaa5f0d334c83efdb8c51ee84e02fef55972e2d/system.slice/httpd.service
├─32424 /usr/local/apache/bin/httpd -k start
├─32425 /usr/local/apache/bin/httpd -k start
├─32426 /usr/local/apache/bin/httpd -k start
├─32427 /usr/local/apache/bin/httpd -k start
├─32428 /usr/local/apache/bin/httpd -k start
└─32429 /usr/local/apache/bin/httpd -k start
Apr 26 14:18:09 dcdcbef5e022 systemd[1]: Starting httpd server daemon...
Apr 26 14:18:09 dcdcbef5e022 apachectl[32421]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive gl>
Apr 26 14:18:09 dcdcbef5e022 systemd[1]: Started httpd server daemon.
[root@dcdcbef5e022 extra]# 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@dcdcbef5e022 web.com]# curl 172.17.0.2 //访问成功
hello!
[root@yyx1 ~]# curl 172.17.0.2 //主机访问成功
hello!
3.镜像
在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行
[root@yyx1 ~]# docker ps //查看运行中的镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcdcbef5e022 centos "/sbin/init" 45 minutes ago Up 45 minutes web
[root@yyx1 ~]# docker commit -p web //生成镜像
sha256:50b05ea6d75ff738da9df84e1c1dc9db16d4aa1554f619fbd3a9c493930c126d
[root@yyx1 ~]# docker images //查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
50b05ea6d75f 45 seconds ago 774MB
centos latest 5d0da3dc9764 7 months ago 231MB
[root@yyx1 ~]# docker tag 50b05ea6d75f yyxnb //添加标签
[root@yyx1 ~]# docker images //再次查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
yyxnb latest 50b05ea6d75f About a minute ago 774MB
centos latest 5d0da3dc9764 7 months ago 231MB
4.过程中可能会遇到的问题
[root@0d5c2fb679ae /]# systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
原因:没有初始化系统
解决方法:保存当前镜像,再用这个镜像新建一个容器
使用这个命令 docker run -d --name 名字 --privileged=true centos /sbin/init 就行了
[root@0d5c2fb679ae /]# apachectl start
AH00112: Warning: DocumentRoot [/usr/local/apache/docs/dummy-host2.example.com] does not exist
原因:配置文件有误
解决方法:进入提示路径 把多余的配置内容删掉