//在脚本同一目录创建一个file目录用来存放httpd的三个软件包
#!/bin/bash
apache_version=2.4.54
apache_install_dir=/usr/local/apache
if [ $UID -ne 0 ];then
echo "你不是管理员用户,请使用管理员用户执行此脚本!"
exit
fi
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -rMs /sbin/nologin apache
fi
echo "安装工具包组"
yum -y groups mark install "Development Tools"
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget vim
rm -rf /tmp/*
tar -xf file/apr-1.6.5.tar.gz -C /tmp/
tar -xf file/apr-util-1.6.1.tar.gz -C /tmp/
tar -xf file/httpd-2.4.54.tar.gz -C /tmp/
sed -i '/$RM "$cfgfile"/d' /tmp/apr-1.6.5/configure
cd /tmp/apr-1.6.5
if [ ! -d /usr/local/apr ];then
./configure --prefix=/usr/local/apr && make && make install
fi
cd /tmp/apr-util-1.6.1
if [ ! -d /usr/local/apr-util ];then
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
fi
cd /tmp/httpd-$apache_version
if [ ! -d $apache_install_dir ];then
./configure --prefix=$apache_install_dir \
--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 && make install
fi
echo "export PATH=\$PATH:${apache_install_dir}/bin/" > /etc/profile.d/apache.sh
ln -s ${apache_install_dir}/include /usr/include/httpd &> /dev/null
grep 'apache' /etc/man_db.conf
if [ $? -ne 0 ];then
sed -i "22aMANDATORY_MANPATH ${apache_install_dir}/man" /etc/man_db.conf
else
echo 'good'
fi
sed -i '/#ServerName/s/#//g' ${apache_install_dir}/conf/httpd.conf
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=${apache_install_dir}/bin/apachectl start
ExecStop=${apache_install_dir}/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
if [ $? -eq 0 ];then
systemctl enable --now httpd
fi