Apache httpd 2.4 源码编译一键安装和卸载脚本
一键安装
[root@vm3 ~]# cat httpd-2.4-install.sh
#!/bin/bash
set -e
httpd_src=/usr/local/src/httpd
ls ${httpd_src} >& /dev/null || mkdir ${httpd_src} && cd ${httpd_src}
httpd_dir=/usr/local/httpd
ls ${httpd_dir} >& /dev/null || mkdir ${httpd_dir}
install_log_dir=${httpd_dir}/install
ls ${install_log_dir} >& /dev/null || mkdir ${install_log_dir}
install_log=${install_log_dir}/install-`date +"%F_%H-%M-%S"`.log
MPM=event
install_env(){
echo Install compile env via dnf | tee -a ${install_log}
dnf -y install gcc make pcre-devel openssl-devel expat-devel wget >& /dev/null
}
download_pkgs(){
echoho Download source code | tee -a ${install_log}
file_suffix=.tar.gz
apr_file=apr-1.7.0
apr_util_file=apr-util-1.6.1
httpd_file=httpd-2.4.52
apr_url=https://dlcdn.apache.org/apr/${apr_file}${file_suffix}
apr_util_url=https://dlcdn.apache.org/apr/${apr_util_file}${file_suffix}
httpd_url=https://dlcdn.apache.org/httpd/${httpd_file}${file_suffix}
wget $apr_url
wget $apr_util_url
wget $httpd_url
}
Unpack_pkgs(){
echo Unpack files | tee -a ${install_log}
tar -zxf ${apr_file}${file_suffix}
tar -zxf ${apr_util_file}${file_suffix}
tar -zxf ${httpd_file}${file_suffix}
mv ${apr_file} ${httpd_file}/srclib/apr
mv ${apr_util_file} ${httpd_file}/srclib/apr-util
}
installr_httpd(){
echo Start to comfigure | tee -a ${install_log}
cd ${httpd_file}
./configure --prefix=${httpd_dir} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=${MPM}
echo Start to make and install | tee -a ${install_log}
make && make install
echo add user and group | tee -a ${install_log}
userdel apache
useradd -s /sbin/nologin -r apache
}
init_httpd(){
sed -i 's/daemon/apache/' ${httpd_dir}/conf/httpd.conf
echo set env | tee -a ${install_log}
echo $PATH:${httpd_dir}/bin > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh >& /dev/null || echo '>>'
echo initiating httpd setting | tee -a ${install_log}
# mv /usr/local/httpd/htdocs/index.html{,.bak}
echo httpd install successful > /usr/local/httpd/htdocs/index.html
sed -i 's/#ServerName .*/ServerName localhost/' /usr/local/httpd/conf/httpd.conf
${httpd_dir}/bin/httpd -V
}
test_http(){
echo test httpd: | tee -a ${install_log}
${httpd_dir}/bin/apachectl -k start
curl localhost
${httpd_dir}/bin/apachectl -k stop
}
install_env
download_pkgs
Unpack_pkgs
installr_httpd
init_httpd
test_http
执行
[root@vm3 ~]# bash httpd-2.4-install.sh
日志
[root@vm3 install]# tail -f /usr/local/httpd/install/xxx.log
一键卸载
[root@vm3 ~]# cat httpd-2.4-uninstall.sh
#!/bin/bash
# delete httpd install via source compile
echo 'Start to delete Apache/2.4.52 (Unix)'
sleep 2
httpd_dir=/usr/local/httpd
httpd_stop="${httpd_dir}/bin/apachectl -k stop"
echo Stop httpd service...
cat ${httpd_dir}/logs/httpd.pid >& /dev/null && ${httpd_stop}
# if [ ! -z "${httpd_pid}" ]; then
# ${httpd_stop}
# fi
echo Delete httpd program...
rm -rf ${httpd_dir}
rm -rf /etc/profile.d/httpd.sh
rm -rf /usr/lib/systemd/system/httpd.service
echo delete apache user and group...
userdel apache
# groupdel apache
echo Source code will not be delete
echo done
测试
[root@vm3 ~]# bash httpd-2.4-uninstall.sh
Start to delete Apache/2.4.52 (Unix)
Stop httpd service...
Delete httpd program...
delete apache user and group...
Source code will not be delete
done