docker容器使用centos容器编译安装httpd
1. 运行一个centos容器
在服务端和客户端都安装nfs服务
[root@localhost ~]# yum -y install nfs-utils
[root@client ~]# yum -y install nfs-utils
// 授权一个对象供其使用
[root@client ~]# mkdir /nfs //因为没有此目录所以需要创建
[root@client ~]# cat /etc/exports
/nfs 192.168.182.150(rw)
[root@client ~]# systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
// 因为/nfs目录默认是nobody使用的,centso7或redhat7默认是nfsnobody用户,所以需要让nobody对/nfs目录有读写执行的权限
[root@client ~]# setfacl -m u:nobody:rwx /nfs/
// 先挂载
[root@localhost data]# mount -t nfs 192.168.182.151:/nfs /usr/src/
// 关联
[root@localhost ~]# docker run -itd --name httpd -p 80:80 --privileged=true -v /var/www/html:/usr/local/apache/htdocs centos /sbin/init //绑定 /var/www/html目录
2. 编写脚本
[root@83fef331955a src]# pwd
/usr/src
[root@83fef331955a src]# vim httpd.sh
[root@83fef331955a src]# chmod +x httpd.sh
[root@83fef331955a src]# cat httpd.sh
#!/bin/bash
route=/usr/local
path=/usr/src
yum -y install epel-release wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat epel-release
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
fi
if [ ! -d $path/apr-1.7.0 ];then
tar xf $path/apr-1.7.0.tar.gz -C $path
fi
if [ ! -d $path/apr-util-1.6.1 ];then
tar xf $path/apr-util-1.6.1.tar.gz -C $path
fi
if [ ! -d $path/httpd-2.4.51 ];then
tar xf $path/httpd-2.4.51.tar.gz -C $path
fi
cd $path/apr-1.7.0
if [ ! -d $route/apr ];then
sed -i 's/$RM "cfgfile"/#$RM "cfgfile"/g' configure
./configure --prefix=$route/apr
make && make install
fi
cd $path/apr-util-1.6.1
if [ ! -d $route/apr-util ];then
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
fi
cd $path/httpd-2.4.51
if [ ! -d $route/httpd-2.4.51 ];then
./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
make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
fi
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
if [ ! -f /usr/lib/systemd/system/httpd.service ];then
cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
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
EOF
sed -i '261s/index.html/test.html &/g' $rouet/apache/conf/httpd.conf
fi
systemctl daemon-reload
systemctl enable --now httpd
systemctl restart httpd.service
3. 执行脚本
[root@localhost ~]# docker exec -it httpd /bin/bash /usr/src/httpd.sh
此处省略N行
Installing configuration files
[PRESERVING EXISTING HTDOCS SUBDIR: /usr/local/apache/htdocs]
[PRESERVING EXISTING ERROR SUBDIR: /usr/local/apache/error]
[PRESERVING EXISTING ICONS SUBDIR: /usr/local/apache/icons]
[PRESERVING EXISTING CGI SUBDIR: /usr/local/apache/cgi-bin]
Installing header files
Installing build system files
Installing man pages and online manual
make[1]: Leaving directory '/usr/src/httpd-2.4.51'
4. 提供网站界面
例如:
// 将其改名为test.html,放到/usr/src目录下