[root@27 ~]# cat install-httpd.sh
#!/bin/bash
PS3="请选择MPM模式:"
cpus=`lscpu | grep 'CPU(s)'| head -1 | awk '{print $2}'`
dist=`lsb_release -a | grep 'Distributor ID' | awk -F"[[:space:]]|:" '{print $4}'`
url_apr=https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.bz2
url_apr_util=https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.bz2
url_httpd=http://pub.mirrors.aliyun.com/apache/httpd/httpd-2.4.51.tar.gz
httpd_file=${url_httpd##*/}
apr_util_file=${url_apr_util##*/}
apr_file=${url_apr##*/}
install_dir=/apps/httpd24
install_prepare(){
if [ $dist = CentOS ];then
echo $dist
yum -y install gcc make pcre-devel openssl-devel expat-devel bzip2
elif [ $dist = Ubuntu ];then
echo $dist
apt -y install gcc make libapr1-dev libaprutil1-dev libpcre3 libpcre3-dev
else
echo "此脚本仅使用户CentOS/Ubuntu"
exit
fi
ll $install_dir &> /dev/null || mkdir -p $install_dir
#yum -y install gcc make pcre-devel openssl-devel expat-devel bzip2 &> /dev/null
wget $url_httpd && tar xf ${httpd_file} && echo httpd 下载 OK
wget $url_apr && tar xf ${apr_file} && echo OK
wget $url_apr_util && tar xf ${apr_util_file} && echo OK
mv -f ${apr_file%.tar*} ${httpd_file%.tar*}/srclib/apr
mv -f ${apr_util_file%.tar*} ${httpd_file%.tar*}/srclib/apr-util
}
install_httpd(){
cd ${httpd_file%.tar*}/
./configure \
--prefix=$install_dir \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=$1
make -j $cpus && make install
}
select_mod(){
select mod in prefork worker event 退出安装
do
case $REPLY in
1)
install_httpd $mod
env_config
exit
;;
2)
install_httpd $mod
env_config
exit
;;
3)
install_httpd $mod
env_config
exit
;;
4)
exit
esac
done
}
env_config(){
echo "PATH=${install_dir}/bin:$PATH" > /etc/profile.d/http24.sh
. /etc/profile.d/http24.sh
useradd -s /sbin/nologin -r apache
sed -Ei "s/daemon/apache/g" ${install_dir}/conf/httpd.conf
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=${install_dir}/bin/apachectl start
ExecReload=${install_dir}/bin/apachectl graceful
ExecStop=${install_dir}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
}
main(){
install_prepare
select_mod
}
main